Skepality
Skepality

Reputation: 21

How to DM everyone in a Discord server with discord.py?

I am currently trying to get my bot to DM all members of a server when the command :dmall is sent, but it doesn't appear to work.

My code:

@bot.command()
@commands.is_owner()
async def dmall(ctx,*,message):
    for mem in ctx.guild.members:
        await ctx.message.delete()
        try:
            await mem.send(message)
            await ctx.send(f'Sent dm to: {mem.name}')
        except:
             print('User dm closed')

@dmall.error
async def dmall_error(ctx, error):
    if isinstance(error, commands.CheckFailure):
        await ctx.send('sorry comrade, you arent allowed to do that')

Upvotes: 2

Views: 3860

Answers (1)

Harmon758
Harmon758

Reputation: 5157

It's impossible to know for certain without a traceback, but this is likely erroring when you're unable to send a DM to a member. This can happen if the member has set their privacy settings not to allow DMs from the server's members or has the bot blocked.

You should remove the catch-all exception handler, which is bad practice, exhibited by this specific instance of inability to determine the actual specific issue, and check the traceback.

Upvotes: 1

Related Questions