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:
parent
a9318fe9b3
commit
8d33c22f71
5 changed files with 64 additions and 10 deletions
18
commands/character_quiz.py
Normal file
18
commands/character_quiz.py
Normal 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
22
commands/link.py
Normal 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
|
2
main.py
2
main.py
|
@ -44,4 +44,6 @@ async def on_message_create(event: MessageCreate):
|
||||||
return
|
return
|
||||||
|
|
||||||
bot.load_extension("commands.yuri")
|
bot.load_extension("commands.yuri")
|
||||||
|
bot.load_extension("commands.link")
|
||||||
|
bot.load_extension("commands.character_quiz")
|
||||||
bot.start(DISCORD_TOKEN)
|
bot.start(DISCORD_TOKEN)
|
||||||
|
|
25
util/db.py
25
util/db.py
|
@ -2,7 +2,6 @@ import sqlite3
|
||||||
from sqlite3 import Error, Connection
|
from sqlite3 import Error, Connection
|
||||||
|
|
||||||
from util.config import DB_PATH
|
from util.config import DB_PATH
|
||||||
from util.enums import AnimeListServices
|
|
||||||
|
|
||||||
|
|
||||||
def setup_database() -> bool:
|
def setup_database() -> bool:
|
||||||
|
@ -29,17 +28,37 @@ def setup_database() -> bool:
|
||||||
return True
|
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()
|
conn = create_connection()
|
||||||
if conn is None:
|
if conn is None:
|
||||||
|
print("No connection")
|
||||||
return False
|
return False
|
||||||
try:
|
try:
|
||||||
c = conn.cursor()
|
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:
|
except Error as e:
|
||||||
print("Error while adding a new connection:")
|
print("Error while adding a new connection:")
|
||||||
print(e)
|
print(e)
|
||||||
return False
|
return False
|
||||||
|
conn.close()
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def create_connection() -> Connection | None:
|
def create_connection() -> Connection | None:
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
from enum import Enum
|
|
||||||
|
|
||||||
|
|
||||||
class AnimeListServices(Enum):
|
|
||||||
ANILIST = "anilist"
|
|
||||||
MYANIMELIST = "mal"
|
|
||||||
ANIMEPLANET = "apl"
|
|
Loading…
Reference in a new issue