Skip to content

Commit fd44504

Browse files
committed
add error handler(global level) and reformatting
1 parent 4a6963d commit fd44504

File tree

7 files changed

+83
-12
lines changed

7 files changed

+83
-12
lines changed
File renamed without changes.

position5/__main__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ def setup_logging():
4848
'cogs.activity',
4949
'cogs.basic',
5050
'cogs.cricket',
51-
'cogs.fun',
5251
'cogs.embed',
53-
'cogs.meme_pic',
52+
'cogs.emotes',
53+
'cogs.error_handler',
54+
'cogs.fun',
55+
'cogs.pic',
5456
'cogs.poll',
5557
'cogs.react',
5658
'cogs.stock',

position5/cogs/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
EMOTES_PATH = 'assets/emotes/'
13+
PIC_PATH = 'assets/pic/'
1314

1415
DISCLAIMER = '''
1516
I am NOT a SEBI registered advisor or a financial adviser.

position5/cogs/meme_pic.py renamed to position5/cogs/emotes.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from . import EMOTES_PATH, delete_message, log_params
66

77

8-
class MemePic(commands.Cog):
8+
class Emotes(commands.Cog):
99
def __init__(self, bot):
1010
self.bot = bot
1111
self._refresh_emotes()
@@ -20,12 +20,6 @@ def _refresh_emotes(self):
2020
emote.split('.', 1)[0].lower(): emote for emote in self.emotes
2121
}
2222

23-
@commands.command(name='xdoubt', description='xdoubt pic')
24-
@delete_message()
25-
@log_params()
26-
async def xdoubt_command(self, ctx):
27-
await ctx.send(file=discord.File('assets/meme_pic/xdoubt.png'))
28-
2923
@commands.command(name='emote', description='emote as pictures', usage='<emote>')
3024
@delete_message()
3125
@log_params()
@@ -88,4 +82,4 @@ async def emotes_command(self, ctx, *, search: str = None):
8882

8983

9084
def setup(bot):
91-
bot.add_cog(MemePic(bot))
85+
bot.add_cog(Emotes(bot))

position5/cogs/error_handler.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import logging
2+
from discord.ext import commands
3+
4+
log = logging.getLogger('position5.error_handler')
5+
6+
7+
class ErrorHandler(commands.Cog):
8+
def __init__(self, bot):
9+
self.bot = bot
10+
11+
@commands.Cog.listener()
12+
async def on_command_error(self, ctx, error):
13+
"""The event triggered when an error is raised while invoking a command.
14+
Parameters
15+
------------
16+
ctx: commands.Context
17+
The context used for command invocation.
18+
error: commands.CommandError
19+
The Exception raised.
20+
"""
21+
22+
# This prevents any commands with local handlers being handled here in on_command_error.
23+
if hasattr(ctx.command, 'on_error'):
24+
return
25+
26+
# Allows us to check for original exceptions raised and sent to CommandInvokeError.
27+
# If nothing is found. We keep the exception passed to on_command_error.
28+
error = getattr(error, 'original', error)
29+
30+
if isinstance(error, commands.CommandNotFound):
31+
log.info(
32+
'Command Not Found | Message: %s | Author: %s',
33+
ctx.message.content,
34+
ctx.author.name,
35+
)
36+
37+
# elif isinstance(error, commands.DisabledCommand):
38+
# await ctx.send(f'{ctx.command} has been disabled.')
39+
40+
# elif isinstance(error, commands.NoPrivateMessage):
41+
# try:
42+
# await ctx.author.send(f'{ctx.command} can not be used in Private Messages.')
43+
# except discord.HTTPException:
44+
# pass
45+
46+
# elif isinstance(error, commands.BadArgument):
47+
# if ctx.command.qualified_name == 'tag list':
48+
# await ctx.send('I could not find that member. Please try again.')
49+
50+
else:
51+
log.info('Exception in command %s', ctx.command)
52+
log.error('Type %s | Error: %s', type(error), error)
53+
54+
55+
def setup(bot):
56+
bot.add_cog(ErrorHandler(bot))

position5/cogs/pic.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import discord
2+
from discord.ext import commands
3+
from . import delete_message, log_params, PIC_PATH
4+
5+
6+
class Pic(commands.Cog):
7+
def __init__(self, bot):
8+
self.bot = bot
9+
10+
@commands.command(name='xdoubt', description='xdoubt pic')
11+
@delete_message()
12+
@log_params()
13+
async def xdoubt_command(self, ctx):
14+
await ctx.send(file=discord.File(f'{PIC_PATH}xdoubt.png'))
15+
16+
17+
def setup(bot):
18+
bot.add_cog(Pic(bot))

position5/cogs/poll.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async def poll_without_options(self, ctx, *, question):
1515
reactions = ['✅', '❌', '💤']
1616
embed = (
1717
discord.Embed(title=question)
18-
.set_footer(text='Poll will end in 100 seconds! Please react once.')
18+
.set_footer(text='Poll will end in 30 seconds! Please react once.')
1919
.set_author(
2020
name=ctx.message.author.name, icon_url=ctx.message.author.avatar_url
2121
)
@@ -24,7 +24,7 @@ async def poll_without_options(self, ctx, *, question):
2424
for reaction in reactions:
2525
await message.add_reaction(reaction)
2626

27-
await asyncio.sleep(10)
27+
await asyncio.sleep(30)
2828

2929
total_count = -3
3030
reaction_count = {}

0 commit comments

Comments
 (0)