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

2024-05-16 23:11:23 发布

您现在位置:Python中文网/ 问答频道 /正文

如何同时向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

Tags: 代码in命令服务器client消息forbot
1条回答
网友
1楼 · 发布于 2024-05-16 23:11:23

您在某些地方使用了client,在其他一些地方使用了bot,更重要的是,这段代码不是非常有效,因为当您仅从一台服务器调用它时,不需要迭代公会,这将在多台服务器中导致垃圾邮件。我还注意到您使用的函数来自discord.py的旧版本。请尝试使用此选项:

@client.command()
async def broadcast(ctx, *, msg):
    for channel in ctx.guild.text_channels:
        try:
            await channel.send(msg)
        except:
            continue

相关问题 更多 >