Merge pull request #98 from classabbyamp/5c-checks-cmn

Moved checks and helpers to common.py
This commit is contained in:
0x5c 2019-12-08 04:16:39 -05:00 committed by GitHub
commit ea44f223c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 27 deletions

View File

@ -21,7 +21,10 @@ import discord
import discord.ext.commands as commands import discord.ext.commands as commands
__all__ = ["colours", "cat", "emojis", "error_embed_factory"] import data.options as opt
__all__ = ["colours", "cat", "emojis", "error_embed_factory", "add_react", "check_if_owner"]
# --- Common values --- # --- Common values ---
@ -56,3 +59,19 @@ def error_embed_factory(ctx: commands.Context, exception: Exception, debug_mode:
icon_url=str(ctx.author.avatar_url)) icon_url=str(ctx.author.avatar_url))
embed.description = "```\n" + '\n'.join(fmtd_ex) + "```" embed.description = "```\n" + '\n'.join(fmtd_ex) + "```"
return embed return embed
async def add_react(msg: discord.Message, react: str):
try:
await msg.add_reaction(react)
except discord.Forbidden:
print(f"[!!] Missing permissions to add reaction in '{msg.guild.id}/{msg.channel.id}'!")
# --- Checks ---
async def check_if_owner(ctx: commands.Context):
if ctx.author.id in opt.owners_uids:
return True
await add_react(ctx.message, emojis.bad)
return False

34
main.py
View File

@ -37,48 +37,30 @@ bot = commands.Bot(command_prefix=opt.prefix,
help_command=commands.MinimalHelpCommand()) help_command=commands.MinimalHelpCommand())
# --- Helper functions ---
async def add_react(msg: discord.Message, react: str):
try:
await msg.add_reaction(react)
except discord.Forbidden:
print(f"[!!] Missing permissions to add reaction in '{msg.guild.id}/{msg.channel.id}'!")
# --- Checks ---
async def check_if_owner(ctx: commands.Context):
if ctx.author.id in opt.owners_uids:
return True
await add_react(ctx.message, cmn.emojis.bad)
return False
# --- Commands --- # --- Commands ---
@bot.command(name="restart", hidden=True) @bot.command(name="restart", hidden=True)
@commands.check(check_if_owner) @commands.check(cmn.check_if_owner)
async def _restart_bot(ctx: commands.Context): async def _restart_bot(ctx: commands.Context):
"""Restarts the bot.""" """Restarts the bot."""
global exit_code global exit_code
await add_react(ctx.message, cmn.emojis.good) await cmn.add_react(ctx.message, cmn.emojis.good)
exit_code = 42 # Signals to the wrapper script that the bot needs to be restarted. exit_code = 42 # Signals to the wrapper script that the bot needs to be restarted.
await bot.logout() await bot.logout()
@bot.command(name="shutdown", hidden=True) @bot.command(name="shutdown", hidden=True)
@commands.check(check_if_owner) @commands.check(cmn.check_if_owner)
async def _shutdown_bot(ctx: commands.Context): async def _shutdown_bot(ctx: commands.Context):
"""Shuts down the bot.""" """Shuts down the bot."""
global exit_code global exit_code
await add_react(ctx.message, cmn.emojis.good) await cmn.add_react(ctx.message, cmn.emojis.good)
exit_code = 0 # Signals to the wrapper script that the bot should not be restarted. exit_code = 0 # Signals to the wrapper script that the bot should not be restarted.
await bot.logout() await bot.logout()
@bot.group(name="extctl", hidden=True) @bot.group(name="extctl", hidden=True)
@commands.check(check_if_owner) @commands.check(cmn.check_if_owner)
async def _extctl(ctx: commands.Context): async def _extctl(ctx: commands.Context):
"""Extension control commands. """Extension control commands.
Defaults to `list` if no subcommand specified""" Defaults to `list` if no subcommand specified"""
@ -101,7 +83,7 @@ async def _extctl_list(ctx: commands.Context):
async def _extctl_load(ctx: commands.Context, extension: str): async def _extctl_load(ctx: commands.Context, extension: str):
try: try:
bot.load_extension(ext_dir + "." + extension) bot.load_extension(ext_dir + "." + extension)
await add_react(ctx.message, cmn.emojis.good) await cmn.add_react(ctx.message, cmn.emojis.good)
except commands.ExtensionError as ex: except commands.ExtensionError as ex:
embed = cmn.error_embed_factory(ctx, ex, debug_mode) embed = cmn.error_embed_factory(ctx, ex, debug_mode)
await ctx.send(embed=embed) await ctx.send(embed=embed)
@ -111,7 +93,7 @@ async def _extctl_load(ctx: commands.Context, extension: str):
async def _extctl_reload(ctx: commands.Context, extension: str): async def _extctl_reload(ctx: commands.Context, extension: str):
try: try:
bot.reload_extension(ext_dir + "." + extension) bot.reload_extension(ext_dir + "." + extension)
await add_react(ctx.message, cmn.emojis.good) await cmn.add_react(ctx.message, cmn.emojis.good)
except commands.ExtensionError as ex: except commands.ExtensionError as ex:
embed = cmn.error_embed_factory(ctx, ex, debug_mode) embed = cmn.error_embed_factory(ctx, ex, debug_mode)
await ctx.send(embed=embed) await ctx.send(embed=embed)
@ -121,7 +103,7 @@ async def _extctl_reload(ctx: commands.Context, extension: str):
async def _extctl_unload(ctx: commands.Context, extension: str): async def _extctl_unload(ctx: commands.Context, extension: str):
try: try:
bot.unload_extension(ext_dir + "." + extension) bot.unload_extension(ext_dir + "." + extension)
await add_react(ctx.message, cmn.emojis.good) await cmn.add_react(ctx.message, cmn.emojis.good)
except commands.ExtensionError as ex: except commands.ExtensionError as ex:
embed = cmn.error_embed_factory(ctx, ex, debug_mode) embed = cmn.error_embed_factory(ctx, ex, debug_mode)
await ctx.send(embed=embed) await ctx.send(embed=embed)