首页 文章

如何在bot启动时将消息发送到它所在的每个服务器?

提问于
浏览
3

所以我想向我的机器人所在的所有服务器发送一个通知 . 我在github上发现了这个,但它需要服务器ID和通道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中 . 它用默认 Channels 说了些什么,但当我尝试时:

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

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

1 回答

  • 3

    首先回答你关于 default_channel 的问题:自2017年6月左右开始,discord不再定义"default" Channels ,因此,服务器的 default_channel 元素通常设置为None .

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

    现在回答原始问题,即向每个 Channels 发送消息,您需要在服务器中找到一个可以实际发布消息的 Channels .

    @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
    

相关问题