From 38829722d5b38af132cedab7b0fcf43c41510efc Mon Sep 17 00:00:00 2001 From: Adriene Hutchins Date: Mon, 2 Mar 2020 02:01:28 -0500 Subject: [PATCH] More Documentation --- extensions/botlist.py | 42 ++++++++++++++++++++------- main.py | 66 ++++++++++++++++++++++++++----------------- 2 files changed, 72 insertions(+), 36 deletions(-) diff --git a/extensions/botlist.py b/extensions/botlist.py index d658658..3b2f5e8 100644 --- a/extensions/botlist.py +++ b/extensions/botlist.py @@ -16,13 +16,16 @@ class BotList(commands.Cog, name='Bot List'): """Provides various utilities for handling BotList stuff.""" def __init__(self, bot): + # Main Stuff self.bot = bot self.request = bot.request - # Tokens + + # List Tokens self.dbl_token = bot.config['DBL'] self.dbots_token = bot.config['DBOTS'] self.bod_token = bot.config['BOD'] self.dblcom_token = bot.config['DBLCOM'] + # top.gg clent self.dbl_client = dbl.DBLClient( self.bot, self.dbots_token) @@ -36,13 +39,18 @@ class BotList(commands.Cog, name='Bot List'): dblcom_call = "https://discordbotlist.com/api" responses = {} + # Calls # bots.discord.gg if self.dbots_token != '': + + # Call Prereqs dbots_call += f"/bots/{self.bot.user.id}/stats" dbots_data = {'guildCount': len(self.bot.guilds)} dbots_headers = {'Authorization': self.dbots_token} - async with self.request.post(dbots_call, - json=dbots_data, + + # Call Handling + async with self.request.post(dbots_call, + json=dbots_data, headers=dbots_headers) as resp: resp_json = await resp.json() print(resp_json) @@ -50,10 +58,14 @@ class BotList(commands.Cog, name='Bot List'): # bots.ondiscord.xyz if self.bod_token != '': + + # Call Prereqs bod_call += f"/bots/{self.bot.user.id}/guilds" bod_data = {'guildCount': len(self.bot.guilds)} bod_headers = {'Authorization': self.bod_token} - async with self.request.post(bod_call, + + # Call Handling + async with self.request.post(bod_call, json=bod_data, headers=bod_headers) as resp: resp_json = await resp.json() @@ -62,10 +74,14 @@ class BotList(commands.Cog, name='Bot List'): # discordbotlist.com if self.dblcom_token != '': + + # Call Prereqs dblcom_call += f"/bots/{self.bot.user.id}/stats" dblcom_data = {'guilds': len(self.bot.guilds)} dblcom_headers = {'Authorization': self.dblcom_token} - async with self.request.post(dblcom_call, + + # Call Handling + async with self.request.post(dblcom_call, json=dblcom_data, headers=dblcom_headers) as resp: resp_json = await resp.json() @@ -74,13 +90,14 @@ class BotList(commands.Cog, name='Bot List'): # top.gg if self.dbl_token != '': + # NOTE top.gg has a lib so it gets different handling. try: resp = await self.dbl_client.post_guild_count() responses['dbl'] = resp except Exception as e: responses['dbl'] = e - # Finishing up + # Finalization return responses # TODO Move to Core, hide behind check for any existing token @@ -88,11 +105,14 @@ class BotList(commands.Cog, name='Bot List'): async def vote(self, ctx): """Review and vote for this bot on various botlists.""" + # Header msg = ( "**Thank you for wanting to help us out!**\n" "You can find us on the following lists:\n\n" ) - + + # Links + # XXX This is really stupidly done, I'm going to find a way to redo it. if self.dbots_token != '': msg += f"_bots.discord.gg_ \n" if self.bod_token != '': @@ -102,6 +122,7 @@ class BotList(commands.Cog, name='Bot List'): if self.dbl_token != '': msg += f"_top.gg_ \n" + # Sending await ctx.send(msg) @commands.command() @@ -111,16 +132,17 @@ class BotList(commands.Cog, name='Bot List'): msg = await ctx.send(" **Updating...**") responses = await self._update_logic() - print(responses) # TODO Look at responses and figure out error handling + print(responses) # TODO Look at responses and figure out error handling await msg.edit(content="**Updated!**") @tasks.loop(minutes=15.0) async def update_stats(self): """Automatically updates statistics every 15 minutes.""" - + responses = await self._update_logic() - print(responses) + print(responses) # TODO See other todo def setup(bot): + """Adds the cog to the bot.""" bot.add_cog(BotList(bot)) diff --git a/main.py b/main.py index b7edef9..8774643 100644 --- a/main.py +++ b/main.py @@ -23,33 +23,39 @@ class Bot(commands.Bot): def __init__(self, **options): """Initializes the main parts of the bot.""" - super().__init__(self._get_prefix_new, **options) + # Logging print('Performing initialization...\n') + # Initializes parent class + super().__init__(self._get_prefix_new, **options) + # Get Config Values with open('config.json') as f: self.config = json.load(f) - self.prefix = self.config.get('PREFIX') - self.version = self.config.get('VERSION') - self.maintenance = self.config.get('MAINTENANCE') - self.description = self.config.get('DESCRIPTION') + self.prefix = self.config['PREFIX'] + self.version = self.config['VERSION'] + self.maintenance = self.config['MAINTENANCE'] + self.description = self.config['DESCRIPTION'] + self.case_insensitive = self.config['CASE_INSENSITIVE'] + # Get Instances with open('searxes.txt') as f: self.instances = f.read().split('\n') + # Logging print('Initialization complete.\n\n') async def _get_prefix_new(self, bot, msg): - """Full flexible check for prefix.""" + """More flexible check for prefix.""" + # Adds empty prefix if in DMs if isinstance(msg.channel, discord.DMChannel) and self.config['PREFIXLESS_DMS']: - # Adds empty prefix if in DMs - plus_none = self.prefix.copy() - plus_none.append('') - return commands.when_mentioned_or(*plus_none)(bot, msg) + plus_empty = self.prefix.copy() + plus_empty.append('') + return commands.when_mentioned_or(*plus_empty)(bot, msg) + # Keeps regular if not else: - # Keeps regular if not return commands.when_mentioned_or(*self.prefix)(bot, msg) async def on_ready(self): @@ -61,7 +67,8 @@ class Bot(commands.Bot): if self.description == '': self.description = self.appinfo.description - # EXTENSION ENTRY POINT + # NOTE Extension Entry Point + # Loads core, which loads all other extensions self.load_extension('extensions.core') # Logging @@ -73,48 +80,52 @@ class Bot(commands.Bot): print(msg) async def on_message(self, message): + """Handles what the bot does whenever a message comes across.""" # Prerequisites mentions = [self.user.mention, f'<@!{self.user.id}>'] ctx = await self.get_context(message) # Handling + # Turn away bots if message.author.bot: - # Turn away bots return + + # Ignore blocked users elif message.author.id in self.config.get('BLOCKED'): - # Ignore blocked users return + + # Maintenance mode elif self.maintenance and not message.author.is_owner(): - # Maintenance mode return + + # Empty ping for assistance elif message.content in mentions and self.config.get('MENTION_ASSIST'): - # Empty ping for assistance assist_msg = ( "**Hi there! How can I help?**\n\n" # Two New Lines Here f"You may use **{self.user.mention} `term here`** to search, " f"or **{self.user.mention} `help`** for assistance.") await ctx.send(assist_msg) + + # Move on to command handling else: - # Move on to command handling await self.process_commands(message) - -bot = Bot( - case_insensitive=True) - +# Creates Bot object +bot = Bot() @bot.listen() async def on_command_error(ctx, error): + """Handles all errors stemming from ext.commands.""" + + # Lets other cogs handle CommandNotFound. + # Change this if you want command not found handling if isinstance(error, commands.CommandNotFound): - # Lets other cogs handle this. - # Change this if you want command not found handling. return + # Provides a very pretty embed if something's actually a dev's fault. elif isinstance(error, commands.CommandInvokeError): - # Provides a very pretty embed if something's actually a dev's fault. - # Prerequisites error = error.original _traceback = traceback.format_tb(error.__traceback__) @@ -149,10 +160,13 @@ async def on_command_error(ctx, error): if len(trace_content) > 1024 else trace_content) + # Sending await ctx.send(embed_fallback, embed=error_embed) + # If anything else goes wrong, just go ahead and send it in chat. else: - # If anything else goes wrong, just go ahead and send it in chat. await ctx.send(error) +# NOTE Bot Entry Point +# Starts the bot bot.run(bot.config['TOKEN'])