1
Fork 0
mirror of https://github.com/allthingslinux/tux.git synced 2024-10-03 00:53:12 +00:00

broke out error handling

This commit is contained in:
ExploitDemon 2024-01-08 14:21:00 -07:00
parent 8638fb0277
commit 5ab77eab0a
5 changed files with 34 additions and 6 deletions

View file

@ -1,3 +1,4 @@
# cog_loader.py
import logging
import os
import traceback

23
tux/error_handler.py Normal file
View file

@ -0,0 +1,23 @@
# error_handler.py
from discord.ext import commands
from utils._tux_logger import TuxLogger
logger = TuxLogger(__name__)
class ErrorHandler:
@staticmethod
async def handle_command_not_found(ctx: commands.Context, error):
await ctx.send("Invalid command used.")
@staticmethod
async def handle_missing_permissions(ctx: commands.Context, error):
await ctx.send("You do not have permission to use this command.")
@staticmethod
async def handle_bot_missing_permissions(ctx: commands.Context, error):
await ctx.send("I do not have permission to execute this command.")
@staticmethod
async def handle_other_errors(ctx: commands.Context, error):
logger.error(error)

View file

@ -1,3 +1,4 @@
# main.py
import os
import discord
@ -9,6 +10,9 @@ from utils._tux_logger import TuxLogger
logger = TuxLogger(__name__)
load_dotenv()
from discord.ext import commands
from error_handler import ErrorHandler
async def setup(bot: commands.Bot, debug: bool = False):
"""
@ -66,16 +70,14 @@ async def main():
error (Exception): The error that occurred.
""" # noqa E501
if isinstance(error, commands.CommandNotFound):
await ctx.send("Invalid command used.")
await ErrorHandler.handle_command_not_found(ctx, error)
elif isinstance(error, commands.MissingPermissions):
await ctx.send("You do not have permission to use this command.")
await ErrorHandler.handle_missing_permissions(ctx, error)
elif isinstance(error, commands.BotMissingPermissions):
await ctx.send("I do not have permission to execute this command.")
await ErrorHandler.handle_bot_missing_permissions(ctx, error)
else:
# For other errors you probably want to log them somewhere for review
logger.error(error)
await ErrorHandler.handle_other_errors(ctx, error)
@bot.event
async def on_command_completion(ctx: commands.Context):

View file

@ -1,3 +1,4 @@
# permissions.py
import configparser
import logging

View file

@ -1,3 +1,4 @@
# utils/_tux_loader.py
import logging
import os