将 dm 发送给用户 discord.py 时出错

时间:2021-06-21 09:44:33

标签: python discord.py

我想将 dm 发送给具有“通知”角色的特定用户。昨天,它运行良好,但现在我无法将 dm 发送给用户。这里的错误代码:

  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 109, in mesaj
    await m.send(msg)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/abc.py", line 1013, in send
    channel = await self._get_channel()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/member.py", line 299, in _get_channel
    ch = await self.create_dm()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/member.py", line 142, in general
    return await getattr(self._user, x)(*args, **kwargs)
AttributeError: 'ClientUser' object has no attribute 'create_dm'

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 939, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, 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: AttributeError: 'ClientUser' object has no attribute 'create_dm'

我的代码在这里:

    async def mesaj(ctx):
        msg = "New text message"
        global members
        role = discord.utils.get(ctx.guild.roles, name="Notification")
        members = [m for m in ctx.guild.members if role in m.roles] 
        for m in members:
            await m.send(msg)

1 个答案:

答案 0 :(得分:2)

您无法向机器人本身发送消息,请使用 try/except

for m in members:
    try:
        await m.send(msg)
    except Exception as exc:
        pass