Python DM all command

1.3k views Asked by At

So, I was making a custom bot for my server and had a problem, heres my code:

@bot.command()
@commands.has_role('| Owner')
async def dmall(ctx,desc):
    title = f'message from {ctx.message.author}'
    await ctx.send('Sending messages!')
    for members in bot.get_all_members():
        embed = discord.Embed(title=title, description=desc)
        await members.send(embed=embed)
        print('Sent a message!')
        time.sleep(3)

The error i get:

Ignoring exception in command dmall:
Traceback (most recent call last):
  File "/home/container/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "/home/container/main.py", line 37, in dmall
    await members.send(embed=embed)
  File "/home/container/discord/abc.py", line 864, in send
    channel = await self._get_channel()
  File "/home/container/discord/member.py", line 250, in _get_channel
    ch = await self.create_dm()
  File "/home/container/discord/member.py", line 110, in general
    return getattr(self._user, x)(*args, **kwargs)
AttributeError: 'ClientUser' object has no attribute 'create_dm'

I have been trying to figure it out, the thing is im not using create_dm anywhere in my code.

1

There are 1 answers

2
Nurqm On

bot.get_all_members() is probably causing that error. You can get the members with ctx.guild members. So you can do this:

@bot.command()
@commands.has_role('| Owner')
async def dmall(ctx,desc):
    title = f'message from {ctx.message.author}'
    await ctx.send('Sending messages!')
    for member in ctx.guild.members:
        embed = discord.Embed(title=title, description=desc)
        await member.send(embed=embed)
        print('Sent a message!')
        await asyncio.sleep(3)

And also I added asyncio.sleep() because as I know, time.sleep() blocks all the code so don't forget to import it.