如何在机器人启动时向它所在的每台服务器发送消息? [英] How to send a message on the bot startup to every server it is in?

查看:95
本文介绍了如何在机器人启动时向它所在的每台服务器发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想向我的机器人所在的所有服务器发送通知.我在 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")

它给了我错误:Destination 必须是 Channel、PrivateChannel、User 或 Object

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

推荐答案

首先回答您关于 default_channel 的问题:自 2017 年 6 月左右起,discord 不再定义默认"频道,因此,服务器的 default_channel 元素通常设置为 None.

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

这篇关于如何在机器人启动时向它所在的每台服务器发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