【问题标题】:How to send a message on the bot startup to every server it is in?如何在机器人启动时向它所在的每台服务器发送消息?
【发布时间】:2018-06-12 16:25:35
【问题描述】:

所以我想向我的机器人所在的所有服务器发送一个公告。我在 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 中。它说的是默认频道,但是当我尝试时:

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

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

【问题讨论】:

    标签: discord.py


    【解决方案1】:

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

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

    现在,要回答最初的问题,即向每个频道发送消息,您需要在服务器中找到一个您可以实际发布消息的频道:

    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
    

    【讨论】:

    • 有点用。它只将消息发送到一台服务器。我需要循环播放吗?
    猜你喜欢
    • 2020-11-19
    • 2018-05-26
    • 2019-01-23
    • 2021-01-28
    • 2021-03-19
    • 2021-06-02
    • 1970-01-01
    • 2022-01-19
    • 2020-08-22
    相关资源
    最近更新 更多