如何在机器人启动时向其所在的每个服务器发送消息?

如何在机器人启动时向其所在的每个服务器发送消息?

问题描述:

所以我想向机器人所在的所有服务器发送公告。我在github上找到了它,但是它需要服务器ID和通道ID。

So i want to send a announcement to all the servers my bot is in. I found this on github, but it requires a server id and a channel id.

@bot.event
async def on_ready():
    server = bot.get_server("server id")
    await bot.send_message(bot.get_channel("channel id"), "Test")

我也找到了类似的问题,但是在discord.js中。它说的是默认频道,但是当我尝试时:

I also found a similar question, but it is in discord.js. It says something with default channel, but when i tried:

@bot.event
async def on_ready():
    await bot.send_message(discord.Server.default_channel, "Hello everyone")

它给了我错误:
目标必须是Channel,PrivateChannel,User或Object

It gave me the error: Destination must be Channel, PrivateChannel, User, or Object

首先要回答您的问题关于 default_channel :自2017年6月起,discord不再定义默认渠道,因此, default_channel 服务器的元素通常设置为无。

Firstly to answer your question about default_channel: Since about June 2017, discord no longer defines a "default" channel, and as such, the default_channel element of a server is usually set to None.

其次,通过说 discord.Server.default_channel ,您要求类定义的元素,而不是实际的渠道。要获得实际的频道,您需要一个服务器实例。

Secondly, by saying discord.Server.default_channel, you're asking for an element of the class definition, not an actual channel. To get an actual channel, you need an instance of a server.

现在要回答原始问题,即向每个频道发送一条消息,您需要找到

Now to answer the original question, which is to send a message to every channel, you need to find a channel in the server that you can actually post messages in.

@bot.event
async def on_ready():
    for server in bot.servers: 
        # Spin through every server
        for channel in server.channels: 
            # Channels on the server
            if channel.permissions_for(server.me).send_messages:
                await bot.send_message(channel, "...")
                # So that we don't send to every channel:
                break