diff --git a/jarvis/__init__.py b/jarvis/__init__.py index 41e0190..567b959 100644 --- a/jarvis/__init__.py +++ b/jarvis/__init__.py @@ -9,12 +9,13 @@ from discord.ext import commands from discord.ext.tasks import loop from discord.utils import find, get from discord_slash import SlashCommand +from psutil import Process + from jarvis import logo, utils from jarvis.config import get_config from jarvis.utils import build_embed from jarvis.utils.db import DBManager from jarvis.utils.field import Field -from psutil import Process if asyncio.get_event_loop().is_closed(): asyncio.set_event_loop(asyncio.new_event_loop()) @@ -31,7 +32,7 @@ invites = re.compile( jarvis = commands.Bot(command_prefix=utils.get_prefix, intents=intents) slash = SlashCommand(jarvis, sync_commands=True, sync_on_cog_reload=True) jarvis_self = Process() -__version__ = "0.9.9" +__version__ = "1.0.0" db = DBManager(get_config().mongo).mongo diff --git a/jarvis/cogs/dev.py b/jarvis/cogs/dev.py index a8ebbb7..83e51a9 100644 --- a/jarvis/cogs/dev.py +++ b/jarvis/cogs/dev.py @@ -1,13 +1,8 @@ import base64 import hashlib -import os import re import subprocess -import sys -import traceback import uuid -from inspect import getsource -from time import time import discord import ulid @@ -18,7 +13,6 @@ from discord_slash import cog_ext import jarvis from jarvis.utils import build_embed, convert_bytesize from jarvis.utils.field import Field -from jarvis.utils.permissions import user_is_bot_admin supported_hashes = { x for x in hashlib.algorithms_guaranteed if "shake" not in x @@ -298,67 +292,6 @@ class DevCog(commands.Cog): await ctx.defer() await self._cloc(ctx) - def resolve_variable(self, variable): - if hasattr(variable, "__iter__"): - var_length = len(list(variable)) - if (var_length > 100) and (not isinstance(variable, str)): - return f"" - elif not var_length: - return f"" - - if (not variable) and (not isinstance(variable, bool)): - return f"" - return ( - variable - if (len(f"{variable}") <= 1000) - else f"" - ) - - def prepare(self, string): - arr = ( - string.strip("```") - .replace("py\n", "") - .replace("python\n", "") - .split("\n") - ) - if not arr[::-1][0].replace(" ", "").startswith("return"): - arr[len(arr) - 1] = "return " + arr[::-1][0] - return "".join(f"\n\t{i}" for i in arr) - - @commands.command(pass_context=True, aliases=["eval", "exec", "evaluate"]) - @user_is_bot_admin() - async def _eval(self, ctx, *, code: str): - code = self.prepare(code) - args = { - "discord": discord, - "sauce": getsource, - "sys": sys, - "os": os, - "imp": __import__, - "this": self, - "ctx": ctx, - } - - try: - exec(f"async def func():{code}", globals().update(args), locals()) - a = time() - response = await eval("func()", globals().update(args), locals()) - if response is None or isinstance(response, discord.Message): - del args, code - return - - if isinstance(response, str): - response = response.replace("`", "") - - await ctx.send( - f"```py\n{self.resolve_variable(response)}```" - + f"`{type(response).__name__} | {(time() - a) / 1000} ms`" - ) - except Exception: - await ctx.send(f"Error occurred:```\n{traceback.format_exc()}```") - - del args, code - def setup(bot): bot.add_cog(DevCog(bot)) diff --git a/jarvis/cogs/error.py b/jarvis/cogs/error.py index a62cdbc..582ba0b 100644 --- a/jarvis/cogs/error.py +++ b/jarvis/cogs/error.py @@ -12,9 +12,7 @@ class ErrorHandlerCog(commands.Cog): if isinstance(error, commands.errors.MissingPermissions): await ctx.send("I'm afraid I can't let you do that.") elif isinstance(error, commands.errors.CommandNotFound): - await ctx.send( - "Command does not exist. Run `>help` to get a list of commands" - ) + return else: await ctx.send(f"Error processing command:\n```{error}```") @@ -25,9 +23,7 @@ class ErrorHandlerCog(commands.Cog): ): await ctx.send("I'm afraid I can't let you do that.") elif isinstance(error, commands.errors.CommandNotFound): - await ctx.send( - "Command does not exist. Run `>help` to get a list of commands" - ) + return else: await ctx.send(f"Error processing command:\n```{error}```") diff --git a/jarvis/cogs/owner.py b/jarvis/cogs/owner.py index 12311e3..0677ef1 100644 --- a/jarvis/cogs/owner.py +++ b/jarvis/cogs/owner.py @@ -1,5 +1,11 @@ +import os +import sys +import traceback +from inspect import getsource +from time import time + import discord -from discord import User +from discord import DMChannel, User from discord.ext import commands import jarvis @@ -192,6 +198,79 @@ class OwnerCog(commands.Cog): reload_config() await ctx.send(f"{user.mention} is no longer an admin.") + def resolve_variable(self, variable): + if hasattr(variable, "__iter__"): + var_length = len(list(variable)) + if (var_length > 100) and (not isinstance(variable, str)): + return f"" + elif not var_length: + return f"" + + if (not variable) and (not isinstance(variable, bool)): + return f"" + return ( + variable + if (len(f"{variable}") <= 1000) + else f"" + ) + + def prepare(self, string): + arr = ( + string.strip("```") + .replace("py\n", "") + .replace("python\n", "") + .split("\n") + ) + if not arr[::-1][0].replace(" ", "").startswith("return"): + arr[len(arr) - 1] = "return " + arr[::-1][0] + return "".join(f"\n\t{i}" for i in arr) + + @commands.command( + pass_context=True, aliases=["eval", "exec", "evaluate"] + ) + @user_is_bot_admin() + async def _eval(self, ctx, *, code: str): + if not isinstance(ctx.message.channel, DMChannel): + return + code = self.prepare(code) + args = { + "discord": discord, + "sauce": getsource, + "sys": sys, + "os": os, + "imp": __import__, + "this": self, + "ctx": ctx, + } + + try: + exec( + f"async def func():{code}", + globals().update(args), + locals(), + ) + a = time() + response = await eval( + "func()", globals().update(args), locals() + ) + if response is None or isinstance(response, discord.Message): + del args, code + return + + if isinstance(response, str): + response = response.replace("`", "") + + await ctx.send( + f"```py\n{self.resolve_variable(response)}```" + + f"`{type(response).__name__} | {(time() - a) / 1000} ms`" + ) + except Exception: + await ctx.send( + f"Error occurred:```\n{traceback.format_exc()}```" + ) + + del args, code + def setup(bot): bot.add_cog(OwnerCog(bot))