bot.py (2461B)
1 # Import dependencies 2 import discord 3 from discord.ext import commands 4 import logging 5 6 7 # Import modules 8 from . import analyticsCog, coreCog, helpCog 9 from .botUtilities import addMessage, COLORS, CountdownNotFound, ContributorNotFound, CommandError, getPrefix 10 11 12 13 class CountdownBot(commands.Bot): 14 def __init__(self, db_connection, prefix): 15 # Set properties 16 self.db_connection = db_connection 17 self.prefix = prefix 18 self.logger = logging.getLogger(__name__) 19 20 # Get intents 21 intents = discord.Intents.default() 22 intents.message_content = True 23 24 # Initialize bot 25 super().__init__(command_prefix=lambda bot, ctx: getPrefix(self.db_connection, ctx, self.prefix), intents=intents) 26 27 28 29 async def setup_hook(self): 30 await self.add_cog(helpCog.Help(self)) 31 await self.add_cog(coreCog.Core(self, self.db_connection)) 32 await self.add_cog(analyticsCog.Analytics(self, self.db_connection)) 33 34 35 36 async def on_ready(self): 37 self.logger.info(f"Connected to Discord as {self.user} (ID {self.user.id})") 38 39 40 41 async def on_message(self, obj): 42 try: 43 # Make command prefixes, names, and arguments case insensitive 44 obj.content = obj.content.lower() 45 46 # Run commands 47 await self.process_commands(obj) 48 except: 49 pass 50 51 52 53 async def on_command_error(self, ctx, error): 54 # Rollback database transaction 55 self.db_connection.rollback() 56 57 # Send error embed 58 embed=discord.Embed(title=":warning: Error", description=str(error), color=COLORS["error"]) 59 if (isinstance(error, commands.CommandNotFound)): 60 embed.description = f"Command not found: `{str(error)[9:-14]}`" 61 elif (isinstance(error.original, CountdownNotFound)): 62 embed.description = f"Countdown not found" 63 elif (isinstance(error.original, ContributorNotFound)): 64 embed.description = f"Contributor not found: `{error.original.args[0]}`" 65 elif (isinstance(error.original, CommandError)): 66 embed.description = error.original.args[0] 67 else: 68 # Unanticipated error 69 embed.description = str(error) 70 logging.error(f"Error during command {ctx.message.content}", exc_info=error) 71 embed.description += f"\n\nUse `{(await self.get_prefix(ctx))[0]}help` to view help information" 72 await ctx.send(embed=embed)