diff options
| -rw-r--r-- | bot/cogs/snekbox.py | 47 | ||||
| -rw-r--r-- | config-default.yml | 1 |
2 files changed, 46 insertions, 2 deletions
diff --git a/bot/cogs/snekbox.py b/bot/cogs/snekbox.py index aa467b2bb..69a2ed59e 100644 --- a/bot/cogs/snekbox.py +++ b/bot/cogs/snekbox.py @@ -1,11 +1,16 @@ import datetime import logging +import random import re -from discord.ext.commands import Bot, Context, command +from discord import Colour, Embed +from discord.ext.commands import ( + Bot, CommandError, Context, MissingPermissions, + NoPrivateMessage, check, command, guild_only +) from bot.cogs.rmq import RMQ -from bot.constants import URLs +from bot.constants import Channels, ERROR_REPLIES, NEGATIVE_REPLIES, Roles, URLs log = logging.getLogger(__name__) @@ -26,6 +31,21 @@ except Exception as e: """ ESCAPE_REGEX = re.compile("[`\u202E\u200B]{3,}") +BYPASS_ROLES = (Roles.owner, Roles.admin, Roles.moderator, Roles.helpers) +WHITELISTED_CHANNELS = (Channels.bot,) +WHITELISTED_CHANNELS_STRING = ', '.join(f"<#{channel_id}>" for channel_id in WHITELISTED_CHANNELS) + + +async def channel_is_whitelisted_or_author_can_bypass(ctx: Context): + """ + Checks that the author is either helper or above + or the channel is a whitelisted channel. + """ + + if ctx.channel.id not in WHITELISTED_CHANNELS and ctx.author.top_role.id not in BYPASS_ROLES: + raise MissingPermissions("You are not allowed to do that here.") + + return True class Snekbox: @@ -44,6 +64,8 @@ class Snekbox: return self.bot.get_cog("RMQ") @command(name="snekbox.eval()", aliases=["snekbox.eval", "eval()", "eval"]) + @guild_only() + @check(channel_is_whitelisted_or_author_can_bypass) async def do_eval(self, ctx: Context, code: str): """ Run some code. get the result back. We've done our best to make this safe, but do let us know if you @@ -137,6 +159,27 @@ class Snekbox: del self.jobs[ctx.author.id] raise + @do_eval.error + async def eval_command_error(self, ctx: Context, error: CommandError): + embed = Embed(colour=Colour.red()) + + if isinstance(error, NoPrivateMessage): + embed.title = random.choice(NEGATIVE_REPLIES) + embed.description = "You're not allowed to use this command in private messages." + await ctx.send(embed=embed) + + elif isinstance(error, MissingPermissions): + embed.title = random.choice(NEGATIVE_REPLIES) + embed.description = f"Sorry, but you may only use this command within {WHITELISTED_CHANNELS_STRING}." + await ctx.send(embed=embed) + + else: + original_error = getattr(error, 'original', "no original error") + log.error(f"Unhandled error in snekbox eval: {error} ({original_error})") + embed.title = random.choice(ERROR_REPLIES) + embed.description = "Some unhandled error occurred. Sorry for that!" + await ctx.send(embed=embed) + def setup(bot): bot.add_cog(Snekbox(bot)) diff --git a/config-default.yml b/config-default.yml index 5b87f8d78..04ed53f7a 100644 --- a/config-default.yml +++ b/config-default.yml @@ -44,6 +44,7 @@ guild: moderator: 267629731250176001 owner: 267627879762755584 verified: 352427296948486144 + helpers: 267630620367257601 keys: |