Compare commits

...

3 Commits

Author SHA1 Message Date
Anorak_1 3fedb29509 Removed some unnecessary prints 2023-08-10 22:30:24 +02:00
Anorak_1 75ff784301 Added guild db management on start/join/kick guild 2023-08-10 22:29:21 +02:00
Anorak_1 f73a6612e2 Fix **a lot** of bugs 2023-08-10 22:20:50 +02:00
2 changed files with 65 additions and 31 deletions

8
bot.py
View File

@ -9,6 +9,7 @@ from discord.ui import Modal, InputText
from discord.utils import get from discord.utils import get
from discord.ext import commands from discord.ext import commands
from dbutil import MessageDB from dbutil import MessageDB
from dbutil import GuildAppDB
load_dotenv() load_dotenv()
@ -39,16 +40,23 @@ async def on_ready():
activity = discord.Activity(name=f"{len(bot.guilds)} guilds", type=discord.ActivityType.listening) activity = discord.Activity(name=f"{len(bot.guilds)} guilds", type=discord.ActivityType.listening)
await bot.change_presence(activity=activity, status = discord.Status.online) await bot.change_presence(activity=activity, status = discord.Status.online)
print(f"Logged in as {bot.user}") print(f"Logged in as {bot.user}")
for i in bot.guilds:
if str(i.id) not in GuildAppDB.get_all_guilds():
GuildAppDB.create_guild(str(i.id), i.name)
print(f"entry for {i.id} created")
@bot.event @bot.event
async def on_guild_join(guild): async def on_guild_join(guild):
activity = discord.Activity(name=f"{len(bot.guilds)} guilds", type=discord.ActivityType.listening) activity = discord.Activity(name=f"{len(bot.guilds)} guilds", type=discord.ActivityType.listening)
await bot.change_presence(activity=activity, status = discord.Status.online) await bot.change_presence(activity=activity, status = discord.Status.online)
GuildAppDB.create_guild(str(guild.id), guild.name)
print(f"Joined guild {guild.name}: {guild.id}")
@bot.event @bot.event
async def on_guild_remove(guild): async def on_guild_remove(guild):
activity = discord.Activity(name=f"{len(bot.guilds)} guilds", type=discord.ActivityType.listening) activity = discord.Activity(name=f"{len(bot.guilds)} guilds", type=discord.ActivityType.listening)
await bot.change_presence(activity=activity, status = discord.Status.online) await bot.change_presence(activity=activity, status = discord.Status.online)
print(f"Removed from guild {guild.name}: {guild.id}")
@bot.slash_command(description = "Command used to apply") @bot.slash_command(description = "Command used to apply")
async def apply(ctx): async def apply(ctx):

View File

