V
V
Vladislav Mukhin2021-02-07 01:04:15
Python
Vladislav Mukhin, 2021-02-07 01:04:15

How to send a DM message to all server users (excluding bots, they should not be sent a message)?

@Client.command()
@commands.is_owner()
async def dm(ctx, *, message):
    for server_member in ctx.message.guild.members:
        await server_member.create_dm()
        await asyncio.sleep(1)


or

@Client.command()
@commands.is_owner()
async def dm(ctx, *, message):
    for server_member in ctx.message.guild.members:
        await server_member.create_dm()
        await asyncio.sleep(1)
        await server_member.send(message)


an error is thrown:
spoiler
Ignoring exception in command dm:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main511.py", line 1170, in dm
await server_member.send(message)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\abc.py", line 883, in send
channel = await self._get_channel()
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\member.py", line 257, in _get_channel
ch = await self.create_dm()
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\member.py", line 110, in general
return 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 "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\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'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Nevzorov, 2021-02-07
@SladkayaDoza

guild.memberscontains all server members, including your bot, which, unlike other server members, is defined on the basis of the bot's "client" object and does not have a create_dm.
The question is - why do you "explicitly" create a LAN channel, if the documentation states that this should be rarely used, and in most cases it happens automatically?
https://discordpy.readthedocs.io/en/stable/api.htm...

This should be rarely called, as this is done transparently for most people.

Regarding "excluding bots, they should not send a message" - add a check for User.bot:
https://discordpy.readthedocs.io/en/stable/api.htm...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question