summaryrefslogtreecommitdiff
path: root/cogs/FFXIV.py
blob: 038a6378e24a60c9d098d1480e1a15fe7733c69d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from discord.ext import commands
from PIL import Image, ImageFont, ImageDraw
from ratelimit import limits, sleep_and_retry
import discord
import importlib
import utils
import csv
import json
import requests
import asyncio
from ffxiv.ffxiv_core import FFXIVCharacter, ScrapeLodestone
from io import BytesIO
importlib.reload(utils)


class FFXIV(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(
        aliases=[],
        application_command_meta=commands.ApplicationCommandMeta(
            options=[
                discord.ApplicationCommandOption(
                    name="fname",
                    description="The first name of this character",
                    type=discord.ApplicationCommandOptionType.string,
                    required=True,
                ),
                discord.ApplicationCommandOption(
                    name="lname",
                    description="The last name of your character",
                    type=discord.ApplicationCommandOptionType.string,
                    required=True,
                ),
                discord.ApplicationCommandOption(
                    name="server",
                    description="The homeworld of your character",
                    type=discord.ApplicationCommandOptionType.string,
                    required=True,
                )
            ],
        )
    )
    async def findme(self, ctx, fname, lname, server):
        """Find and save a character from the FFXIV Lodestone"""
        member = ctx.interaction.user
        msg = await ctx.send(f'Searching for {fname} {lname} on {server}...')

        url = f"https://xivapi.com/character/search?name={fname}%20{lname}&server={server}"
        data = await utils.get_request(url)
        if data and len(data["Results"]) > 0:
            data_id = data["Results"][0]["ID"]
            print(f'Remembering {data["Results"][0]}')
            print(f'Remembering {data["Results"][0]["Name"]}')
            print(f'Remembering {data["Results"][0]["ID"]}')
            await utils.sql('INSERT INTO "database1".synthy.ffxiv (member_id, id) VALUES (%s, %s) ON CONFLICT (member_id) DO UPDATE SET id = %s;', (member.id, data_id, data_id,))
            await msg.edit(content=f'I have found {data["Results"][0]["Name"]} on {data["Results"][0]["Server"]}. From now on you can use `showme` to view your character.')
        else:
            await msg.edit(content=f'Unable to find a character with this name.')
            return

    @commands.defer(ephemeral=False)
    @commands.command(aliases=[], application_command_meta=commands.ApplicationCommandMeta(options=[]))
    async def showme(self, ctx):
        "Show your saved character from the FFXIV Lodestone"""
        member = ctx.interaction.user
        user_id = await utils.sql(f'SELECT id FROM "database1".synthy.ffxiv WHERE member_id = %s;', (member.id,))
        if user_id and len(user_id) == 1 and 'id' in user_id[0]:
            user_id = user_id[0]['id']
        else:
            await ctx.send("Unable to find your character. Please try using /findme `firstname` `lastname` `server` first")
            return

        character = FFXIVCharacter(character_id=user_id)
        await character.obtain_character_data()
        await character.build_character_view()

        with open('./character.png', 'rb') as fp:
            await ctx.interaction.edit_original_message(content=None, file=discord.File(fp, "image.png"))
            return


def setup(bot):
    print("INFO: Loading [FFXIV]... ", end="")
    bot.add_cog(FFXIV(bot))
    print("Done!")


def teardown(bot):
    print("INFO: Unloading [FFXIV]")