From c74e259d61515e2545ea253688f7b859a21156cd Mon Sep 17 00:00:00 2001 From: Anorak_1 Date: Thu, 17 Aug 2023 23:28:04 +0200 Subject: [PATCH] =?UTF-8?q?first=20commit=20for=20Application=20Actions?= =?UTF-8?q?=E2=84=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbutil.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/dbutil.py b/dbutil.py index 1ed09fd..b23143d 100644 --- a/dbutil.py +++ b/dbutil.py @@ -3,6 +3,8 @@ import os import sqlite3 import pickle +from action import Action + class MessageDB(): def add_application_msg(msg_id: str, author_id: str, guild_id: str) -> None: @@ -81,7 +83,8 @@ class GuildAppDB(): applications[application_name] = { "app_id": "", "resp_channel": "", - "questions": [] + "questions": [], + "actions": [] } application_blob2 = pickle.dumps(applications) cur.execute("UPDATE app_guildapp_db SET applications_blob = (?) WHERE guild_id= (?)", (application_blob2, guild_id)) @@ -239,4 +242,56 @@ class GuildAppDB(): else: return "error on remove question: question index not found" else: - return "error on remove question: application not found" \ No newline at end of file + return "error on remove question: application not found" + + + + + def add_action(guild_id: str, application_name: str, action: Action) -> 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(): + action_index = int(len(applications[application_name]["actions"])) + applications[application_name]["actions"].append(action) + application_blob2 = pickle.dumps(applications) + cur.execute("UPDATE app_guildapp_db SET applications_blob = (?) WHERE guild_id= (?)", (application_blob2, guild_id)) + con.commit() + return "success" + else: + return "error on add action: application not found" + + def get_actions(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(): + actions = applications[application_name]["actions"] + return actions + else: + return "error on get actions: application not found" + + def remove_action(guild_id: str, application_name: str, action_index: int): + 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(): + actions = applications[application_name]["actions"] + if action_index <= len(actions): + actions.pop(action_index-1) + applications[application_name]["actions"] = actions + application_blob2 = pickle.dumps(applications) + cur.execute("UPDATE app_guildapp_db SET applications_blob = (?) WHERE guild_id= (?)", (application_blob2, guild_id)) + con.commit() + return "success" + else: + return "error on remove action: action index not found" + else: + return "error on remove action: application not found" +