From 6a8919290b398665551adf2e14697ba373431197 Mon Sep 17 00:00:00 2001 From: Georgios Atheridis Date: Sat, 2 Apr 2022 07:01:56 +0300 Subject: [PATCH] fixed bugs and made spam to spam as much as possible --- skgyorugo/analyze_auto_message.py | 9 +++- skgyorugo/commands/commands.py | 4 -- skgyorugo/commands/spam.py | 9 ++-- skgyorugo/database_manager.py | 35 ++++++++++++---- skgyorugo/main.py | 47 +++++++++++++++------ skgyorugo/tools/smart_privmsg.py | 2 - skgyorugo/tools/smart_start_stream_time.py | 48 ++++++++++++++-------- 7 files changed, 104 insertions(+), 50 deletions(-) diff --git a/skgyorugo/analyze_auto_message.py b/skgyorugo/analyze_auto_message.py index 5031b03..44a5968 100644 --- a/skgyorugo/analyze_auto_message.py +++ b/skgyorugo/analyze_auto_message.py @@ -3,7 +3,11 @@ import os import sqlite3 import time import tools.smart_privmsg -from tools.smart_start_stream_time import start_stream_timestamp as stream_ts +import tools.smart_start_stream_time +from importlib import reload + +reload(tools.smart_privmsg) +reload(tools.smart_start_stream_time) PATH = os.path.dirname(os.path.realpath(__file__)) @@ -12,7 +16,7 @@ def do_auto_message(bot: Bot, message: Message, auto_message_modules: dict): conn = sqlite3.connect(os.path.join(PATH, "database.db")) c = conn.cursor() - start_stream_ts = stream_ts() + start_stream_ts = tools.smart_start_stream_time.start_stream_timestamp() if not start_stream_ts: return @@ -54,4 +58,5 @@ def do_auto_message(bot: Bot, message: Message, auto_message_modules: dict): ) ) conn.commit() + time.sleep(1.5) conn.close() diff --git a/skgyorugo/commands/commands.py b/skgyorugo/commands/commands.py index c678858..d2cf437 100644 --- a/skgyorugo/commands/commands.py +++ b/skgyorugo/commands/commands.py @@ -23,8 +23,6 @@ def main(bot: Bot, message: Message): message.tags["user-id"] ) - print(f"user perm is: {user_perm}") - c.execute( "SELECT prefix, command FROM commands WHERE permission >= ? ORDER BY permission ASC", ( @@ -34,8 +32,6 @@ def main(bot: Bot, message: Message): fetched_commands = c.fetchall() - print(f"fetched commands is: {fetched_commands}") - conn.commit() conn.close() diff --git a/skgyorugo/commands/spam.py b/skgyorugo/commands/spam.py index 13c8d20..cdb5588 100644 --- a/skgyorugo/commands/spam.py +++ b/skgyorugo/commands/spam.py @@ -7,9 +7,12 @@ DESCRIPTION = "" USER_COOLDOWN = 10 GLOBAL_COOLDOWN = 0 +MAX_LENGTH = 469 + def main(bot: Bot, message: Message): msg = ' '.join(message.value.split(' ')[1:]) - msg = (msg + ' ') * 10 - tools.smart_privmsg.send(bot, message, msg) - # bot.send_privmsg(message.channel, msg) + new_msg = "" + while len(new_msg) + len(msg) > MAX_LENGTH: + new_msg += msg + " " + bot.send_privmsg(message.channel, msg) diff --git a/skgyorugo/database_manager.py b/skgyorugo/database_manager.py index 98dbae9..41b72c7 100644 --- a/skgyorugo/database_manager.py +++ b/skgyorugo/database_manager.py @@ -104,7 +104,7 @@ def create_database(): try: c.execute( """ - CREATE TABLE streamer_info ( + CREATE TABLE stream_info ( start_stream_ts INTEGER NOT NULL, last_checked INTEGER NOT NULL, ended INTEGER NOT NULL, @@ -156,14 +156,31 @@ def update_auto_messages_in_database(modules, auto_messages): auto_message_cooldown = modules[auto_message_name].COOLDOWN auto_message_end_time = modules[auto_message_name].END_TIME auto_message_last_used = 0 - c.execute( - "REPLACE INTO commands VALUES (?, ?, ?, ?)", - ( - auto_message_name, - auto_message_cooldown, - auto_message_end_time, - auto_message_last_used, + try: + c.execute( + "INSERT INTO auto_messages (name, cooldown, end_time, last_used) VALUES (?, ?, ?, ?)", + ( + auto_message_name, + auto_message_cooldown, + auto_message_end_time, + auto_message_last_used, + ) + ) + except Exception as e: + c.execute( + """ + UPDATE auto_messages + SET + cooldown = ?, + end_time = ? + WHERE + name = ? + """, + ( + auto_message_cooldown, + auto_message_end_time, + auto_message_name, + ) ) - ) conn.commit() conn.close() diff --git a/skgyorugo/main.py b/skgyorugo/main.py index 0908c85..0817a1e 100644 --- a/skgyorugo/main.py +++ b/skgyorugo/main.py @@ -9,6 +9,7 @@ import tools.permissions import analyze_command import scripts.unit_converter import database_manager +import analyze_auto_message from importlib import reload reload(tools.raid) @@ -17,14 +18,18 @@ reload(tools.permissions) reload(analyze_command) reload(scripts.unit_converter) reload(database_manager) +reload(analyze_auto_message) PATH = os.path.dirname(os.path.realpath(__file__)) COMMANDS_PATH = os.path.join(PATH, "commands") AUTO_MESSAGES_PATH = os.path.join(PATH, "auto_messages") -specs = {} -modules = {} +commands_specs = {} +commands_modules = {} + +auto_message_specs = {} +auto_message_modules = {} commands = [ c for c in os.listdir(COMMANDS_PATH) if os.path.isfile(os.path.join(COMMANDS_PATH, c)) @@ -33,15 +38,16 @@ commands = filter(lambda x: not x.startswith('.'), commands) commands = filter(lambda x: os.path.splitext(x)[1] == ".py", commands) commands = list(commands) for command in commands: - specs[command.split('.')[0]] = ( + commands_specs[command.split('.')[0]] = ( importlib.util.spec_from_file_location( f"{command.split('.')[0]}", os.path.join(COMMANDS_PATH, command) ) ) +print(f"my commands are {commands}") auto_messages = [ - c for c in os.listdir(COMMANDS_PATH) if os.path.isfile(os.path.join(COMMANDS_PATH, c)) + c for c in os.listdir(AUTO_MESSAGES_PATH) if os.path.isfile(os.path.join(AUTO_MESSAGES_PATH, c)) ] auto_messages = filter(lambda x: not x.startswith('.'), auto_messages) auto_messages = filter( @@ -50,19 +56,32 @@ auto_messages = filter( ) auto_messages = list(auto_messages) for auto_message in auto_messages: - specs[auto_message.split('.')[0]] = ( + auto_message_specs[auto_message.split('.')[0]] = ( importlib.util.spec_from_file_location( f"{auto_message.split('.')[0]}", - os.path.join(COMMANDS_PATH, auto_message) + os.path.join(AUTO_MESSAGES_PATH, auto_message) ) ) -for spec in specs: - modules[spec] = importlib.util.module_from_spec(specs[spec]) - if not specs[spec]: +for spec in commands_specs: + commands_modules[spec] = importlib.util.module_from_spec( + commands_specs[spec]) + if not commands_specs[spec]: + continue + try: + commands_specs[spec].loader.exec_module(commands_modules[spec]) + except Exception as e: + print() + print(traceback.format_exc()) + print(f"Problem Loading Module: {e}") + +for spec in auto_message_specs: + auto_message_modules[spec] = importlib.util.module_from_spec( + auto_message_specs[spec]) + if not auto_message_specs[spec]: continue try: - specs[spec].loader.exec_module(modules[spec]) + auto_message_specs[spec].loader.exec_module(auto_message_modules[spec]) except Exception as e: print() print(traceback.format_exc()) @@ -70,12 +89,16 @@ for spec in specs: database_manager.create_database() -database_manager.update_commands_in_database(modules, commands) +database_manager.update_commands_in_database(commands_modules, commands) +database_manager.update_auto_messages_in_database( + auto_message_modules, auto_messages) def main(bot: Bot, message: Message): if message.command == Commands.PRIVMSG: - analyze_command.do_command(bot, message, modules) + analyze_command.do_command(bot, message, commands_modules) scripts.unit_converter.send_metric(bot, message) + analyze_auto_message.do_auto_message(bot, message, auto_message_modules) + tools.raid.raid(bot, message) diff --git a/skgyorugo/tools/smart_privmsg.py b/skgyorugo/tools/smart_privmsg.py index f2276a6..2e25282 100644 --- a/skgyorugo/tools/smart_privmsg.py +++ b/skgyorugo/tools/smart_privmsg.py @@ -17,8 +17,6 @@ def _split_message(message: str) -> list[str]: def send(bot: Bot, message_data: Message, message: str): - # msg = ' '.join(message_data.value.split(' ')[1:]) - # msg = split_message(msg) for msg in _split_message(' '.join(message_data.value.split(' ')[1:])): message = message.replace("{message}", msg) message = message.replace("{nick}", message_data.nick) diff --git a/skgyorugo/tools/smart_start_stream_time.py b/skgyorugo/tools/smart_start_stream_time.py index 5218936..27aaca4 100644 --- a/skgyorugo/tools/smart_start_stream_time.py +++ b/skgyorugo/tools/smart_start_stream_time.py @@ -21,24 +21,34 @@ def start_stream_timestamp() -> Optional[int]: conn = sqlite3.connect(os.path.join(PATH, "database.db")) c = conn.cursor() - c.execute( - """ - SELECT - start_stream_ts, - last_checked, - ended - FROM - stream_info - WHERE - last_checked = (SELECT MAX(last_checked) FROM stream_info); - AND ended = 0 - """ - ) + c.execute("SELECT MAX(last_checked) FROM stream_info") + max_last_checked = c.fetchone() + if max_last_checked: + c.execute( + """ + SELECT + start_stream_ts, + last_checked, + ended + FROM + stream_info + WHERE + last_checked = ? + AND ended = 0 + """, + ( + max_last_checked[0], + ) + ) + print(f"I checked {max_last_checked}") fetched = c.fetchone() if fetched: start_stream_ts, last_checked, _ = fetched - if last_checked + CHECK_STREAMTIME_CD < time.time(): + print(f"stream ts = {start_stream_ts}") + print( + f"last_checked {last_checked} + check_streamtime_cd {CHECK_STREAMTIME_CD} \n time.time {time.time()}") + if time.time() < last_checked + CHECK_STREAMTIME_CD: return start_stream_ts stream_info = ttv_api.stream.get_streams(user_logins=[streamer_login]) @@ -47,12 +57,12 @@ def start_stream_timestamp() -> Optional[int]: if not stream_info: start_stream_ts, last_checked, _ = fetched - if last_checked + MAX_OFF_STREAM_MARGIN < time.time(): + if time.time() < last_checked + MAX_OFF_STREAM_MARGIN: conn.close() return c.execute( - "REPLACE INTO commands VALUES (?, ?, ?)", + "REPLACE INTO stream_info VALUES (?, ?, ?)", ( start_stream_ts, last_checked, @@ -65,8 +75,9 @@ def start_stream_timestamp() -> Optional[int]: if not fetched: start_stream_ts = int(stream_info[0].started_at.timestamp()) + print("TEST") c.execute( - "REPLACE INTO commands VALUES (?, ?, ?)", + "REPLACE INTO stream_info VALUES (?, ?, ?)", ( start_stream_ts, int(time.time()), @@ -77,9 +88,10 @@ def start_stream_timestamp() -> Optional[int]: conn.close() return start_stream_ts + print("MORE TEST") start_stream_ts, last_checked, _ = fetched c.execute( - "REPLACE INTO commands VALUES (?, ?, ?)", + "REPLACE INTO stream_info VALUES (?, ?, ?)", ( start_stream_ts, int(time.time()), -- 2.30.2