From 6b426b348e3db168cab4d00d3796b7bf8ddbd9a8 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sun, 27 Jan 2019 20:35:17 -0600 Subject: [PATCH] Added help command --- GeoffreyBot/geoffrey.py | 64 ++++++++++++++++++++++----- GeoffreyBot/geoffrey_api.py | 87 ++++++++++++++++++++++++++++++++++++- 2 files changed, 137 insertions(+), 14 deletions(-) diff --git a/GeoffreyBot/geoffrey.py b/GeoffreyBot/geoffrey.py index 78f2753..78891ae 100644 --- a/GeoffreyBot/geoffrey.py +++ b/GeoffreyBot/geoffrey.py @@ -1,5 +1,6 @@ from discord import Game from discord.ext import commands +from discord.ext.commands.formatter import HelpFormatter from discord.utils import oauth_url import requests import logging @@ -13,17 +14,9 @@ description = ''' Geoffrey (pronounced JOFF-ree) started his life as an inside joke none of you will understand. At some point, she was to become an airhorn discord_bot. Now, they know where your stuff is. -Please respect Geoffrey, the discord_bot is very sensitive. - -All commands must be prefaced with '?' - If have a suggestion or if something is borked, you can PM my ding dong of a creator ZeroHD. *You must use ?register before adding things to Geoffrey* - -For a better a explanation on how this discord_bot works go the following link: -https://github.com/joeyahines/Geoffrey/blob/master/README.md - ''' bad_error_message = 'OOPSIE WOOPSIE!! Uwu We made a fucky wucky!! A wittle fucko boingo! The admins at our ' \ @@ -35,12 +28,14 @@ class GeoffreyBot(commands.Bot): self.base_url = base_url self.api_token = api_token - URL = base_url + '/api/settings/' + settings_url = base_url + '/api/settings/' + command_list_url = base_url + '/api/command/commands' logger.info("Connecting to the Geoffrey API... ") while True: try: - setting = requests.get(url=URL, params={"api": self.api_token}).json() + setting = requests.get(url=settings_url, params={"api": self.api_token}).json() + self.command_list = requests.get(url=command_list_url, params={"api": self.api_token}).json() break except Exception: time.sleep(1) @@ -51,11 +46,14 @@ class GeoffreyBot(commands.Bot): self.special_users = [] self.default_status = setting['STATUS'] - super().__init__(command_prefix=self.prefix, description=description, pm_help=True, - case_insensitive=True) + super().__init__(command_prefix=self.prefix, description=description, case_insensitive=True) + self.remove_command("help") self.load_extension('GeoffreyBot.geoffrey_api') + self.help_page = self.build_help_page() + self.help_dict = self.build_help_dict() + async def on_ready(self): logger.info("%s Online, ID: %s", self.user.name, self.user.id) info = await self.application_info() @@ -135,3 +133,45 @@ class GeoffreyBot(commands.Bot): if len(msg) > 0: await ctx.send(msg) + + def build_help_page(self): + help_list = ["```"] + + largest_command_size = 0 + for command in self.command_list: + if largest_command_size < len(command["command"]): + largest_command_size = len(command["command"]) + + for line in self.description.split("\n"): + help_list.append(line) + + help_list.append("\nCommands:") + for command in self.command_list: + name = command["command"].ljust(largest_command_size + 2) + help_list.append(" {}{}{}".format(self.prefix, name, command["help"])) + + help_list.append("\nDo ?help to get more info on any of thee commands above") + help_list.append("```") + + return help_list + + def build_help_dict(self): + help_dict = {} + for command in self.command_list: + command_name = command["command"] + + help_list = ['```'] + + help_list.append(command["help"] + "\n") + + help_msg = self.get_command(command_name).help + help_list.append(help_msg.format(self.prefix)) + + help_list.append('```') + + help_dict[command_name] = help_list + + return help_dict + + + diff --git a/GeoffreyBot/geoffrey_api.py b/GeoffreyBot/geoffrey_api.py index e513be5..97c124f 100644 --- a/GeoffreyBot/geoffrey_api.py +++ b/GeoffreyBot/geoffrey_api.py @@ -43,6 +43,10 @@ class GeoffreyCommands: @commands.command(pass_context=True) async def add_base(self, ctx, x_pos: int, z_pos: int, *args): + ''' + {}add_base + The name parameter is optional. + ''' name = get_name(args) try: @@ -65,6 +69,10 @@ class GeoffreyCommands: @commands.command(pass_context=True) async def add_item(self, ctx, item_name, quantity: int, diamond_price: int, *args): + ''' + {}add_item + The name parameter is optional. + ''' shop_name = get_name(args) try: @@ -87,6 +95,11 @@ class GeoffreyCommands: @commands.command(pass_context=True) async def add_shop(self, ctx, x_pos: int, z_pos: int, *args): + ''' + {}add_shop + The name parameter is optional. + ''' + name = get_name(args) try: @@ -109,6 +122,11 @@ class GeoffreyCommands: @commands.command(pass_context=True) async def add_tunnel(self, ctx, tunnel_direction, tunnel_number: int, *args): + ''' + {}add_tunnel + The name parameter is optional. + ''' + name = get_name(args) try: @@ -134,6 +152,10 @@ class GeoffreyCommands: @commands.command(pass_context=True) async def delete(self, ctx, *args): + ''' + {}delete + ''' + name = get_name(args) try: location = run_command(self.base_url, self.api_token, "POST", "delete", name=name, @@ -152,6 +174,11 @@ class GeoffreyCommands: @commands.command(pass_context=True) async def delete_item(self, ctx, item_name: str, *args): + ''' + {}delete_item + The name parameter is optional. + ''' + shop_name = get_name(args) try: shop = run_command(self.base_url, self.api_token, "POST", "delete_item", item=item_name, @@ -177,6 +204,11 @@ class GeoffreyCommands: @commands.command(pass_conext=True) async def edit_name(self, ctx, new_name: str, old_name: str): + ''' + {}edit_name + If the name has spaces in it, it must be wrapped in quotes. eg "Cool Shop 123" + ''' + try: location = run_command(self.base_url, self.api_token, "POST", "edit_name", loc_name=old_name, new_name=new_name, discord_uuid=ctx.message.author.id) @@ -198,6 +230,10 @@ class GeoffreyCommands: @commands.command(pass_conext=True) async def edit_pos(self, ctx, new_x: int, new_z: int, *args): + ''' + {}edit_post + ''' + loc_name = get_name(args) try: location = run_command(self.base_url, self.api_token, "POST", "edit_pos", x=new_x, z=new_z, @@ -217,6 +253,10 @@ class GeoffreyCommands: @commands.command(pass_conext=True) async def edit_tunnel(self, ctx, new_tunnel_direction: str, new_tunnel_number: int, *args): + ''' + {}edit_tunnel + ''' + loc_name = get_name(args) try: location = run_command(self.base_url, self.api_token, "POST", "edit_tunnel", @@ -242,6 +282,10 @@ class GeoffreyCommands: @commands.command(pass_context=True) async def find_around(self, ctx, x_pos, z_pos, *args): + ''' + {}find_around + ''' + try: if len(args) > 0: radius = int(args[0]) @@ -273,8 +317,12 @@ class GeoffreyCommands: msg = check_error(e, error_list) await ctx.send(msg) - @commands.command(pass_context=True) - async def find(self, ctx, *args): + @commands.command(pass_context=True, aliases=["find"]) + async def find_location(self, ctx, *args): + ''' + {}find_location + ''' + search = get_name(args) try: locations = run_command(self.base_url, self.api_token, "GET", "find_location", search=search) @@ -296,8 +344,24 @@ class GeoffreyCommands: msg = check_error(e, error_list) await ctx.send(msg) + @commands.command(pass_context=True) + async def help(self, ctx, *args): + if len(args) > 0: + command = args[0].lower() + try: + await self.bot.send_list(ctx.message.author, self.bot.help_dict[command]) + except KeyError: + await ctx.message.author.send("{}, no command named **{}** exists. Try {}help".format( + ctx.message.author.mention, command, self.bot.prefix)) + else: + await self.bot.send_list(ctx.message.author, self.bot.help_page) + @commands.command(pass_context=True) async def info(self, ctx, *args): + ''' + {}info + ''' + location_name = get_name(args) try: location = run_command(self.base_url, self.api_token, "GET", "info", location_name=location_name) @@ -319,6 +383,10 @@ class GeoffreyCommands: @commands.command(pass_context=True) async def me(self, ctx): + ''' + {}me + ''' + locations = run_command(self.base_url, self.api_token, "GET", "me", discord_uuid=ctx.message.author.id) message = ["{}, you have the following locations:".format(ctx.message.author.mention)] @@ -330,6 +398,9 @@ class GeoffreyCommands: @commands.command(pass_context=True) async def register(self, ctx): + ''' + {}register + ''' try: run_command(self.base_url, self.api_token, "POST", "register", player_name=ctx.message.author.display_name, discord_uuid=ctx.message.author.id) @@ -348,6 +419,9 @@ class GeoffreyCommands: @commands.command(pass_context=True) async def selling(self, ctx, *args): + ''' + {}selling + ''' item = get_name(args) try: @@ -372,6 +446,11 @@ class GeoffreyCommands: @commands.command(pass_context=True) async def restock(self, ctx, item_name, *args): + ''' + {}restock + The shop name is optional + ''' + shop_name = get_name(args) try: @@ -397,6 +476,10 @@ class GeoffreyCommands: @commands.command(pass_context=True) async def tunnel(self, ctx, *args): + ''' + {}tunnel + ''' + player_name = get_name(args) try: tunnels = run_command(self.base_url, self.api_token, "GET", "tunnel", player_name=player_name)