From fe30c9405f2056106b63075e3d620d83f3b2b466 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sat, 11 Aug 2018 11:20:40 -0500 Subject: [PATCH] Added admin commands for managing the db --- Add_Commands.py | 5 +++ Admin_Commands.py | 88 ++++++++++++++++++++++++++++++++++++++++------ BotConfig.py | 3 +- BotErrors.py | 2 ++ Commands.py | 46 ++++++++++++++++++++++++ Edit_Commands.py | 5 +-- Geoffrey.py | 3 ++ Search_Commands.py | 9 +++-- 8 files changed, 145 insertions(+), 16 deletions(-) diff --git a/Add_Commands.py b/Add_Commands.py index 7325f49..d6fcdf3 100644 --- a/Add_Commands.py +++ b/Add_Commands.py @@ -15,6 +15,7 @@ class Add_Commands: self.bot = bot @commands.command(pass_context=True) + @commands.cooldown(5, 60, commands.BucketType.user) async def register(self, ctx): ''' Registers your Discord and Minecraft account with the the database. @@ -31,6 +32,7 @@ class Add_Commands: await self.bot.say('{}, you are already in the database. Ding dong.'.format(ctx.message.author.mention)) @commands.command(pass_context=True) + @commands.cooldown(5, 60, commands.BucketType.user) async def add_base(self, ctx, x_pos: int, z_pos: int, *args): ''' Adds your base to the database. @@ -55,6 +57,7 @@ class Add_Commands: ctx.message.author.mention, name)) @commands.command(pass_context=True) + @commands.cooldown(5, 60, commands.BucketType.user) async def add_shop(self, ctx, x_pos: int, z_pos: int, *args): ''' Adds your shop to the database. @@ -79,6 +82,7 @@ class Add_Commands: ctx.message.author.mention, name)) @commands.command(pass_context=True) + @commands.cooldown(5, 60, commands.BucketType.user) async def add_tunnel(self, ctx, tunnel_color: str, tunnel_number: int, *args): ''' Adds your tunnel to the database. If you only have one location, you do not need to specify a location name. @@ -104,6 +108,7 @@ class Add_Commands: await self.bot.say('{}, **{}** is an invalid tunnel color.'.format(ctx.message.author.mention, tunnel_color)) @commands.command(pass_context=True) + @commands.cooldown(5, 60, commands.BucketType.user) async def add_item(self, ctx, item_name: str, quantity: int, diamond_price: int, *args): ''' Adds an item to a shop's inventory. diff --git a/Admin_Commands.py b/Admin_Commands.py index c3e2ec9..325ddb7 100644 --- a/Admin_Commands.py +++ b/Admin_Commands.py @@ -1,9 +1,21 @@ from discord.ext import commands from BotErrors import * +from BotConfig import bot_config from DiscordHelperFunctions import * from Geoffrey import bot_commands +def check_mod(user): + try: + for role in user.roles: + if role.id == bot_config.bot_mod: + return True + except AttributeError: + raise NotOnServerError + + return False + + class Admin_Commands: ''' Commands for cool people only. @@ -12,26 +24,82 @@ class Admin_Commands: def __init__(self, bot): self.bot = bot - def check_mod(self, user): - try: - if ("admin" in [y.name.lower() for y in user.roles]) | \ - ("mod" in [y.name.lower() for y in user.roles]): - return True - else: - return False - except AttributeError: - raise NotOnServerError + async def error(self, error, ctx): + if isinstance(error.original, PlayerNotFound): + error_str = 'that player is not in the database.' + elif isinstance(error.original, DeleteEntryError): + error_str = 'that player does not have a location by that name.' + else: + return + + await self.bot.send_message(ctx.message.channel, '{}, {}'.format(ctx.message.author.mention, error_str)) @commands.command(pass_context=True) async def test(self, ctx): ''' Checks if the bot is alive. ''' - if self.check_mod(ctx.message.author): + if check_mod(ctx.message.author): await self.bot.say('I\'m here you ding dong') else: raise NoPermissionError + @commands.group(pass_context=True) + async def mod(self, ctx): + + if check_mod(ctx.message.author): + if ctx.invoked_subcommand is None: + await self.bot.say('{}, invalid sub-command for command **mod**.'.format(ctx.message.author.mention)) + else: + raise NoPermissionError + + @mod.command(pass_context=True) + async def delete(self, ctx, discord_uuid: str, location_name: str): + bot_commands.delete(location_name, discord_uuid=discord_uuid) + await self.bot.say('{}, **{}** has been deleted.'.format(ctx.message.author.mention, location_name)) + + @delete.error + async def delete_error(self, error, ctx): + await self.error(error, ctx) + + @mod.command(pass_context=True) + async def edit(self, ctx, discord_uuid: str, new_name: str, current_name: str): + bot_commands.edit_name(new_name, current_name, discord_uuid=discord_uuid) + await self.bot.say('{}, **{}** has been rename to **{}**.'.format(ctx.message.author.mention, current_name, + new_name)) + + @edit.error + async def edit_error(self, error, ctx): + await self.error(error, ctx) + + @mod.command(pass_context=True) + async def update_mc_uuid(self, ctx, discord_uuid: str, mc_uuid: str): + bot_commands.update_mc_uuid(discord_uuid, mc_uuid) + await self.bot.say('{}, **{}** has been updated.'.format(ctx.message.author.mention, discord_uuid)) + + @update_mc_uuid.error + async def update_mc_uuid_error(self, error, ctx): + await self.error(error, ctx) + + @mod.command(pass_context=True) + async def update_discord_uuid(self, ctx, current_discord_uuid: str, new_discord_uuid: str): + bot_commands.update_mc_uuid(current_discord_uuid, new_discord_uuid) + await self.bot.say('{}, user **{}** has been updated.'.format(ctx.message.author.mention, current_discord_uuid)) + + @update_discord_uuid.error + async def update_discord_uuid_error(self, error, ctx): + await self.error(error, ctx) + + @mod.command(pass_context=True) + async def update_mc_name(self, ctx, discord_uuid: str, mc_name: str): + bot_commands.update_mc_name(discord_uuid, mc_name) + await self.bot.say('{}, user **{}** has been renamed.'.format(ctx.message.author.mention, mc_name)) + + @update_mc_name.error + async def update_mc_name_error(self, error, ctx): + await self.error(error, ctx) + + def setup(bot): bot.add_cog(Admin_Commands(bot)) \ No newline at end of file diff --git a/BotConfig.py b/BotConfig.py index 969129c..10deedc 100644 --- a/BotConfig.py +++ b/BotConfig.py @@ -12,6 +12,7 @@ class Config: self.status = self.config['Discord']['Status'] self.prefix = self.config['Discord']['Prefix'] self.dynmap_url = self.config['Minecraft']['dynmap_url'] + self.bot_mod = self.config['Discord']['bot_mod'] except: print("Invalid config file") quit(1) @@ -30,7 +31,7 @@ class Config: def create_config(self, config): config['Discord'] = {'Token': '', 'Status': '', 'Prefix': '', } config['SQL'] = {'Dialect+Driver': '', 'username': '', 'password': '', 'host': '', 'port': '', - 'database':'','test_args':''} + 'database': '', 'bot_mod': ''} config['Minecraft'] = {'World_Name': '', 'dynmap_url': ''} with open('GeoffreyConfig.ini', 'w') as configfile: config.write(configfile) diff --git a/BotErrors.py b/BotErrors.py index c6187fa..5cd5de2 100644 --- a/BotErrors.py +++ b/BotErrors.py @@ -53,3 +53,5 @@ class NoPermissionError(DataBaseError): class NotOnServerError(DataBaseError): '''You need to run this command on 24CC''' +class FuckyWucky: + '''You made one.''' \ No newline at end of file diff --git a/Commands.py b/Commands.py index aba048f..cafbe88 100644 --- a/Commands.py +++ b/Commands.py @@ -328,3 +328,49 @@ class Commands: return loc_str + def update_mc_uuid(self, mc_uuid, discord_uuid): + session = self.interface.database.Session() + + try: + player = self.interface.find_player_by_discord_uuid(session, discord_uuid) + + player.mc_uuid = mc_uuid + + session.commit() + except Exception as e: + session.rollback() + raise e + finally: + session.close() + + def update_discord_uuid(self, old_discord_uuid, new_discord_uuid): + session = self.interface.database.Session() + + try: + player = self.interface.find_player_by_discord_uuid(session, old_discord_uuid) + + player.discord_uuid = new_discord_uuid + + session.commit() + except Exception as e: + session.rollback() + raise e + finally: + session.close() + + def update_mc_name(self, discord_uuid, mc_name): + session = self.interface.database.Session() + + try: + player = self.interface.find_player_by_discord_uuid(session, discord_uuid) + + player.name = mc_name + + session.commit() + except Exception as e: + session.rollback() + raise e + finally: + session.close() + + diff --git a/Edit_Commands.py b/Edit_Commands.py index dfa3ef2..4d028d5 100644 --- a/Edit_Commands.py +++ b/Edit_Commands.py @@ -4,13 +4,13 @@ from DiscordHelperFunctions import * from Geoffrey import bot_commands -@commands.cooldown(5, 60, commands.BucketType.user) class Edit_Commands: def __init__(self, bot): self.bot = bot @commands.command(pass_context=True) + @commands.cooldown(5, 60, commands.BucketType.user) async def edit_pos(self, ctx, x_pos: int, y_pos: int, *args): ''' Edits the position of a location @@ -27,8 +27,8 @@ class Edit_Commands: await self.bot.say('{}, you do not have a location called **{}**.'.format( ctx.message.author.mention, loc)) - @commands.command(pass_context=True) + @commands.cooldown(5, 60, commands.BucketType.user) async def edit_tunnel(self, ctx, tunnel_color: str, tunnel_number: int, *args): ''' Edits the tunnel of a location @@ -48,6 +48,7 @@ class Edit_Commands: await self.bot.say('{}, **{}** is an invalid tunnel color.'.format(ctx.message.author.mention, tunnel_color)) @commands.command(pass_context=True) + @commands.cooldown(5, 60, commands.BucketType.user) async def edit_name(self, ctx, new_name: str, current_name: str): ''' Edits the name of a location diff --git a/Geoffrey.py b/Geoffrey.py index 3ce8f5a..7572990 100644 --- a/Geoffrey.py +++ b/Geoffrey.py @@ -36,6 +36,9 @@ async def on_ready(): @bot.event async def on_command_error(error, ctx): + if hasattr(ctx, 'cog'): + if "Admin_Commands" in ctx.cog.__str__(): + return if isinstance(error, commands.CommandNotFound): error_str = 'Command not found, ding dongs like you can use ?help to see all the commands this bot can do.' elif isinstance(error, commands.CommandOnCooldown): diff --git a/Search_Commands.py b/Search_Commands.py index 2e8837a..a6b8baa 100644 --- a/Search_Commands.py +++ b/Search_Commands.py @@ -4,7 +4,6 @@ from DiscordHelperFunctions import * from Geoffrey import bot_commands -@commands.cooldown(5, 60, commands.BucketType.user) class Search_Commands: ''' Commands to find stuff. @@ -14,6 +13,7 @@ class Search_Commands: self.bot = bot @commands.command(pass_context=True) + @commands.cooldown(5, 60, commands.BucketType.user) async def find(self, ctx, * args): ''' Finds all the locations and tunnels matching the search term @@ -33,6 +33,7 @@ class Search_Commands: @commands.command(pass_context=True) + @commands.cooldown(5, 60, commands.BucketType.user) async def tunnel(self, ctx, player: str): ''' Finds all the tunnels a player owns. @@ -48,6 +49,7 @@ class Search_Commands: @commands.command(pass_context=True) + @commands.cooldown(5, 60, commands.BucketType.user) async def find_around(self, ctx, x_pos: int, z_pos: int, * args): ''' Finds all the locations around a certain point. @@ -90,6 +92,7 @@ class Search_Commands: await self.bot.say('{}, {} is an invalid dimension.'.format(ctx.message.author.mention, dimension)) @commands.command(pass_context=True) + @commands.cooldown(5, 60, commands.BucketType.user) async def selling(self, ctx, item_name: str): ''' Lists all the shops selling an item @@ -103,6 +106,7 @@ class Search_Commands: await self.bot.say('{}, no shop sells **{}**.'.format(ctx.message.author.mention, item_name)) @commands.command(pass_context=True) + @commands.cooldown(5, 60, commands.BucketType.user) async def info(self, ctx, * args): ''' Displays info about a location. @@ -123,11 +127,10 @@ class Search_Commands: await self.bot.say('{}, no locations in the database match **{}**.'.format(ctx.message.author.mention, loc)) @commands.command(pass_context=True) + @commands.cooldown(5, 60, commands.BucketType.user) async def me(self, ctx): ''' Displays all your locations in the database - - ?me ''' try: