【问题标题】:Sending a message to all channels — Discord.py向所有频道发送消息 — Discord.py
【发布时间】:2021-05-03 02:28:48
【问题描述】:

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

【问题讨论】:

  • 欢迎来到 Stackoverflow。在except 块中添加一些print 语句以查看是否有任何问题是有意义的。
  • 您的命令有@client.command(),但您在函数中使用了bot.guilds。请根据您的定义仅使用一个。
  • 我稍微摆弄了一下,最后得到了这个错误:raise MissingRequiredArgument(param) discord.ext.commands.errors.MissingRequiredArgument: msg is a required argument that is missing.
  • 那是因为你没有传递任何消息
  • 另外,你为什么用else

标签: python discord discord.py


【解决方案1】:

您在某些地方使用了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

【讨论】:

  • @graacelinyoung 使用text_channels 而不是channels,这将花费更少的时间。
猜你喜欢
  • 1970-01-01
  • 2021-03-24
  • 2021-04-04
  • 1970-01-01
  • 1970-01-01
  • 2020-12-03
  • 2021-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多