From 4a8233cd1759bd6451f999fea3a01feda560b33f Mon Sep 17 00:00:00 2001 From: Georgios Atheridis Date: Thu, 5 May 2022 18:40:35 +0000 Subject: [PATCH] created lol account database --- skgyorugo/commands/addaccount.py | 74 ++++++++++++++++++++++++++++++++ skgyorugo/commands/youtube.py | 2 +- skgyorugo/database_manager.py | 21 +++++++++ skgyorugo/main.py | 1 + 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 skgyorugo/commands/addaccount.py diff --git a/skgyorugo/commands/addaccount.py b/skgyorugo/commands/addaccount.py new file mode 100644 index 0000000..8b292d0 --- /dev/null +++ b/skgyorugo/commands/addaccount.py @@ -0,0 +1,74 @@ +from aptbot.bot import Message, Commands, Bot +import sqlite3 +import os +import logging +import lol_api.summoner_v4 +import ttv_api.users + +logger = logging.getLogger(__name__) +logger.setLevel(logging.DEBUG) + +formatter = logging.Formatter("[%(levelname)s] %(asctime)s: %(name)s; %(message)s") + +file_handler = logging.FileHandler('/var/log/aptbot/logs.log') +file_handler.setFormatter(formatter) + +logger.handlers = [] +logger.addHandler(file_handler) + + +PERMISSION = 10 +PREFIX = '\\' +DESCRIPTION = "Adds a LoL account to the database. Use: \\addaccount | " +USER_COOLDOWN = 0 +GLOBAL_COOLDOWN = 0 + +PATH = os.path.dirname(os.path.realpath(__file__)) +PATH = os.path.join(PATH, "..") + + +def main(bot: Bot, message: Message): + msg = ' '.join(message.value.split(' ')[1:]) + summoner_name, twitch_name = msg.split('|') + summoner = lol_api.summoner_v4.get_summoner_from_name(summoner_name) + twitch = ttv_api.users.get_users(user_logins=[twitch_name]) + if summoner is None: + logger.warning(f"Account {summoner_name} wasn't able to be added") + bot.send_privmsg(message.channel, f"Error, unable to add summoner: {summoner_name}") + return + if twitch is None: + logger.warning(f"Unable to use twitch account {twitch_name}") + bot.send_privmsg(message.channel, f"Error, unable to use twitch account: {twitch_name}") + return + twitch_id = twitch[0].user_id + + conn = sqlite3.connect(os.path.join(PATH, "lol_data.db")) + c = conn.cursor() + + try: + c.execute( + """ + INSERT INTO accounts ( + "puuid", + "summoner_id", + "account_id", + "twitch_id" + ) VALUES ( + ?, ?, ?, ? + ); + """, + ( + summoner.puuid, + summoner.summoner_id, + summoner.account_id, + twitch_id, + ) + ) + except sqlite3.IntegrityError: + logger.warning(f"Unable to add account with puuid: {summoner.puuid} and twitch id: {twitch_id}. Account probably already exists") + bot.send_privmsg(message.channel, f"Account already exists.") + return + conn.commit() + logger.info(f"Successfully added account with puuid: {summoner.puuid} and twitch id: {twitch_id}") + bot.send_privmsg(message.channel, f"Successfully added account.") + conn.close() diff --git a/skgyorugo/commands/youtube.py b/skgyorugo/commands/youtube.py index b034c29..afe4a6c 100644 --- a/skgyorugo/commands/youtube.py +++ b/skgyorugo/commands/youtube.py @@ -14,7 +14,7 @@ def main(bot: Bot, message: Message): video = yt_api.videos.get_newest_video(CHANNEL_ID) if video: video_link = f"https://www.youtube.com/watch?v={video.video_id}" - msg = f"Watch Peks' latest video \" {video.video_name} \" here: {video_link}" + msg = f"Watch Peks' latest video \"{video.video_name}\" here: {video_link}" else: msg = f"Check out my youtube channel here -> https://www.youtube.com/channel/{CHANNEL_ID}" bot.send_privmsg(message.channel, msg) diff --git a/skgyorugo/database_manager.py b/skgyorugo/database_manager.py index e28c916..f4dd676 100644 --- a/skgyorugo/database_manager.py +++ b/skgyorugo/database_manager.py @@ -71,6 +71,27 @@ def create_variables_db(): conn.commit() conn.close() +def create_lol_database(): + db_name_database = "lol_data.db" + conn = sqlite3.connect(os.path.join(PATH, db_name_database)) + c = conn.cursor() + logger.info(f"connected to database {db_name_database}") + + c.execute( + """ + CREATE TABLE IF NOT EXISTS accounts ( + puuid TEXT NOT NULL, + summoner_id TEXT NOT NULL, + account_id TEXT NOT NULL, + twitch_id INTEGER, + PRIMARY KEY (puuid) + ) + """ + ) + logger.info(f"created table accounts") + + conn.commit() + conn.close() def create_database(): db_name_database = "database.db" diff --git a/skgyorugo/main.py b/skgyorugo/main.py index ba7189e..525f8df 100644 --- a/skgyorugo/main.py +++ b/skgyorugo/main.py @@ -107,6 +107,7 @@ for spec in auto_message_specs: database_manager.create_database() +database_manager.create_lol_database() database_manager.create_variables_db() database_manager.create_chat_history_database() database_manager.update_commands_in_database(commands_modules, commands) -- 2.30.2