2

I am able to get the bot to direct message anyone with a specific role using the following code:

@bot.command(pass_context=True)
@commands.has_any_role(roleCheck)
async def dmrole(ctx, role: discord.Role, *, message):
    for member in ctx.message.server.members:
        if role in member.roles:
            await bot.send_message(member, message)

However, when I try to make the bot dm all users in my server, only I receive a message. This is the code I used:

@bot.command(pass_context=True)
@commands.has_any_role(roleCheck)
async def dmall(ctx,*, message):
    for member in ctx.message.server.members:
        await bot.send_message(member, message)

When checking the output window, I get the error:

"Forbidden: FORBIDDEN (status code: 403): Cannot send messages to this user"

*Please note that there are various imports and variables defined outside of the code snippets shown here.

Any pointers as to where I am going wrong would be very much appreciated.

Ivan Chan
  • 39
  • 1
  • 8
  • Your bot may be missing the permissions it needs to be able to message the user. Does it have the "manage role" permissions? – b13rg Jun 25 '18 at 22:23
  • Yes, I have given it administrator perms and made sure that it was not a permission issue – Ivan Chan Jun 25 '18 at 22:25
  • thanks, that is a really good idea. will give that a try. – Ivan Chan Jun 25 '18 at 22:36
  • 2
    You will encounter FORBIDDEN if you attempt to direct message a user that has DMs disabled for non-friends. This is not a permission issue. – Sam Rockett Jun 25 '18 at 23:04

1 Answers1

1

So I found that simply catching and passing the exceptions was able to fix the error. Thanks for all your help.

Ivan Chan
  • 39
  • 1
  • 8