From 9891c42ed913594fbde541bd430b0c2a63673144 Mon Sep 17 00:00:00 2001 From: annihilator708 Date: Sun, 20 May 2018 21:41:06 +0200 Subject: [PATCH] Add cog reload|unload|load commands --- run.py | 4 +-- src/cogs/upload.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 src/cogs/upload.py diff --git a/run.py b/run.py index f0e1797..5a30e26 100644 --- a/run.py +++ b/run.py @@ -20,9 +20,10 @@ class SebiMachine(commands.Bot, LoadConfig): # Load plugins # Add your cog file name in this list - cogs = ['example'] + cogs = ['example', 'upload'] for cog in cogs: + print(cog) self.load_extension(f'src.cogs.{cog}') async def on_ready(self): @@ -30,7 +31,6 @@ class SebiMachine(commands.Bot, LoadConfig): if self.maintenance: print('MAINTENANCE ACTIVE') - if __name__ == '__main__': client = SebiMachine() # Make sure the key stays private. diff --git a/src/cogs/upload.py b/src/cogs/upload.py new file mode 100644 index 0000000..7e77075 --- /dev/null +++ b/src/cogs/upload.py @@ -0,0 +1,82 @@ +#!/usr/bin/python +# -*- coding: -*- + +from discord.ext import commands +import discord +import traceback + +class Upload: + """ + CogName should be the name of the cog + """ + def __init__(self, bot): + self.bot = bot + print('upload loaded') + + @commands.command() + async def reload(self, ctx, *, extension: str): + """Reload an extension.""" + + if ctx.author.id not in self.bot.ownerlist: + return await ctx.send('Only my creator can use me like this :blush:', delete_after=1) + + extension = extension.lower() + try: + self.bot.unload_extension("src.cogs.{}".format(extension)) + self.bot.load_extension("src.cogs.{}".format(extension)) + except Exception as e: + traceback.print_exc() + await ctx.send(f'Could not reload `{extension}` -> `{e}`') + else: + await ctx.send(f'Reloaded `{extension}`.') + + @commands.command() + async def reloadall(self, ctx): + """Reload all extensions.""" + if ctx.author.id not in self.bot.ownerlist: + return await ctx.send('Only my creator can use me like this :blush:', delete_after=1) + + try: + for extension in self.bot.extensions: + self.bot.unload_extension(extension) + self.bot.load_extension(extension) + await ctx.send(f"Reload success! :thumbsup:\n") + except Exception as e: + await ctx.send(f"Could not reload `{extension}` -> `{e}`.\n") + + @commands.command() + async def unload(self, ctx, *, extension: str): + """Unload an extension.""" + if ctx.author.id not in self.bot.ownerlist: + return await ctx.send('Only my creator can use me like this :blush:', delete_after=1) + + extension = extension.lower() + try: + self.bot.unload_extension("src.cogs.{}".format(extension)) + + except Exception as e: + traceback.print_exc() + if ctx.message.author.id not in self.bot.owner_list: + await ctx.send(f'Could not unload `{extension}` -> `{e}`') + + else: + await ctx.send(f'Unloaded `{extension}`.') + + @commands.command() + async def load(self, ctx, *, extension: str): + """Load an extension.""" + if ctx.author.id not in self.bot.ownerlist: + return await ctx.send('Only my creator can use me like this :blush:', delete_after=1) + + extension = extension.lower() + try: + self.bot.load_extension("src.cogs.{}".format(extension)) + + except Exception as e: + traceback.print_exc() + await ctx.send(f'Could not unload `{extension}` -> `{e}`') + else: + await ctx.send(f'Loaded `{extension}`.') + +def setup(bot): + bot.add_cog(Upload(bot)) \ No newline at end of file