qRnt
qRnt

Reputation: 35

Getting error while sending dm to users discord.py

I want to send dm to specific users with "Notification" role. Yesterday, it was working good, but now I can't send dm to users. The error code here:

  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'

My code here:

    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)

Upvotes: 0

Views: 147

Answers (1)

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15728

You can't send a message to the bot itself, use a try/except block

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

Upvotes: 3

Related Questions