feat: start work on character quiz

fixed DB and added a link command that saves user's list to DB
This commit is contained in:
Matyáš Caras 2024-04-07 12:49:03 +02:00
parent a9318fe9b3
commit 8d33c22f71
Signed by untrusted user who does not match committer: hernik
GPG key ID: 2A3175F98820C5C6
5 changed files with 64 additions and 10 deletions

View file

@ -0,0 +1,18 @@
from interactions import Extension, slash_command, SlashContext
from util.db import get_connection
class CharacterQuizExtension(Extension):
@slash_command(name="kviz", description="Kvíz postav podle tvého anime listu")
async def start_quiz(self, ctx: SlashContext):
await ctx.defer()
c = get_connection(ctx.author_id)
print(c)
if not c:
await ctx.respond("Nemáš propojený anime list, použij příkaz `/link` k propojení!", ephemeral=True)
return
_, _, name, service = c
await ctx.respond(content="Ok", ephemeral=True)

22
commands/link.py Normal file
View file

@ -0,0 +1,22 @@
from interactions import Extension, slash_command, SlashContext, slash_option, OptionType, SlashCommandChoice
from util.db import get_connection, add_user_connection
class LinkExtension(Extension):
@slash_command(name="link", description="Propojí tvůj účet s anime listem")
@slash_option(name="username", description="Uživatelské jméno", required=True, opt_type=OptionType.STRING)
@slash_option(name="service", description="Služba (Anilist, MyAnimeList atp.)", required=True,
opt_type=OptionType.STRING,
choices=[SlashCommandChoice(name="Anilist", value="anilist"), ])
async def link(self, ctx: SlashContext, username: str, service: str):
await ctx.defer()
if get_connection(ctx.author_id) is not None:
await ctx.respond(content="Tvůj účet je již propojen!", ephemeral=True)
return
r = add_user_connection(ctx.author_id, username, service)
if not r:
await ctx.respond(content="Nepodařilo se propojit tě.", ephemeral=True)
return
await ctx.respond(content="Tvůj účet byl úspěšně propojen.", ephemeral=True)
return

View file

@ -44,4 +44,6 @@ async def on_message_create(event: MessageCreate):
return
bot.load_extension("commands.yuri")
bot.load_extension("commands.link")
bot.load_extension("commands.character_quiz")
bot.start(DISCORD_TOKEN)

View file

@ -2,7 +2,6 @@ import sqlite3
from sqlite3 import Error, Connection
from util.config import DB_PATH
from util.enums import AnimeListServices
def setup_database() -> bool:
@ -29,17 +28,37 @@ def setup_database() -> bool:
return True
def add_user_connection(uid: str, listname: str, service: AnimeListServices) -> bool:
def get_connection(uid: str) -> tuple | None:
conn = create_connection()
try:
c = conn.cursor()
c.execute(f"""SELECT * FROM connections WHERE uid='{uid}';""")
o = c.fetchall()
conn.close()
if len(o) == 0:
return None
return o[0]
except Error as e:
print("Error finding user connection:")
print(e)
return None
def add_user_connection(uid: str, listname: str, service: str) -> bool:
conn = create_connection()
if conn is None:
print("No connection")
return False
try:
c = conn.cursor()
c.execute(f"""INSERT INTO connections(uid,animelist,service) VALUES('{uid}','{listname}','{service.value}')""")
c.execute(f"""INSERT INTO connections(uid,animelist,service) VALUES('{uid}','{listname}','{service}')""")
conn.commit()
except Error as e:
print("Error while adding a new connection:")
print(e)
return False
conn.close()
return True
def create_connection() -> Connection | None:

View file

@ -1,7 +0,0 @@
from enum import Enum
class AnimeListServices(Enum):
ANILIST = "anilist"
MYANIMELIST = "mal"
ANIMEPLANET = "apl"