From 91f96ce246730dd669d459e9ecb9e354e653afd6 Mon Sep 17 00:00:00 2001 From: bibin Date: Sun, 27 Dec 2020 20:58:18 +0100 Subject: [PATCH] Initial: Add load commands and developer cog The commands `load`, `unload`, `reload` were added in the main `bot.py` file. With these one can dynamically load extensions like discord cogs. The cog `developer` has some test commands and listeners. It also has a command to shut down the bot (and therefore the python program). --- src/bot.py | 22 ++++++++++++++++++++++ src/cogs/__init__.py | 0 src/cogs/developer.py | 27 +++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 src/cogs/__init__.py create mode 100644 src/cogs/developer.py diff --git a/src/bot.py b/src/bot.py index dd34702..60c1a4d 100644 --- a/src/bot.py +++ b/src/bot.py @@ -24,5 +24,27 @@ if TOKEN is None: bot = commands.Bot(command_prefix=commands.when_mentioned_or('$b ')) +default_extensions = ['developer'] + +for extension in default_extensions: + bot.load_extension(f'cogs.{extension}') + +@bot.command() +@commands.is_owner() +async def load(ctx, extension): + bot.load_extension(f'cogs.{extension}') + print(f'cogs.{extension} loaded') + +@bot.command() +@commands.is_owner() +async def unload(ctx, extension): + bot.unload_extension(f'cogs.{extension}') + print(f'cogs.{extension} unloaded') + +@bot.command() +@commands.is_owner() +async def reload(ctx, extension): + bot.reload_extension(f'cogs.{extension}') + print(f'cogs.{extension} reloaded') bot.run(TOKEN) diff --git a/src/cogs/__init__.py b/src/cogs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/cogs/developer.py b/src/cogs/developer.py new file mode 100644 index 0000000..65632c4 --- /dev/null +++ b/src/cogs/developer.py @@ -0,0 +1,27 @@ +import discord +from discord.ext import commands + + +class Developer(commands.Cog): + """This class is intended only for the developer, mainly for testing purposes""" + + def __init__(self, bot): + self.bot = bot + + @commands.Cog.listener() + async def on_ready(self): + await self.bot.change_presence(status=discord.Status.online, activity=discord.Game('$b help')) + print('We have logged in as {0.user}'.format(self.bot)) + + @commands.command() + async def hello(self, ctx): + await ctx.send(f"Hello {ctx.message.author.name} :wave:") + + @commands.command() + @commands.is_owner() + async def logout(self, ctx): + """Shut down the bot""" + await self.bot.logout() + +def setup(bot): + bot.add_cog(Developer(bot))