diff --git a/cogs/help.py b/cogs/help.py index 8339851f..69819f52 100644 --- a/cogs/help.py +++ b/cogs/help.py @@ -18,18 +18,15 @@ # Help paginator by Rapptz # Edited by F4stZ4p # Edited by Hamothy + import asyncio import datetime -import io -import textwrap -import traceback -from contextlib import redirect_stdout from typing import Optional import discord from discord import Embed, DMChannel from discord.ext import commands -from discord.ext.commands import Cog, command, has_permissions, guild_only, is_owner, bot_has_permissions +from discord.ext.commands import Cog, command, has_permissions, guild_only, bot_has_permissions class CannotPaginate(Exception): @@ -622,36 +619,6 @@ def error_handling(self, author): return ErrorHandlingEmbed -def cleanup_code(content): - """Automatically removes code blocks from the code.""" - # remove ```py\n``` - if content.startswith('```') and content.endswith('```'): - return '\n'.join(content.split('\n')[1:-1]) - - # remove `foo` - return content.strip('` \n') - - -def paginate(text: str): - """Simple generator that paginates text.""" - last = 0 - pages = [] - for curr in range(0, len(text)): - if curr % 1980 == 0: - pages.append(text[last:curr]) - last = curr - appd_index = curr - if appd_index != len(text) - 1: - pages.append(text[last:curr]) - return list(filter(lambda a: a != '', pages)) - - -def get_syntax_error(e): - if e.text is None: - return f'```py\n{e.__class__.__name__}: {e}\n```' - return f'```py\n{e.text}{"^":>{e.offset}}\n{e.__class__.__name__}: {e}```' - - # Set up the Cog class Help(Cog): """Help Commands!""" @@ -681,27 +648,6 @@ class Help(Cog): except Exception as ex: await ctx.send(f"**{ex}**") - @command(name="forceprefix", hidden=True) - @guild_only() - @is_owner() - async def override_prefix(self, ctx, new: Optional[str] = None): - """Override the prefix in any given guild (Owner only)""" - - # As long as a new prefix has been given and is less than 5 characters - if new and len(new) <= 5: - # Store the new prefix in the dictionary and update the database - await self.bot.storage_prefix_for_guild(ctx, new) - - # Making sure that errors are handled if prefix is above 5 characters - elif new and len(new) > 5: - await self.bot.generate_embed(ctx, desc="The guild prefix must be less than or equal to **5** characters!") - - # if no prefix was provided - elif not new: - # Grab the current prefix for the guild within the cached dictionary - prefix = self.bot.get_prefix_for_guild(ctx.guild.id) - await self.bot.generate_embed(ctx, desc=f"**The current guild prefix is `{prefix}`**") - @command(name="prefix") @guild_only() @has_permissions(manage_guild=True) @@ -770,75 +716,6 @@ class Help(Cog): await ctx.send(embed=embed) - @command(name="eval", hidden=True) - @is_owner() - async def _eval(self, ctx, *, body): - """ - Evaluates python code - Gracefully yoinked from (https://github.com/fourjr/eval-bot)""" - - env = { - 'ctx': ctx, - 'bot': self.bot, - 'channel': ctx.channel, - 'author': ctx.author, - 'guild': ctx.guild, - 'message': ctx.message, - 'source': inspect.getsource - } - - env.update(globals()) - - body = cleanup_code(body) - stdout = io.StringIO() - err = out = None - - to_compile = f'async def func():\n{textwrap.indent(body, " ")}' - - try: - exec(to_compile, env) - except Exception as e: - err = await ctx.send(f'```py\n{e.__class__.__name__}: {e}\n```') - return await ctx.message.add_reaction('\u2049') - - func = env['func'] - try: - with redirect_stdout(stdout): - ret = await func() - except Exception as e: - value = stdout.getvalue() - err = await ctx.send(f'```py\n{value}{traceback.format_exc()}\n```') - else: - value = stdout.getvalue() - if ret is None: - if value: - try: - out = await ctx.send(f'```py\n{value}\n```') - except: - paginated_text = paginate(value) - for page in paginated_text: - if page == paginated_text[-1]: - out = await ctx.send(f'```py\n{page}\n```') - break - await ctx.send(f'```py\n{page}\n```') - else: - try: - out = await ctx.send(f'```py\n{value}{ret}\n```') - except: - paginated_text = paginate(f"{value}{ret}") - for page in paginated_text: - if page == paginated_text[-1]: - out = await ctx.send(f'```py\n{page}\n```') - break - await ctx.send(f'```py\n{page}\n```') - - if out: - await ctx.message.add_reaction('\u2705') # tick - elif err: - await ctx.message.add_reaction('\u2049') # x - else: - await ctx.message.add_reaction('\u2705') - @command(name="support") @bot_has_permissions(embed_links=True) async def support(self, ctx): diff --git a/cogs/owner.py b/cogs/owner.py index 2c095c37..f3a6cadf 100644 --- a/cogs/owner.py +++ b/cogs/owner.py @@ -13,7 +13,13 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . + import asyncio +import inspect +import io +import textwrap +import traceback +from contextlib import redirect_stdout from typing import Optional import asyncpg @@ -21,6 +27,36 @@ from discord import Member from discord.ext.commands import Cog, command, is_owner +def cleanup_code(content): + """Automatically removes code blocks from the code.""" + # remove ```py\n``` + if content.startswith('```') and content.endswith('```'): + return '\n'.join(content.split('\n')[1:-1]) + + # remove `foo` + return content.strip('` \n') + + +def paginate(text: str): + """Simple generator that paginates text.""" + last = 0 + pages = [] + for curr in range(0, len(text)): + if curr % 1980 == 0: + pages.append(text[last:curr]) + last = curr + appd_index = curr + if appd_index != len(text) - 1: + pages.append(text[last:curr]) + return list(filter(lambda a: a != '', pages)) + + +def get_syntax_error(e): + if e.text is None: + return f'```py\n{e.__class__.__name__}: {e}\n```' + return f'```py\n{e.text}{"^":>{e.offset}}\n{e.__class__.__name__}: {e}```' + + class Owner(Cog): """Commands for Ensō server""" @@ -45,6 +81,26 @@ class Owner(Cog): await self.bot.generate_embed(ctx, desc="**Leaving the guild... Bye Bye uvu**") await ctx.guild.leave() + @command(name="forceprefix", hidden=True) + @is_owner() + async def override_prefix(self, ctx, new: Optional[str] = None): + """Override the prefix in any given guild (Owner only)""" + + # As long as a new prefix has been given and is less than 5 characters + if new and len(new) <= 5: + # Store the new prefix in the dictionary and update the database + await self.bot.storage_prefix_for_guild(ctx, new) + + # Making sure that errors are handled if prefix is above 5 characters + elif new and len(new) > 5: + await self.bot.generate_embed(ctx, desc="The guild prefix must be less than or equal to **5** characters!") + + # if no prefix was provided + elif not new: + # Grab the current prefix for the guild within the cached dictionary + prefix = self.bot.get_prefix_for_guild(ctx.guild.id) + await self.bot.generate_embed(ctx, desc=f"**The current guild prefix is `{prefix}`**") + @command(name="restart", hidden=True) @is_owner() async def restart(self, ctx): @@ -115,6 +171,75 @@ class Owner(Cog): f"\nCurrent Queue Length: **{queue_len}**" f"\nMax Size Of Cache: **{max_cache_len}**") + @command(name="eval", hidden=True) + @is_owner() + async def _eval(self, ctx, *, body): + """ + Evaluates python code + Gracefully yoinked from (https://github.com/fourjr/eval-bot)""" + + env = { + 'ctx': ctx, + 'bot': self.bot, + 'channel': ctx.channel, + 'author': ctx.author, + 'guild': ctx.guild, + 'message': ctx.message, + 'source': inspect.getsource + } + + env.update(globals()) + + body = cleanup_code(body) + stdout = io.StringIO() + err = out = None + + to_compile = f'async def func():\n{textwrap.indent(body, " ")}' + + try: + exec(to_compile, env) + except Exception as e: + err = await ctx.send(f'```py\n{e.__class__.__name__}: {e}\n```') + return await ctx.message.add_reaction('\u2049') + + func = env['func'] + try: + with redirect_stdout(stdout): + ret = await func() + except Exception as e: + value = stdout.getvalue() + err = await ctx.send(f'```py\n{value}{traceback.format_exc()}\n```') + else: + value = stdout.getvalue() + if ret is None: + if value: + try: + out = await ctx.send(f'```py\n{value}\n```') + except: + paginated_text = paginate(value) + for page in paginated_text: + if page == paginated_text[-1]: + out = await ctx.send(f'```py\n{page}\n```') + break + await ctx.send(f'```py\n{page}\n```') + else: + try: + out = await ctx.send(f'```py\n{value}{ret}\n```') + except: + paginated_text = paginate(f"{value}{ret}") + for page in paginated_text: + if page == paginated_text[-1]: + out = await ctx.send(f'```py\n{page}\n```') + break + await ctx.send(f'```py\n{page}\n```') + + if out: + await ctx.message.add_reaction('\u2705') # tick + elif err: + await ctx.message.add_reaction('\u2049') # x + else: + await ctx.message.add_reaction('\u2705') + def setup(bot): bot.add_cog(Owner(bot))