This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
/
command_handler.py
71 lines (65 loc) · 3.72 KB
/
command_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from discord.ext import commands
import discord
import traceback
import random
import json
from cogs.utils import checks
from cogs.utils.tools import generate_id, resolve_emoji
class CommandHandler:
def __init__(self, bot):
self.bot = bot
self.__sendable_exceptions = (
checks.ChannelNotNSFW,
commands.BadArgument,
commands.UserInputError,
)
async def on_command(self, ctx):
self.bot.executed_commands += 1
async def on_command_error(self, ctx, error):
if isinstance(error, commands.CommandNotFound):
pass
elif isinstance(error, commands.MissingPermissions):
await ctx.send(resolve_emoji('ERROR',
ctx) + f' You seem to be missing the `{", ".join(error.missing_perms)}` permission(s).')
elif isinstance(error, commands.BotMissingPermissions):
await ctx.send(resolve_emoji('ERROR',
ctx) + f" I need the permission(s) `{', '.join(error.missing_perms)}` to run this command.")
elif isinstance(error, commands.CheckFailure):
await ctx.send(resolve_emoji('ERROR', ctx) + " You are not authorized to use this command.")
elif isinstance(error, commands.CommandOnCooldown):
m, s = divmod(error.retry_after, 60)
h, m = divmod(m, 60)
await ctx.send(resolve_emoji('ERROR', ctx)
+ f' You can use this command again in {"%d hours, %02d minutes and %02d seconds" % (h, m, s)}'
+ (" (about now)." if error.retry_after == 0 else "."))
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send(resolve_emoji('ERROR',
ctx) + f" Missing required argument `{error.param.name}`, check `{ctx.prefix}help {ctx.command}`")
ctx.command.reset_cooldown(ctx)
elif isinstance(error, self.__sendable_exceptions):
await ctx.send(resolve_emoji('ERROR', ctx) + ' ' + str(error))
ctx.command.reset_cooldown(ctx)
else:
errid = generate_id()
await ctx.send(resolve_emoji('ERROR', ctx)
+ f" Unhandled exception. Report this on my support guild (<https://discord.gg/ewvvKHM>) with the ID **{errid}**")
err_msg = f"Unhandled exception on command `{ctx.command}`\n**Error ID:** {errid}\n**Content:** {ctx.message.clean_content}\n**Author:** {ctx.author} ({ctx.author.id})\n**Guild:** {ctx.guild} ({ctx.guild.id})\n"
trace = ''.join(traceback.format_exception(type(error), error, error.__traceback__))
print(err_msg + f"**Traceback:** ```py\n{trace}\n```")
try:
self.bot.webhook.send(err_msg + f"**Traceback:** ```py\n{trace}\n```")
except discord.HTTPException:
self.bot.webhook.send(err_msg + '**Traceback:** ' + await self.bot.post_to_haste(trace))
if random.randint(1, 5) == 3:
await ctx.send('O-oh? Looks like you found a bug! Added it to your inventory!')
results = self.bot.query_db(f'''SELECT items FROM users WHERE userid={ctx.author.id}''')
items = json.loads(results[0][0].replace("'", '"')) if results and results[0][0] else json.loads('{}')
try:
amnt = items['BUG']
items['BUG'] = amnt + 1
except KeyError:
items['BUG'] = 1
self.bot.query_db(f'''INSERT INTO users (userid, items) VALUES ({ctx.author.id}, "{str(items)}")
ON DUPLICATE KEY UPDATE items="{str(items)}";''')
def setup(bot):
bot.add_cog(CommandHandler(bot))