1
0
Fork 0

Add extension management, start common check function and error handler

This commit is contained in:
Ethanell 2020-04-08 17:36:42 +02:00
parent 8143d59c76
commit ce89d8c609
5 changed files with 107 additions and 3 deletions

10
bot_bde/check.py Normal file
View file

@ -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")

View file

@ -1 +1 @@
{"prefix": "!", "token": "GOOD_BOT_TOKEN", "admin_id": "GOOD_USER_ID"}
{"prefix": "!", "token": "GOOD_BOT_TOKEN", "admin_id": 1234567890}

View file

@ -2,3 +2,4 @@ from bot_bde import bot
bot.load_extension("extensions.help")
bot.load_extension("extensions.speak")
bot.load_extension("extensions.extension")

88
extensions/extension.py Normal file
View file

@ -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")

View file

@ -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):