向所有频道发送消息 — Discord.py

时间:2021-01-29 06:41:26

标签: python discord discord.py

如何同时向 Discord 服务器中的每个频道发送消息?

我在另一篇文章中使用了此代码,但在运行该命令时没有收到任何响应。

@client.command(pass_context=True)
async def broadcast(ctx, *, msg):
    for guild in bot.guilds:
        for channel in guild.channels:
            try:
                await bot.send_message(channel, msg)
            except Exception:
                continue
            else:
                break

1 个答案:

答案 0 :(得分:1)

您在某些地方使用了 create_app(),在其他地方使用了 client,更多的是此代码效率不高,因为在调用它时不需要遍历公会仅来自一台服务器,这会导致多台服务器中的垃圾邮件。我还注意到您正在使用旧版 bot 中的函数。尝试改用这个:

discord.py