【问题标题】:DM Everyone CommandDM所有人命令
【发布时间】:2021-01-12 20:15:26
【问题描述】:

我不明白为什么这段代码有错误:

@client.command()
async def dmall(ctx, message):
    for m in client.get_all_members():
        await m.send(message)
    await ctx.send("Done!")

我的 DM 已开启,但出现以下错误: 我也尝试将它与我的 alt 一起使用,但同样的错误。

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 49, in dmall
    await m.send("Hello! This is a DM :)")
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/abc.py", line 850, in send
    channel = await self._get_channel()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/member.py", line 243, in _get_channel
    ch = await self.create_dm()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/user.py", line 715, in create_dm
    data = await state.http.start_private_message(self.id)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http.py", line 245, in request
    raise HTTPException(r, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50007): Cannot send messages to this user

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 855, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: HTTPException: 400 Bad Request (error code: 50007): Cannot send messages to this user

另外,附带说明一下,我没有将其用于恶意目的。我知道滥用它可能会导致禁令。

【问题讨论】:

  • 尝试找出当它崩溃时它正在向哪个成员发送消息。在m.send 周围使用try/except,如果出错,记录成员的用户名/用户ID/等。
  • 实际上是所有的机器人都导致了错误。

标签: python discord discord.py discord.py-rewrite dm


【解决方案1】:

实际上,我的代码文件夹中的代码已经存在一段时间了,代码将是

@client.command()
async def dmall(ctx, *, message):
    guild = ctx.guild
    for m in guild.members:
        try:
            await m.send(message)
            await ctx.send(f"[Success] Successfully sent to {m}.")

        except Exception as e:
            await ctx.send(f"[Failure] Failed to send to {m}\n\n\n{e}")

    await ctx.send(f"[Done] Finnished!")

注意await ctx.send(f"[Done] Finnished!") 不是错字。

【讨论】:

    【解决方案2】:

    使用 try 语句会让它跳过它不能发送消息的所有用户:

    @client.command()
    async def dmall(ctx, message):
        for m in client.get_all_members():
            try:
               await m.send(message)
            except:
               print("couldn't send message to "+m)
        await ctx.send("Done!")
    

    这可能是 Discord 方面的错误,或者您的机器人在服务器中的权限有问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-21
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      • 2021-01-17
      相关资源
      最近更新 更多