From c2a4054e069a6c316fe61ef58cb5b8c2c5995c63 Mon Sep 17 00:00:00 2001 From: Luke Robles <98352913+lrobles-iterable@users.noreply.github.com> Date: Thu, 13 Oct 2022 09:37:20 -0700 Subject: [PATCH] convert ffxiv to a slash command --- app/bot.py | 1 + app/cogs/game_apis.py | 42 ++++++++++++++++++++++++++++-------------- app/ffxiv.py | 8 -------- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/app/bot.py b/app/bot.py index e88df377..c7c13b9f 100755 --- a/app/bot.py +++ b/app/bot.py @@ -1,5 +1,6 @@ #!/usr/local/bin/python from discord.ext import commands +import core_utils import discord import os import random diff --git a/app/cogs/game_apis.py b/app/cogs/game_apis.py index 6a33acea..48558b96 100644 --- a/app/cogs/game_apis.py +++ b/app/cogs/game_apis.py @@ -1,3 +1,4 @@ +from discord.commands import Option from discord.ext import commands import core_utils import discord @@ -7,29 +8,42 @@ class Games(commands.Cog): def __init__(self, bot): self.bot: commands.Bot = bot - @commands.command(name="bf5") - async def bf5(self, ctx: commands.Context): + @commands.slash_command( + guild_ids=None, + name="bf5", + description="Query the game's API for data about a player", + ) + async def bf5(self, ctx: commands.Context, player): import bf5 try: - await ctx.reply(embed=bf5.get_player(ctx.message.content.split()[1])) - except Exception: + await ctx.defer() + embed = bf5.get_player(player) + await ctx.send_followup(embed=embed) + except Exception as e: await ctx.send( - "I encountered an error while searching for that player.\nPlease check that your player name is spelled correctly" + e + # "I encountered an error while searching for that player.\nPlease check that your player name is spelled correctly" ) - @commands.command(name="ffxiv") + @commands.slash_command( + guild_ids=None, + name="ffxiv", + description="Query the game's API for data about a player", + ) async def ffxiv(self, ctx: commands.Context, first_name, last_name, server): import ffxiv - async with ctx.message.channel.typing(): - try: - name = "%s %s" % (first_name, last_name) - await ctx.reply(embed=ffxiv.make_request(name=name, server=server)) - except Exception: - await ctx.send( - "I encountered an error while searching for that player.\nPlease check that your player name and server are spelled correctly" - ) + try: + name = " ".join([first_name, last_name]) + embed = ffxiv.make_request(name=name, server=server) + + await ctx.defer() + await ctx.send_followup(embed=embed) + except Exception: + await ctx.send( + "I encountered an error while searching for that player.\nPlease check that your player name and server are spelled correctly" + ) def setup(bot): diff --git a/app/ffxiv.py b/app/ffxiv.py index fe1ad0bb..790b6269 100755 --- a/app/ffxiv.py +++ b/app/ffxiv.py @@ -14,16 +14,12 @@ def make_request(name, server): server, token, ) - # print(player_search) player_id = list( filter( lambda x: server.lower() in x["Server"].lower(), requests.get(player_search).json()["Results"], ) )[0]["ID"] - import pprint - - pp = pprint.PrettyPrinter(indent=2, compact=True) # if adding extended=1 to the query, you'll see some image files, eg. /cj/1/blackmage.png # prepend these with https://xivapi.com/ to get a useable url @@ -32,11 +28,8 @@ def make_request(name, server): player_id, token, ) - # print(request_url) player_blob = requests.get(request_url).json() character = player_blob["Character"] - # pp.pprint(player_blob) - # pp.pprint(character) current_class = character["ActiveClassJob"] current_class_name = current_class["Job"]["Name"] @@ -103,5 +96,4 @@ def make_request(name, server): name=job["Job"]["Abbreviation"], value=job["Level"], inline=True ) - # embed.set_footer(text='Feel free to use my refer a friend code: P9CQ9JUT') return embed