Checking channel permissions instead of guild wide permissions as it is more accurate. Less chance for errors

pull/8/head
sgoudham 4 years ago
parent 71b6204793
commit ac8df1f96c

@ -410,45 +410,48 @@ async def on_member_remove(member):
async def on_command_error(ctx, args2):
"""Event to detect and handle errors"""
# Get Enso Chan
bot = ctx.guild.get_member(client.user.id)
# Get permissions for the bot within
perms = ctx.guild.me.permissions_in(ctx.message.channel)
# if the user did not specify an user
if isinstance(args2, commands.MissingRequiredArgument):
await on_command_missing_argument(ctx, bot)
await on_command_missing_argument(ctx, perms)
# if the user has spammed a command and invoked a cooldown
elif isinstance(args2, commands.CommandOnCooldown):
await on_command_cooldown(ctx, bot, args2)
await on_command_cooldown(ctx, perms, args2)
# if the user tries to access a command that isn't available
elif isinstance(args2, commands.CommandNotFound):
await on_command_not_found(ctx, bot)
await on_command_not_found(ctx, perms)
# if the user provides an argument that isn't recognised
elif isinstance(args2, commands.BadArgument):
await on_command_bad_argument(ctx, bot)
await on_command_bad_argument(ctx, perms)
# if the user does not the correct permissions to call a command
elif isinstance(args2, commands.MissingPermissions):
await on_command_permission(ctx, bot, args2)
await on_command_permission(ctx, perms, args2)
# if the bot is missing permissions needed
elif isinstance(args2, commands.BotMissingPermissions):
await on_bot_forbidden(ctx, bot, args2)
await on_bot_forbidden(ctx, perms, args2)
# if the bot is forbidden from performing the command
elif isinstance(args2, Forbidden):
await on_command_forbidden(ctx, bot)
await on_command_forbidden(ctx, perms)
# if the user tries to invoke a command that is only for the owner
elif isinstance(args2, commands.NotOwner):
await on_not_owner(ctx, bot)
await on_not_owner(ctx, perms)
async def send_error(ctx, bot, embed):
"""Sending error message to the user"""
async def send_error(ctx, perms, embed):
"""
Sending error message to the user
Only send error message if the channel permissions allow it
"""
if bot.guild_permissions.send_messages and bot.guild_permissions.embed_links:
if perms.send_messages and perms.embed_links:
await ctx.send(embed=embed)
else:
print("Error: Error Handling Message Could Not Be Sent")
async def on_bot_forbidden(ctx, bot, args2):
async def on_bot_forbidden(ctx, perms, args2):
"""Handles Missing Bot Permissions Errors"""
# Convert list into string of the missing permissions
@ -457,46 +460,46 @@ async def on_bot_forbidden(ctx, bot, args2):
embed = Embed(description=f"❌ I Need **{missing_perms}** Permission(s) to Execute This Command! ❌",
colour=enso_embedmod_colours)
await send_error(ctx, bot, embed)
await send_error(ctx, perms, embed)
async def on_command_forbidden(ctx, bot):
async def on_command_forbidden(ctx, perms):
"""Handles Forbidden Error"""
embed = Embed(description="**❌ I Don't Have Permissions To Execute This Command ❌**",
colour=enso_embedmod_colours)
await send_error(ctx, bot, embed)
await send_error(ctx, perms, embed)
async def on_command_bad_argument(ctx, bot):
async def on_command_bad_argument(ctx, perms):
"""Handles Bad Argument Errors (Argument can't be read properly)"""
embed = Embed(description="**❌ Uh oh! Couldn't find anyone to mention! Try again! ❌**",
colour=enso_embedmod_colours)
await send_error(ctx, bot, embed)
await send_error(ctx, perms, embed)
async def on_command_not_found(ctx, bot):
async def on_command_not_found(ctx, perms):
"""Handles the command not found error"""
embed = Embed(description=f"Command Not Found! ❌ Please use **{ctx.prefix}help** to see all commands",
colour=enso_embedmod_colours)
await send_error(ctx, bot, embed)
await send_error(ctx, perms, embed)
async def on_command_cooldown(ctx, bot, error):
async def on_command_cooldown(ctx, perms, error):
"""Handles Cooldown Errors"""
embed = Embed(description=f"That command is on cooldown. Try again in **{error.retry_after:,.2f}** seconds",
colour=enso_embedmod_colours)
await send_error(ctx, bot, embed)
await send_error(ctx, perms, embed)
async def on_command_permission(ctx, bot, args2):
async def on_command_permission(ctx, perms, args2):
"""Handles User Missing Permissions Errors"""
# Convert list into string of the missing permissions
@ -505,26 +508,26 @@ async def on_command_permission(ctx, bot, args2):
embed = Embed(description=f"❌ Uh oh! You Need **{missing_perms}** Permission(s) To Execute This Command! ❌",
colour=enso_embedmod_colours)
await send_error(ctx, bot, embed)
await send_error(ctx, perms, embed)
async def on_command_missing_argument(ctx, bot):
async def on_command_missing_argument(ctx, perms):
"""Handles the missing argument error"""
embed = Embed(description="Required Argument(s) Missing!"
f"\nUse **{ctx.prefix}help** to find how to use **{ctx.command}**",
colour=enso_embedmod_colours)
await send_error(ctx, bot, embed)
await send_error(ctx, perms, embed)
async def on_not_owner(ctx, bot):
async def on_not_owner(ctx, perms):
"""Handles the error when the user is not the owner and tries to invoke owner only command"""
embed = Embed(description="**❌ Owner Only Command ❌**",
colour=enso_embedmod_colours)
await send_error(ctx, bot, embed)
await send_error(ctx, perms, embed)
# Run the bot, allowing it to come online

Loading…
Cancel
Save