From ce89d8c6097ed5f12533f46b774b20ed6e8bca67 Mon Sep 17 00:00:00 2001 From: flifloo Date: Wed, 8 Apr 2020 17:36:42 +0200 Subject: [PATCH] Add extension management, start common check function and error handler --- bot_bde/check.py | 10 +++++ config_exemple.json | 2 +- extensions/__init__.py | 1 + extensions/extension.py | 88 +++++++++++++++++++++++++++++++++++++++++ extensions/speak.py | 9 ++++- 5 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 bot_bde/check.py create mode 100644 extensions/extension.py diff --git a/bot_bde/check.py b/bot_bde/check.py new file mode 100644 index 0000000..9aa9439 --- /dev/null +++ b/bot_bde/check.py @@ -0,0 +1,10 @@ +from discord.ext import commands +from bot_bde import config + + +class NotOwner(commands.CheckFailure): + pass + + +async def is_owner(ctx: commands.Context): + return ctx.author.id == config.get("admin_id") diff --git a/config_exemple.json b/config_exemple.json index b49a4a0..9d20d7a 100644 --- a/config_exemple.json +++ b/config_exemple.json @@ -1 +1 @@ -{"prefix": "!", "token": "GOOD_BOT_TOKEN", "admin_id": "GOOD_USER_ID"} \ No newline at end of file +{"prefix": "!", "token": "GOOD_BOT_TOKEN", "admin_id": 1234567890} \ No newline at end of file diff --git a/extensions/__init__.py b/extensions/__init__.py index 61f74b9..b8eff8f 100644 --- a/extensions/__init__.py +++ b/extensions/__init__.py @@ -2,3 +2,4 @@ from bot_bde import bot bot.load_extension("extensions.help") bot.load_extension("extensions.speak") +bot.load_extension("extensions.extension") diff --git a/extensions/extension.py b/extensions/extension.py new file mode 100644 index 0000000..10da37c --- /dev/null +++ b/extensions/extension.py @@ -0,0 +1,88 @@ +from discord.ext import commands +from discord.ext.commands.errors import CommandNotFound +from discord import Embed +from bot_bde.check import is_owner, NotOwner +from bot_bde.logger import logger + + +extension_name = "extension" +logger = logger.getChild(extension_name) + + +class Extension(commands.Cog): + def __init__(self, bot: commands.Bot): + self.bot = bot + self.strict = False + self.voice_chan = None + self.waiting = [] + self.lastSpeaker = None + + @commands.group("extension", pass_context=True) + @commands.check(is_owner) + async def extension(self, ctx: commands.Context): + if ctx.invoked_subcommand is None: + embed = Embed(title="Extensions") + for extension in self.bot.extensions: + embed.add_field(name=extension, value="Loaded", inline=False) + await ctx.send(embed=embed) + + @extension.group("load", pass_context=True) + @commands.check(is_owner) + async def extension_load(self, ctx: commands.Context, name: str): + try: + self.bot.load_extension(name) + except Extension as e: + await ctx.message.add_reaction("\u26a0") + else: + await ctx.message.add_reaction("\U0001f44d") + + @extension.group("unload", pass_context=True) + @commands.check(is_owner) + async def extension_unload(self, ctx: commands.Context, name: str): + try: + self.bot.unload_extension(name) + except Extension as e: + await ctx.message.add_reaction("\u26a0") + else: + await ctx.message.add_reaction("\U0001f44d") + + @extension.group("reload", pass_context=True) + @commands.check(is_owner) + async def extension_reload(self, ctx: commands.Context, name: str): + try: + self.bot.unload_extension(name) + self.bot.load_extension(name) + except Extension as e: + await ctx.message.add_reaction("\u26a0") + else: + await ctx.message.add_reaction("\U0001f44d") + + @commands.Cog.listener() + async def on_command_error(self, ctx: commands.Context, error): + if isinstance(error, NotOwner): + await ctx.message.add_reaction("\u274C") + elif isinstance(error, CommandNotFound): + await ctx.message.add_reaction("\u2753") + else: + await ctx.send("An error occurred !") + raise error + + +def setup(bot): + logger.info(f"Loading...") + try: + bot.add_cog(Extension(bot)) + except Exception as e: + logger.error(f"Error loading: {e}") + else: + logger.info(f"Load successful") + + +def teardown(bot): + logger.info(f"Unloading...") + try: + bot.remove_cog("Extension") + except Exception as e: + logger.error(f"Error unloading: {e}") + else: + logger.info(f"Unload successful") diff --git a/extensions/speak.py b/extensions/speak.py index bac23ec..5e1e332 100644 --- a/extensions/speak.py +++ b/extensions/speak.py @@ -1,5 +1,7 @@ from discord.ext import commands from discord import Member, VoiceState, Embed +from discord.ext.commands import CommandNotFound + from bot_bde.logger import logger @@ -134,8 +136,11 @@ class Speak(commands.Cog): @commands.Cog.listener() async def on_command_error(self, ctx: commands.Context, error): - await ctx.send("An error occurred !") - raise error + if isinstance(error, CommandNotFound): + await ctx.message.add_reaction("\u2753") + else: + await ctx.send("An error occurred !") + raise error def setup(bot):