@ -30,7 +30,7 @@ class GuildAppDB():
applications = {} applications = {}
application_blob = pickle.dumps(applications) application_blob = pickle.dumps(applications)
data = guild_id, guild_name, application_blob data = guild_id, guild_name, application_blob
con = sqlite3.connect("application.db") con = sqlite3.connect("applications.db")
cur = con.cursor() cur = con.cursor()
cur.execute("INSERT INTO app_guildapp_db VALUES (?, ?, ?)", data) cur.execute("INSERT INTO app_guildapp_db VALUES (?, ?, ?)", data)
con.commit() con.commit()
@ -38,22 +38,32 @@ class GuildAppDB():
def remove_guild(guild_id: str) -> None: def remove_guild(guild_id: str) -> None:
con = sqlite3.connect("applications.db") con = sqlite3.connect("applications.db")
cur = con.cursor() cur = con.cursor()
cur.execute(f"DELETE FROM app_guildapp_db WHERE guild_id={guild_id}") cur.execute("DELETE FROM app_guildapp_db WHERE guild_id=(?)", (guild_id, ))
con.commit() con.commit()
def get_all_guilds() -> list:
con = sqlite3.connect("applications.db")
cur = con.cursor()
cur.execute("SELECT guild_id FROM app_guildapp_db")
guilds = cur.fetchall()
guilds = [guild[0] for guild in guilds]
return guilds
def add_application_entry(guild_id: str, application_name: str) -> str: def add_application_entry(guild_id: str, application_name: str) -> str:
con = sqlite3.connect("applications.db") con = sqlite3.connect("applications.db")
cur = con.cursor() cur = con.cursor()
cur.execute(f"SELECT application_blob FROM app_guildapp_db WHERE guild_id={guild_id}") cur.execute("SELECT applications_blob FROM app_guildapp_db WHERE guild_id=(?)", (guild_id, ))
application_blob = cur.fetchone() application_blob = cur.fetchone()
applications = pickle.loads(application_blob) applications = pickle.loads(application_blob[0])
print(applications)
if application_name not in applications.keys(): if application_name not in applications.keys():
applications[f"application_name"] = { applications[application_name] = {
"app_id": "", "app_id": "",
"resp_channel": "" "resp_channel": "",
"questions": []
} }
application_blob2 = pickle.dumps(applications) application_blob2 = pickle.dumps(applications)
cur.execute(f"UPDATE app_guildapp_db SET application_blob = ? WHERE guild_id={guild_id}", application_blob2) cur.execute("UPDATE app_guildapp_db SET applications_blob = (?) WHERE guild_id= (?)", (application_blob2, guild_id))
con.commit() con.commit()
return "success" return "success"
else: else:
@ -62,28 +72,37 @@ class GuildAppDB():
def remove_application_entry(guild_id: str, application_name: str) -> str: def remove_application_entry(guild_id: str, application_name: str) -> str:
con = sqlite3.connect("applications.db") con = sqlite3.connect("applications.db")
cur = con.cursor() cur = con.cursor()
cur.execute(f"SELECT application_blob FROM app_guildapp_db WHERE guild_id={guild_id}") cur.execute("SELECT applications_blob FROM app_guildapp_db WHERE guild_id=(?)", (guild_id, ))
application_blob = cur.fetchone() application_blob = cur.fetchone()
applications = pickle.loads(application_blob) applications = pickle.loads(application_blob[0])
if application_name in applications.keys(): if application_name in applications.keys():
applications.pop(application_name) applications.pop(application_name)
application_blob2 = pickle.dumps(applications) application_blob2 = pickle.dumps(applications)
cur.execute(f"UPDATE app_guildapp_db SET application_blob = ? WHERE guild_id={guild_id}", application_blob2) cur.execute("UPDATE app_guildapp_db SET applications_blob = (?) WHERE guild_id= (?)", (application_blob2, guild_id))
con.commit() con.commit()
return "success" return "success"
else: else:
return "error on remove application entry: application not found" return "error on remove application entry: application not found"
def get_application_entry(guild_id: str, application_name: str):
con = sqlite3.connect("applications.db")
cur = con.cursor()
cur.execute("SELECT applications_blob FROM app_guildapp_db WHERE guild_id=(?)", (guild_id, ))
application_blob = cur.fetchone()
applications = pickle.loads(application_blob[0])
if application_name in applications.keys():
return applications[application_name]
def set_response_channel(guild_id: str, application_name: str, channel_id: str) -> str: def set_response_channel(guild_id: str, application_name: str, channel_id: str) -> str:
con = sqlite3.connect("applications.db") con = sqlite3.connect("applications.db")
cur = con.cursor() cur = con.cursor()
cur.execute(f"SELECT application_blob FROM app_guildapp_db WHERE guild_id={guild_id}") cur.execute("SELECT applications_blob FROM app_guildapp_db WHERE guild_id=(?)", (guild_id, ))
application_blob = cur.fetchone() application_blob = cur.fetchone()
applications = pickle.loads(application_blob) applications = pickle.loads(application_blob[0])
if application_name in applications.keys(): if application_name in applications.keys():
applications[application_name]["resp_channel"] = channel_id applications[application_name]["resp_channel"] = channel_id
application_blob2 = pickle.dumps(applications) application_blob2 = pickle.dumps(applications)
cur.execute(f"UPDATE app_guildapp_db SET application_blob = ? WHERE guild_id={guild_id}", application_blob2) cur.execute("UPDATE app_guildapp_db SET applications_blob = (?) WHERE guild_id= (?)", (application_blob2, guild_id))
con.commit() con.commit()
return "success" return "success"
else: else:
@ -92,14 +111,14 @@ class GuildAppDB():
def add_question(guild_id: str, application_name: str, question: str) -> str: def add_question(guild_id: str, application_name: str, question: str) -> str:
con = sqlite3.connect("applications.db") con = sqlite3.connect("applications.db")
cur = con.cursor() cur = con.cursor()
cur.execute(f"SELECT application_blob FROM app_guildapp_db WHERE guild_id={guild_id}") cur.execute("SELECT applications_blob FROM app_guildapp_db WHERE guild_id=(?)", (guild_id, ))
application_blob = cur.fetchone() application_blob = cur.fetchone()
applications = pickle.loads(application_blob) applications = pickle.loads(application_blob[0])
if application_name in applications.keys(): if application_name in applications.keys():
question_index = int(len(applications[application_name]["questions"])) question_index = int(len(applications[application_name]["questions"]))
applications[application_name]["questions"][question_index] = question applications[application_name]["questions"].append(question)
application_blob2 = pickle.dumps(applications) application_blob2 = pickle.dumps(applications)
cur.execute(f"UPDATE app_guildapp_db SET application_blob = ? WHERE guild_id={guild_id}", application_blob2) cur.execute("UPDATE app_guildapp_db SET applications_blob = (?) WHERE guild_id= (?)", (application_blob2, guild_id))
con.commit() con.commit()
return "success" return "success"
else: else:
@ -108,9 +127,9 @@ class GuildAppDB():
def get_questions(guild_id: str, application_name: str): def get_questions(guild_id: str, application_name: str):
con = sqlite3.connect("applications.db") con = sqlite3.connect("applications.db")
cur = con.cursor() cur = con.cursor()
cur.execute(f"SELECT application_blob FROM app_guildapp_db WHERE guild_id={guild_id}") cur.execute("SELECT applications_blob FROM app_guildapp_db WHERE guild_id=(?)", (guild_id, ))
application_blob = cur.fetchone() application_blob = cur.fetchone()
applications = pickle.loads(application_blob) applications = pickle.loads(application_blob[0])
if application_name in applications.keys(): if application_name in applications.keys():
questions = applications[application_name]["questions"] questions = applications[application_name]["questions"]
return questions, len(questions) return questions, len(questions)
@ -120,16 +139,16 @@ class GuildAppDB():
def edit_question(guild_id: str, application_name: str, question_index: int, new_question: str): def edit_question(guild_id: str, application_name: str, question_index: int, new_question: str):
con = sqlite3.connect("applications.db") con = sqlite3.connect("applications.db")
cur = con.cursor() cur = con.cursor()
cur.execute(f"SELECT application_blob FROM app_guildapp_db WHERE guild_id={guild_id}") cur.execute("SELECT applications_blob FROM app_guildapp_db WHERE guild_id=(?)", (guild_id, ))
application_blob = cur.fetchone() application_blob = cur.fetchone()
applications = pickle.loads(application_blob) applications = pickle.loads(application_blob[0])
if application_name in applications.keys(): if application_name in applications.keys():
if question_index in applications[application_name].keys(): questions = applications[application_name]["questions"]
questions = applications[application_name]["questions"]#[question_index] = new_question if question_index <= len(questions):
questions[question_index-1] = new_question questions[question_index-1] = new_question
applications[application_name]["questions"] = questions applications[application_name]["questions"] = questions
application_blob2 = pickle.dumps(applications) application_blob2 = pickle.dumps(applications)
cur.execute(f"UPDATE app_guildapp_db SET application_blob = ? WHERE guild_id={guild_id}", application_blob2) cur.execute("UPDATE app_guildapp_db SET applications_blob = (?) WHERE guild_id= (?)", (application_blob2, guild_id))
con.commit() con.commit()
return "success" return "success"
else: else:
@ -140,16 +159,23 @@ class GuildAppDB():
def move_question(guild_id: str, application_name: str, init_que_index: int, fin_que_index: int): def move_question(guild_id: str, application_name: str, init_que_index: int, fin_que_index: int):
con = sqlite3.connect("applications.db") con = sqlite3.connect("applications.db")
cur = con.cursor() cur = con.cursor()
cur.execute(f"SELECT application_blob FROM app_guildapp_db WHERE guild_id={guild_id}") cur.execute("SELECT applications_blob FROM app_guildapp_db WHERE guild_id=(?)", (guild_id, ))
application_blob = cur.fetchone() application_blob = cur.fetchone()
applications = pickle.loads(application_blob) applications = pickle.loads(application_blob[0])
if application_name in applications.keys(): if application_name in applications.keys():
questions = applications[application_name]["questions"] questions = applications[application_name]["questions"]
if init_que_index <= len(questions) and fin_que_index <= len(questions): if init_que_index <= len(questions) and fin_que_index <= len(questions):
questions.insert(init_que_index-1, questions.pop(fin_que_index-1)) if init_que_index > fin_que_index:
questions.insert(fin_que_index-1, questions[init_que_index-1])
questions.pop(init_que_index)
elif init_que_index < fin_que_index:
questions.insert(fin_que_index, questions[init_que_index-1])
questions.pop(init_que_index-1)
else:
return "error on move question: init and fin index equal"
applications[application_name]["questions"] = questions applications[application_name]["questions"] = questions
application_blob2 = pickle.dumps(applications) application_blob2 = pickle.dumps(applications)
cur.execute(f"UPDATE app_guildapp_db SET application_blob = ? WHERE guild_id={guild_id}", application_blob2) cur.execute("UPDATE app_guildapp_db SET applications_blob = (?) WHERE guild_id= (?)", (application_blob2, guild_id))
con.commit() con.commit()
return "success" return "success"
else: else:
@ -160,16 +186,16 @@ class GuildAppDB():
def remove_question(guild_id: str, application_name: str, question_index: int): def remove_question(guild_id: str, application_name: str, question_index: int):
con = sqlite3.connect("applications.db") con = sqlite3.connect("applications.db")
cur = con.cursor() cur = con.cursor()
cur.execute(f"SELECT application_blob FROM app_guildapp_db WHERE guild_id={guild_id}") cur.execute("SELECT applications_blob FROM app_guildapp_db WHERE guild_id=(?)", (guild_id, ))
application_blob = cur.fetchone() application_blob = cur.fetchone()
applications = pickle.loads(application_blob) applications = pickle.loads(application_blob[0])
if application_name in applications.keys(): if application_name in applications.keys():
questions = applications[application_name]["questions"] questions = applications[application_name]["questions"]
if question_index <= len(questions): if question_index <= len(questions):
questions.pop(question_index-1) questions.pop(question_index-1)
applications[application_name]["questions"] = questions applications[application_name]["questions"] = questions
application_blob2 = pickle.dumps(applications) application_blob2 = pickle.dumps(applications)
cur.execute(f"UPDATE app_guildapp_db SET application_blob = ? WHERE guild_id={guild_id}", application_blob2) cur.execute("UPDATE app_guildapp_db SET applications_blob = (?) WHERE guild_id= (?)", (application_blob2, guild_id))
con.commit() con.commit()
return "success" return "success"
else: else: