Skip to content

Commit

Permalink
Setting up the base of the testing bot with cog loading utility
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastiaanZ committed Jan 5, 2019
1 parent aa534b1 commit 1c77c31
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
13 changes: 11 additions & 2 deletions bot/__main__.py
@@ -1,7 +1,16 @@
from discord.ext.commands import Bot
from os import getenv

from discord.ext.commands import Bot


BOT_TOKEN = getenv("BOT_TOKEN")

print(BOT_TOKEN)
bot = Bot(
command_prefix="@",
case_insensitive=True,
max_messages=10_000
)

bot.load_extension("bot.cogs.cogs")

bot.run(BOT_TOKEN)
Empty file added bot/cogs/__init__.py
Empty file.
80 changes: 80 additions & 0 deletions bot/cogs/cogs.py
@@ -0,0 +1,80 @@
from discord.ext.commands import Bot, Context, group


class Cogs:
"""Cog management cog to load, unload, reload the testing cogs"""

def __init__(self, bot: Bot):
self.bot = bot

@group(name='cogs', invoke_without_command=True)
async def cogs_group(self, ctx: Context):
await ctx.send(f"No cog action specified!")

@cogs_group.command(name='load')
async def load_command(self, ctx: Context, cog: str = ""):
cog = cog.lower()

if cog:
full_cog = f"bot.cogs.{cog}"

if full_cog not in self.bot.extensions:
try:
self.bot.load_extension(full_cog)
except ImportError:
await ctx.send(f"ImportError while loading {cog}")
except Exception as e:
await ctx.send(f"{e} while loading {cog}")
else:
await ctx.send(f"Loaded {cog}!")
else:
await ctx.send(f"The cog {cog} was already loaded... Try reloading it!")
else:
await ctx.send(f"You didn't specify a cog to load!")

@cogs_group.command(name='unload')
async def unload_command(self, ctx: Context, cog: str = ""):
cog = cog.lower()

if cog:
full_cog = f"bot.cogs.{cog}"

if full_cog == "bot.cogs.cogs":
await ctx.send(f"Can't unload the cogs cog!")
elif full_cog in self.bot.extensions:
try:
self.bot.unload_extension(full_cog)
except Exception as e:
await ctx.send(f"{e} while unloading {cog}!")
else:
await ctx.send(f"Unloaded {cog}!")
else:
await ctx.send(f"Can't unloaded {cog}, as it wasn't loaded!")
else:
await ctx.send(f"You didn't specify a cog to unload!")

@cogs_group.command(name='reload')
async def reload_command(self, ctx: Context, cog: str = ""):
cog = cog.lower()

if cog:
full_cog = f"bot.cogs.{cog}"

if full_cog == "bot.cogs.cogs":
await ctx.send(f"Can't reload the cogs cog!")
elif full_cog in self.bot.extensions:
try:
self.bot.unload_extension(full_cog)
self.bot.load_extension(full_cog)
except Exception as e:
await ctx.send(f"{e} while reloading {cog}!")
else:
await ctx.send(f"Reloaded {cog}!")
else:
await ctx.send(f"Can't reloaded {cog}, as it wasn't loaded!")
else:
await ctx.send(f"You didn't specify a cog to reload!")


def setup(bot):
bot.add_cog(Cogs(bot))
1 change: 1 addition & 0 deletions bot/constants.py
@@ -0,0 +1 @@
DEV_TEST = 530364497509220363

0 comments on commit 1c77c31

Please sign in to comment.