Handle discord permission errors when users have DMs disabled

feature/embed-uploads
Tristan Gosselin-Hane 7 years ago
parent 937866234d
commit 23977145c8
  1. 42
      webhook-bridge.py

@ -285,6 +285,7 @@ def main():
# PM Commands
if message.content.startswith("mc!help"):
try:
send_channel = message.channel
if isinstance(message.channel, discord.abc.GuildChannel):
await message.delete()
@ -294,9 +295,18 @@ def main():
send_channel = message.author.dm_channel
msg = get_discord_help_string()
await send_channel.send(msg)
except discord.errors.Forbidden:
if isinstance(message.author, discord.abc.User):
msg = "{}, please allow private messages from this bot.".format(message.author.mention)
error_msg = await message.channel.send(msg)
await asyncio.sleep(3)
await error_msg.delete()
finally:
return
elif message.content.startswith("mc!register"):
try:
# TODO: Catch the Forbidden error in a smart way before running application logic
send_channel = message.channel
if isinstance(message.channel, discord.abc.GuildChannel):
await message.delete()
@ -320,6 +330,13 @@ def main():
msg = "Please connect your minecraft account to `{}.{}:{}` in order to link it to this bridge!".format(new_token, config.auth_dns, config.auth_port)
session.close()
await send_channel.send(msg)
except discord.errors.Forbidden:
if isinstance(message.author, discord.abc.User):
msg = "{}, please allow private messages from this bot.".format(message.author.mention)
error_msg = await message.channel.send(msg)
await asyncio.sleep(3)
await error_msg.delete()
finally:
return
# Global Commands
@ -330,12 +347,20 @@ def main():
return
if message.author.id not in config.admin_users:
await message.delete()
try:
dm_channel = message.author.dm_channel
if not dm_channel:
await message.author.create_dm()
dm_channel = message.author.dm_channel
msg = "Sorry, you do not have permission to execute that command!"
await dm_channel.send(msg)
except discord.errors.Forbidden:
if isinstance(message.author, discord.abc.User):
msg = "{}, please allow private messages from this bot.".format(message.author.mention)
error_msg = await message.channel.send(msg)
await asyncio.sleep(3)
await error_msg.delete()
finally:
return
session = database_session.get_session()
channels = session.query(DiscordChannel).filter_by(channel_id=this_channel).all()
@ -359,15 +384,22 @@ def main():
return
if message.author.id not in config.admin_users:
await message.delete()
try:
dm_channel = message.author.dm_channel
if not dm_channel:
await message.author.create_dm()
dm_channel = message.author.dm_channel
msg = "Sorry, you do not have permission to execute that command!"
await dm_channel.send(msg)
except discord.errors.Forbidden:
if isinstance(message.author, discord.abc.User):
msg = "{}, please allow private messages from this bot.".format(message.author.mention)
error_msg = await message.channel.send(msg)
await asyncio.sleep(3)
await error_msg.delete()
finally:
return
session = database_session.get_session()
channels = session.query(DiscordChannel).all()
deleted = session.query(DiscordChannel).filter_by(channel_id=this_channel).delete()
session.commit()
session.close()
@ -383,6 +415,7 @@ def main():
elif message.content.startswith("mc!"):
# Catch-all
send_channel = message.channel
try:
if isinstance(message.channel, discord.abc.GuildChannel):
await message.delete()
dm_channel = message.author.dm_channel
@ -391,6 +424,13 @@ def main():
send_channel = message.author.dm_channel
msg = "Unknown command, type `mc!help` for a list of commands."
await send_channel.send(msg)
except discord.errors.Forbidden:
if isinstance(message.author, discord.abc.User):
msg = "{}, please allow private messages from this bot.".format(message.author.mention)
error_msg = await message.channel.send(msg)
await asyncio.sleep(3)
await error_msg.delete()
finally:
return
elif not message.author.bot:

Loading…
Cancel
Save