diff --git a/config.example.yaml b/config.example.yaml index 16e45f4..7663eee 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -1,11 +1,6 @@ --- token: api key here client_id: 123456789012345678 - admins: - - list - - of - - user - - ids logo: alligator2 mongo: username: user diff --git a/jarvis/cogs/admin.py b/jarvis/cogs/admin.py index 55f1c03..afbf531 100644 --- a/jarvis/cogs/admin.py +++ b/jarvis/cogs/admin.py @@ -1,5 +1,7 @@ from discord import User from discord.ext import commands +from jarvis.utils.db import DBManager +from jarvis.utils import user_is_bot_admin class AdminCog(commands.Cog): @@ -11,6 +13,7 @@ class AdminCog(commands.Cog): def __init__(self, bot): self.bot = bot + self.db = DBManager() @commands.command(name="ban") @commands.has_permissions(administrator=True) @@ -21,12 +24,32 @@ class AdminCog(commands.Cog): reason = ( "Mr. Stark is displeased with your presence. Please leave." ) + guild_name = ctx.message.guild.name await user.send( - f"You have been banned from Stark Industries. Reason:\n{reason}" + f"You have been banned from {guild_name}. Reason:\n{reason}" ) await ctx.guild.ban(user, reason=reason) await ctx.send( - f"{user.name} has been banned from Stark Industries." + f"{user.name} has been banned from {guild_name}." + + f"Reason:\n{reason}" + ) + + @commands.command(name="kick") + @commands.has_permissions(administrator=True) + async def _kick(self, ctx, user: User = None, reason=None): + if not user or user == ctx.message.author: + await ctx.send("You cannot kick yourself.") + if not reason: + reason = ( + "Mr. Stark is displeased with your presence. Please leave." + ) + guild_name = ctx.message.guild.name + await user.send( + f"You have been kicked from {guild_name}. Reason:\n{reason}" + ) + await ctx.guild.kick(user, reason=reason) + await ctx.send( + f"{user.name} has been banned from {guild_name}." + f"Reason:\n{reason}" ) diff --git a/jarvis/cogs/dev.py b/jarvis/cogs/dev.py index 0c87f8f..dd4003c 100644 --- a/jarvis/cogs/dev.py +++ b/jarvis/cogs/dev.py @@ -15,6 +15,7 @@ from discord.ext import commands from jarvis.utils import build_embed, convert_bytesize from jarvis.utils.field import Field from bson import ObjectId +from jarvis.utils import user_is_bot_admin supported_hashes = { @@ -270,7 +271,7 @@ class DevCog(commands.Cog): return "".join(f"\n\t{i}" for i in arr) @commands.command(pass_context=True, aliases=["eval", "exec", "evaluate"]) - @commands.is_owner() + @commands.check(user_is_bot_admin) async def _eval(self, ctx, *, code: str): code = self.prepare(code) args = { @@ -294,7 +295,7 @@ class DevCog(commands.Cog): await ctx.send( f"```py\n{self.resolve_variable(response)}````{type(response).__name__} | {(time() - a) / 1000} ms`" ) - except Exception as e: + except Exception: await ctx.send(f"Error occurred:```\n{traceback.format_exc()}```") del args, code diff --git a/jarvis/cogs/owner.py b/jarvis/cogs/owner.py index 384d3aa..07baa7f 100644 --- a/jarvis/cogs/owner.py +++ b/jarvis/cogs/owner.py @@ -2,7 +2,7 @@ import jarvis import discord from discord.ext import commands from jarvis.config import get_config -from jarvis.utils import update +from jarvis.utils import update, user_is_bot_admin class OwnerCog(commands.Cog): @@ -17,6 +17,7 @@ class OwnerCog(commands.Cog): self.admins = get_config().admins @commands.command(name="load", hidden=True) + @commands.check(user_is_bot_admin) async def _load_cog(self, ctx, *, cog: str): info = await self.bot.application_info() if ( @@ -35,6 +36,7 @@ class OwnerCog(commands.Cog): await ctx.send("I'm afraid I can't let you do that") @commands.command(name="unload", hidden=True) + @commands.check(user_is_bot_admin) async def _unload_cog(self, ctx, *, cog: str): if cog == "jarvis.cogs.owner": await ctx.send("Cannot unload `owner` cog") @@ -56,6 +58,7 @@ class OwnerCog(commands.Cog): await ctx.send("I'm afraid I can't let you do that") @commands.command(name="reload", hidden=True) + @commands.check(user_is_bot_admin) async def _cog_reload(self, ctx, *, cog: str): if cog == "jarvis.cogs.owner": await ctx.send("Cannot reload `owner` cog") @@ -78,6 +81,7 @@ class OwnerCog(commands.Cog): await ctx.send("I'm afraid I can't let you do that") @commands.group(name="system", hidden=True, pass_context=True) + @commands.check(user_is_bot_admin) async def _system(self, ctx): if ctx.invoked_subcommand is None: await ctx.send( @@ -86,6 +90,7 @@ class OwnerCog(commands.Cog): ) @_system.command(name="restart", hidden=True) + @commands.check(user_is_bot_admin) async def _restart(self, ctx): info = await self.bot.application_info() if ( @@ -108,6 +113,7 @@ class OwnerCog(commands.Cog): await ctx.send("I'm afraid I can't let you do that") @_system.command(name="update", hidden=True) + @commands.check(user_is_bot_admin) async def _update(self, ctx): info = await self.bot.application_info() if ( @@ -138,6 +144,11 @@ class OwnerCog(commands.Cog): else: await ctx.send("I'm afraid I can't let you do that") + @commands.Cog.listener() + async def on_command_error(self, ctx, error): + if isinstance(error, commands.errors.MissingPermissions): + await ctx.send("I'm afraid I can't let you do that.") + def setup(bot): bot.add_cog(OwnerCog(bot)) diff --git a/jarvis/config.py b/jarvis/config.py index dc44da2..14571f1 100644 --- a/jarvis/config.py +++ b/jarvis/config.py @@ -42,3 +42,8 @@ def get_config(path: str = "config.yaml") -> Config: raw = f.read() y = load(raw, Loader=Loader) return Config.from_yaml(y) + + +def reload_config(): + if "it" in Config.__dict__: + Config.__dict__.pop("it") diff --git a/jarvis/utils/__init__.py b/jarvis/utils/__init__.py index 499037a..7cfcdd6 100644 --- a/jarvis/utils/__init__.py +++ b/jarvis/utils/__init__.py @@ -4,6 +4,7 @@ from pkgutil import iter_modules from datetime import datetime import jarvis.cogs import jarvis.utils.db +import jarvis.config import git __all__ = ["field", "db"] @@ -85,3 +86,7 @@ def update(): def get_repo_hash(): repo = git.Repo(".") return repo.head.object.hexsha + + +def user_is_bot_admin(ctx): + return ctx.author.id in jarvis.config.get_config().admins