diff --git a/Gwendolyn.py b/Gwendolyn.py index 5e72481..47e7977 100644 --- a/Gwendolyn.py +++ b/Gwendolyn.py @@ -29,9 +29,9 @@ class Gwendolyn(commands.Bot): intents = discord.Intents.default() intents.members = True - super().__init__(command_prefix=" ", case_insensitive=True, intents = intents) + super().__init__(command_prefix=" ", case_insensitive=True, intents = intents, status = discord.Status.dnd) - def log(self, messages, channel : str = "", level : int = 10): + def log(self, messages, channel : str = "", level : int = 20): logThis(messages, channel, level) diff --git a/cogs/EventCog.py b/cogs/EventCog.py index c220968..d732ccd 100644 --- a/cogs/EventCog.py +++ b/cogs/EventCog.py @@ -5,17 +5,22 @@ class EventCog(commands.Cog): def __init__(self, bot): self.bot = bot - # Sets the game and logs when the bot logs in + # Syncs commands, sets the game, and logs when the bot logs in @commands.Cog.listener() async def on_ready(self): - self.bot.log("Logged in as "+self.bot.user.name+", "+str(self.bot.user.id), level = 20) + await self.bot.databaseFuncs.syncCommands() + self.bot.log("Logged in as "+self.bot.user.name+", "+str(self.bot.user.id), level = 25) game = discord.Game("Use /help for commands") - await self.bot.change_presence(activity=game) + await self.bot.change_presence(activity=game, status = discord.Status.online) + + @commands.Cog.listener() + async def on_disconnect(self): + await self.bot.change_presence(status = discord.Status.offline) # Logs when user sends a command @commands.Cog.listener() async def on_slash_command(self, ctx): - self.bot.log(f"{ctx.author.display_name} ran /{ctx.name}", str(ctx.channel_id), level = 20) + self.bot.log(f"{ctx.author.display_name} ran /{ctx.name}", str(ctx.channel_id), level = 25) # Logs if a command experiences an error @commands.Cog.listener() @@ -36,5 +41,17 @@ class EventCog(commands.Cog): self.bot.log([f"exception in /{ctx.name}", f"{exceptionString}"],str(ctx.channel_id), 40) await ctx.send("Something went wrong (error code 000)") + # Logs if an error occurs + @commands.Cog.listener() + async def on_error(self, method): + exception = traceback.format_exc() + stopAt = "\nThe above exception was the direct cause of the following exception:\n\n" + if stopAt in exception: + index = exception.index(stopAt) + exception = exception[:index] + + exceptionString = "".join(exception) + self.bot.log([f"exception in /{method}", f"{exceptionString}"], level = 40) + def setup(bot): bot.add_cog(EventCog(bot)) diff --git a/utils/helperClasses.py b/utils/helperClasses.py index 74b4cc1..07f2f99 100644 --- a/utils/helperClasses.py +++ b/utils/helperClasses.py @@ -1,4 +1,4 @@ -import re, git, os, json +import re, git, os, json, time def sanitize(data : str, options : bool = False): data = data.splitlines() @@ -130,3 +130,16 @@ class databaseFuncs(): return True, False, data["imdbNames"] else: return False, None, None + + async def syncCommands(self): + collection = self.bot.database["last synced"] + lastSynced = collection.find_one() + now = time.time() + if lastSynced["last synced"] < now - 86400: + slashCommandList = str(await self.bot.slash.to_dict()) + self.bot.log(f"Updating commands: {slashCommandList}") + await self.bot.slash.sync_all_commands() + idNumber = lastSynced["_id"] + queryFilter = {"_id" : idNumber} + update = {"$set" : {"last synced" : now}} + collection.update_one(queryFilter, update) diff --git a/utils/utilFunctions.py b/utils/utilFunctions.py index 58a1406..aaee5bf 100644 --- a/utils/utilFunctions.py +++ b/utils/utilFunctions.py @@ -4,6 +4,7 @@ import logging import os from .helperClasses import Options +logging.addLevelName(25, "PRINT") logging.basicConfig(filename="gwendolyn.log", level=logging.INFO) def getParams(): @@ -33,7 +34,7 @@ def logThis(messages, channel : str = "", level : int = 20): if len(messages) > 1: messages[0] += " (details in log)" - if level != 10: + if level >= 25: print(messages[0]) for logMessage in messages: