简体   繁体   English

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

[英]How to send a message on the bot startup to every server it is in?

I want to send an announcement to all the servers my bot is in. I found this on GitHub, but it requires a server ID and a channel ID.我想向我的机器人所在的所有服务器发送通知。我在 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")

I found a similar question, but it is in discord.js.我发现了一个类似的问题,但它在 discord.js 中。 It says something with default channel, but when I tried this:它用默认频道说了一些话,但是当我尝试这个时:

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

it gave me this error:它给了我这个错误:

Destination must be Channel, PrivateChannel, User, or Object

First, 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 .首先,回答您关于default_channel的问题:自 2017 年 6 月左右以来,Discord 不再定义“默认”频道,因此,服务器的default_channel元素通常设置为None

Next, by saying discord.Server.default_channel , you're asking for an element of the class definition, not an actual channel.接下来,通过说discord.Server.default_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:现在,要回答最初的问题,即向每个频道发送消息,您需要在服务器中找到一个可以实际发布消息的频道:

python
    @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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python discord 向机器人所在的每个服务器发送消息 - How can I send a message to every server the bot is in with python discord 如何向我的机器人所在的每台服务器发送消息 python discord - How can I send a message to every server my bot is in with python discord Discord Bot Python 每小时发一条消息 - Discord Bot Python send a message every hour 如何让 discord py bot 在被邀请到服务器时发送消息 - how to make discord py bot send a message when it is invited to a server 机器人加入服务器时发送消息 - Send a message when the bot joins a server 如何让我的 python 电报机器人每天在特定时间发送消息? - how to make my python telegram bot to send message at certain time every day? 用 Python 编写 Discord 机器人 - 如何让它每天在特定时间发送消息? - Programming a Discord bot in Python- How do I make it send a message every day at a certain time? 如何让机器人添加对特定频道中发送的每条消息的反应? - How do i make the bot to add reaction to every message send in a specific channel? 如何让我的 discord.py 机器人删除在特定频道上发送的每条消息? - How can I make my discord.py bot delete every message send on a specific channel? 如何让机器人每 5 分钟或因为另一个事件发布/发送消息? - How to let a bot post/send a message every 5 minutes or because of another event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM