Reputation: 47
So I have an Emergency command incase there is something and I have to DM every one but there is a problem with the bots as DMing them causes an error (the bots role is "BOTS")
@bot.command(pass_context=True)
async def emergency(ctx, *, message: str):
if admin in [role.id for role in ctx.message.author.roles]:
for server_member in ctx.message.server.members:
await bot.send_message(server_member, message)
else:
await bot.say("DENIED, You do not have permission to use this command")
and the Error
Traceback (most recent call last):
File "C:\Users\adamk\PycharmProjects\bot\venv\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
yield from command.invoke(ctx)
File "C:\Users\adamk\PycharmProjects\bot\venv\lib\site-packages\discord\ext\commands\core.py", line 374, in invoke
yield from injected(*ctx.args, **ctx.kwargs)
File "C:\Users\adamk\PycharmProjects\bot\venv\lib\site-packages\discord\ext\commands\core.py", line 54, in wrapped
raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: FORBIDDEN (status code: 403): Cannot send messages to this user
Upvotes: 1
Views: 1110
Reputation: 60974
There are some other circumstances in which you'll get this message (you've been blocked, that user has DMs disabled entirely, etc.), so it makes more sense to just catch the error and proceed each time it happens.
@bot.command(pass_context=True)
async def emergency(ctx, *, message: str):
if admin in [role.id for role in ctx.message.author.roles]:
for server_member in ctx.message.server.members:
try:
await bot.send_message(server_member, message)
except discord.Forbidden:
pass
else:
await bot.say("DENIED, You do not have permission to use this command")
If you also want to check for the role, you can do so with discord.utils.get
if get(server_member.roles, name="BOTS"):
# has role
else:
# does not have role
Upvotes: 1