From: Georgios Atheridis Date: Tue, 29 Mar 2022 11:08:10 +0000 (+0300) Subject: Adding skgyorugo X-Git-Url: https://git.atheridis.org/?a=commitdiff_plain;h=86bb694373fc0c18752be1c0f6da0eb54fe87edd;p=ihaspeks%2Faptbot-ihaspeks.git Adding skgyorugo --- 86bb694373fc0c18752be1c0f6da0eb54fe87edd diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1e041e3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,132 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# sqlite databse +*.db diff --git a/skgyorugo/commands/addcommand.py b/skgyorugo/commands/addcommand.py new file mode 100644 index 0000000..ea68f87 --- /dev/null +++ b/skgyorugo/commands/addcommand.py @@ -0,0 +1,62 @@ +from aptbot.bot import Message, Commands, Bot +import sqlite3 +import os + +PERMISSION = 1 +PREFIX = '\\' +DESCRIPTION = "" +USER_COOLDOWN = 0 +GLOBAL_COOLDOWN = 0 + +PATH = os.path.dirname(os.path.realpath(__file__)) +PATH = os.path.join(PATH, "..") + +DEFAULT_USER_COOLDOWN = 5 +DEFAULT_GLOBAL_COOLDOWN = 0 + + +def main(bot: Bot, message: Message): + msg = ' '.join(message.value.split(' ')[1:]) + command = msg.split(' ')[0] + command_prefix = command[0] + command_name = command[1:] + command_value = msg = ' '.join(msg.split(' ')[1:]) + if command_prefix != '?': + bot.send_privmsg( + message.channel, + f"{message.nick} you cannot use {command_prefix} as a prefix" + ) + return + + conn = sqlite3.connect(os.path.join(PATH, "database.db")) + c = conn.cursor() + try: + c.execute( + "INSERT INTO commands VALUES (?, ?, ?, ?, ?, ?, ?)", + ( + command_name, + command_prefix, + 99, + command_value, + "", + DEFAULT_USER_COOLDOWN, + DEFAULT_GLOBAL_COOLDOWN, + ) + ) + except sqlite3.IntegrityError: + bot.send_privmsg( + message.channel, + f"The command {command_name} already exists." + ) + except Exception as e: + bot.send_privmsg( + message.channel, + f"There was an error adding the command: {e}" + ) + else: + bot.send_privmsg( + message.channel, + f"Successfully added {command_name}." + ) + conn.commit() + conn.close() diff --git a/skgyorugo/commands/commands.py b/skgyorugo/commands/commands.py new file mode 100644 index 0000000..c678858 --- /dev/null +++ b/skgyorugo/commands/commands.py @@ -0,0 +1,48 @@ +from aptbot.bot import Message, Commands, Bot +import tools.permissions +import tools.smart_privmsg +import sqlite3 +import os + +PERMISSION = 99 +PREFIX = '?' +DESCRIPTION = "" +USER_COOLDOWN = 30 +GLOBAL_COOLDOWN = 15 + + +COMMANDS_PATH = os.path.dirname(os.path.realpath(__file__)) +PATH = os.path.join(COMMANDS_PATH, "..") + + +def main(bot: Bot, message: Message): + conn = sqlite3.connect(os.path.join(PATH, "database.db")) + c = conn.cursor() + + user_perm = tools.permissions.get_permission_from_id( + message.tags["user-id"] + ) + + print(f"user perm is: {user_perm}") + + c.execute( + "SELECT prefix, command FROM commands WHERE permission >= ? ORDER BY permission ASC", + ( + user_perm, + ) + ) + + fetched_commands = c.fetchall() + + print(f"fetched commands is: {fetched_commands}") + + conn.commit() + conn.close() + + commands = [] + for command in fetched_commands: + commands.append(f"{command[0]}{command[1]}") + + commands = ' '.join(commands) + print(f"commands is: {commands}") + tools.smart_privmsg.send(bot, message, commands) diff --git a/skgyorugo/commands/editcommand.py b/skgyorugo/commands/editcommand.py new file mode 100644 index 0000000..ec35331 --- /dev/null +++ b/skgyorugo/commands/editcommand.py @@ -0,0 +1,64 @@ +from aptbot.bot import Message, Commands, Bot +import sqlite3 +import os + +PERMISSION = 1 +PREFIX = '\\' +DESCRIPTION = "" +USER_COOLDOWN = 0 +GLOBAL_COOLDOWN = 0 + + +COMMANDS_PATH = os.path.dirname(os.path.realpath(__file__)) +PATH = os.path.join(COMMANDS_PATH, "..") + + +def main(bot: Bot, message: Message): + msg = ' '.join(message.value.split(' ')[1:]) + command = msg.split(' ')[0] + command_prefix = command[0] + command_name = command[1:] + command_value = msg = ' '.join(msg.split(' ')[1:]) + if command_prefix != '?': + bot.send_privmsg( + message.channel, + f"{message.nick} you cannot use {command_prefix} as a prefix" + ) + return + + conn = sqlite3.connect(os.path.join(PATH, "database.db")) + c = conn.cursor() + c.execute( + "SELECT value FROM commands WHERE command = ? AND prefix = ?", + ( + command_name, + command_prefix, + ) + ) + if not c.fetchone()[0]: + bot.send_privmsg( + message.channel, + f"The command {command_prefix}{command_name} cannot be edited" + ) + return + + try: + c.execute( + "UPDATE commands SET value = ? WHERE command = ?", + ( + command_value, + command_name, + ) + ) + except sqlite3.IntegrityError: + bot.send_privmsg( + message.channel, + f"The command {command_name} doesn't exist." + ) + else: + bot.send_privmsg( + message.channel, + f"Successfully updated {command_name}." + ) + conn.commit() + conn.close() diff --git a/skgyorugo/commands/emotes.py b/skgyorugo/commands/emotes.py new file mode 100644 index 0000000..ed4c7b6 --- /dev/null +++ b/skgyorugo/commands/emotes.py @@ -0,0 +1,45 @@ +from aptbot.bot import Message, Commands, Bot +import tools.smart_privmsg +import urllib3 +import os + +PERMISSION = 99 +PREFIX = '?' +DESCRIPTION = "" +USER_COOLDOWN = 90 +GLOBAL_COOLDOWN = 60 + +OAUTH = os.getenv("APTBOT_OAUTH") +CLIENT_ID = os.getenv("APTBOT_CLIENT_ID") + +HEADER = { + "Authorization": f"Bearer {OAUTH}", + "Client-Id": f"{CLIENT_ID}", + "Content-Type": "application/json", +} + + +def main(bot: Bot, message: Message): + http = urllib3.PoolManager() + r1 = http.request( + "GET", + f"https://twitch.center/customapi/bttvemotes?channel={message.channel}", + headers=HEADER, + ) + r2 = http.request( + "GET", + f"https://twitch.center/customapi/ffzemotes?channel={message.channel}", + headers=HEADER, + ) + + if r1.status != 200 or r2.status != 200: + bot.send_privmsg( + message.channel, + "NotLikeThis oopsie woopsie, we've made a fucky wucky. We can't \ + get the emotes at the moment. Hopefuwwy UwU wiww wait patientwy \ + tiww we get thiws pwobwem sowted. OwO" + ) + return + emotes = r1.data.decode("utf-8") + emotes += " " + r2.data.decode("utf-8") + tools.smart_privmsg.send(bot, message, emotes) diff --git a/skgyorugo/commands/removecommand.py b/skgyorugo/commands/removecommand.py new file mode 100644 index 0000000..d41a26d --- /dev/null +++ b/skgyorugo/commands/removecommand.py @@ -0,0 +1,60 @@ +from aptbot.bot import Message, Commands, Bot +import sqlite3 +import os + +PERMISSION = 1 +PREFIX = '\\' +DESCRIPTION = "" +USER_COOLDOWN = 0 +GLOBAL_COOLDOWN = 0 + +COMMANDS_PATH = os.path.dirname(os.path.realpath(__file__)) +PATH = os.path.join(COMMANDS_PATH, "..") + + +def main(bot: Bot, message: Message): + msg = ' '.join(message.value.split(' ')[1:]) + command = msg.split(' ')[0] + command_prefix = command[0] + command_name = command[1:] + + conn = sqlite3.connect(os.path.join(PATH, "database.db")) + c = conn.cursor() + c.execute( + "SELECT value FROM commands WHERE command = ? AND prefix = ?", + ( + command_name, + command_prefix, + ) + ) + command_path = os.path.join(COMMANDS_PATH, f"{command_name}.py") + hidden_command_path = os.path.join(COMMANDS_PATH, f".{command_name}.py") + try: + if not c.fetchone()[0]: + try: + os.rename(command_path, hidden_command_path) + except FileNotFoundError: + pass + except TypeError: + pass + + try: + c.execute( + "DELETE FROM commands WHERE command = ? AND prefix = ?", + ( + command_name, + command_prefix, + ) + ) + except sqlite3.IntegrityError: + bot.send_privmsg( + message.channel, + f"The command {command_name} doesn't exist." + ) + else: + bot.send_privmsg( + message.channel, + f"Successfully removed {command_name}." + ) + conn.commit() + conn.close() diff --git a/skgyorugo/commands/scam.py b/skgyorugo/commands/scam.py new file mode 100644 index 0000000..b983db4 --- /dev/null +++ b/skgyorugo/commands/scam.py @@ -0,0 +1,12 @@ +from aptbot.bot import Message, Commands, Bot + +PERMISSION = 99 +PREFIX = '?' +DESCRIPTION = "" +USER_COOLDOWN = 10 +GLOBAL_COOLDOWN = 10 + + +def main(bot: Bot, message: Message): + msg = message.nick + " you have been scammed KEKW" + bot.send_privmsg(message.channel, msg) diff --git a/skgyorugo/commands/spam.py b/skgyorugo/commands/spam.py new file mode 100644 index 0000000..13c8d20 --- /dev/null +++ b/skgyorugo/commands/spam.py @@ -0,0 +1,15 @@ +from aptbot.bot import Message, Commands, Bot +import tools.smart_privmsg + +PERMISSION = 99 +PREFIX = '?' +DESCRIPTION = "" +USER_COOLDOWN = 10 +GLOBAL_COOLDOWN = 0 + + +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) diff --git a/skgyorugo/main.py b/skgyorugo/main.py new file mode 100644 index 0000000..312be30 --- /dev/null +++ b/skgyorugo/main.py @@ -0,0 +1,187 @@ +import tools.raid +import tools.smart_privmsg +import tools.permissions +from aptbot.bot import Bot, Message, Commands +import os +import importlib +import importlib.util +import sqlite3 +from importlib import reload +import traceback +import ttv_api.users + +reload(tools.raid) +reload(tools.smart_privmsg) +reload(tools.permissions) + +PATH = os.path.dirname(os.path.realpath(__file__)) +COMMANDS_PATH = os.path.join(PATH, "commands") + + +commands = [ + c for c in os.listdir(COMMANDS_PATH) if os.path.isfile(os.path.join(COMMANDS_PATH, c)) +] +specs = {} +for command in commands: + if not command.split('.')[0]: + continue + specs[command.split('.')[0]] = ( + importlib.util.spec_from_file_location( + f"{command.split('.')[0]}", + os.path.join(COMMANDS_PATH, command) + ) + ) + +modules = {} +for command in specs: + modules[command] = importlib.util.module_from_spec(specs[command]) + if not specs[command]: + continue + try: + specs[command].loader.exec_module(modules[command]) + except Exception as e: + print() + print(traceback.format_exc()) + print(f"Problem Loading Module: {e}") + + +def create_database(): + conn = sqlite3.connect(os.path.join(PATH, "database.db")) + c = conn.cursor() + try: + c.execute("""CREATE TABLE commands ( + command text PRIMARY KEY, + prefix text, + permission integer, + value text, + description text, + user_cooldown int, + global_cooldown int + )""") + except sqlite3.OperationalError: + print("Table commands exists") + + try: + c.execute("""CREATE TABLE users ( + user_id text PRIMARY KEY, + permission integer + )""") + except sqlite3.OperationalError: + print("Table users exists") + else: + aptbot_id = ttv_api.users.get_users(user_logins=["skgyorugo"]) + if aptbot_id: + c.execute("INSERT INTO users VALUES (?, ?)", + (aptbot_id[0].user_id, 0)) + + try: + c.execute("""CREATE TABLE cooldowns ( + user_id text, + command text, + prefix text, + user_cooldown integer, + global_cooldown integer, + PRIMARY KEY (user_id, command) + )""") + except sqlite3.OperationalError: + print("Table cooldowns exists") + + conn.commit() + conn.close() + + +def update_commands_in_database(): + conn = sqlite3.connect(os.path.join(PATH, "database.db")) + c = conn.cursor() + + for command in commands: + command_name = command.split('.')[0] + command_permission = modules[command_name].PERMISSION + command_prefix = modules[command_name].PREFIX + command_description = modules[command_name].DESCRIPTION + command_user_cooldown = modules[command_name].USER_COOLDOWN + command_global_cooldown = modules[command_name].GLOBAL_COOLDOWN + # try: + c.execute( + "REPLACE INTO commands VALUES (?, ?, ?, ?, ?, ?, ?)", + ( + command_name, + command_prefix, + command_permission, + None, + command_description, + command_user_cooldown, + command_global_cooldown, + ) + ) + conn.commit() + conn.close() + + +create_database() +update_commands_in_database() + + +def main(bot: Bot, message: Message): + if message.command == Commands.PRIVMSG: + conn = sqlite3.connect(os.path.join(PATH, "database.db")) + c = conn.cursor() + command = message.value.split(' ')[0] + prefix = command[0] + command = command[1:] + c.execute("SELECT * FROM commands WHERE command = ?", (command,)) + fetched_command = c.fetchone() + user_perm = tools.permissions.get_permission_from_id( + message.tags["user-id"] + ) + message_timestamp = int(message.tags["tmi-sent-ts"]) // 1000 + if fetched_command and prefix == fetched_command[1] and user_perm <= fetched_command[2]: + c.execute( + "SELECT global_cooldown FROM cooldowns WHERE command = ? ORDER BY global_cooldown DESC", + (command, ) + ) + try: + fetched_global_cooldown = c.fetchone()[0] + except TypeError: + fetched_global_cooldown = 0 + c.execute( + "SELECT user_cooldown FROM cooldowns WHERE user_id = ? AND command = ?", + (message.tags["user-id"], command) + ) + try: + fetched_user_cooldown = c.fetchone()[0] + except TypeError: + fetched_user_cooldown = 0 + cooldown = max(fetched_global_cooldown, fetched_user_cooldown) + if message_timestamp > cooldown: + c.execute( + "SELECT user_cooldown, global_cooldown FROM commands WHERE command = ?", + (command, ) + ) + user_cooldown, global_cooldown = c.fetchone() + c.execute( + "REPLACE INTO cooldowns VALUES (?, ?, ?, ?, ?)", + ( + message.tags["user-id"], + command, + prefix, + user_cooldown + message_timestamp, + global_cooldown + message_timestamp, + ) + ) + conn.commit() + if not fetched_command[3]: + modules[command].main(bot, message) + elif fetched_command[3]: + tools.smart_privmsg.send(bot, message, fetched_command[3]) + else: + bot.send_privmsg( + message.channel, + f"The command '{prefix}{command}' is on cooldown. \ + Please wait {int(cooldown - message_timestamp) + 1} seconds." + ) + + conn.commit() + conn.close() + + tools.raid.raid(bot, message) diff --git a/skgyorugo/tools/permissions.py b/skgyorugo/tools/permissions.py new file mode 100644 index 0000000..bf496a0 --- /dev/null +++ b/skgyorugo/tools/permissions.py @@ -0,0 +1,18 @@ +import os +import sqlite3 + +TOOLS_PATH = os.path.dirname(os.path.realpath(__file__)) +PATH = os.path.join(TOOLS_PATH, "..") + +DEFAULT_PERMISSION = 99 + + +def get_permission_from_id(user_id: str) -> int: + conn = sqlite3.connect(os.path.join(PATH, "database.db")) + c = conn.cursor() + c.execute("SELECT permission FROM users WHERE user_id = ?", (user_id,)) + fetched_user = c.fetchone() + if fetched_user: + return fetched_user[0] + + return DEFAULT_PERMISSION diff --git a/skgyorugo/tools/raid.py b/skgyorugo/tools/raid.py new file mode 100644 index 0000000..f16eb16 --- /dev/null +++ b/skgyorugo/tools/raid.py @@ -0,0 +1,18 @@ +from aptbot.bot import Bot, Message, Commands + + +def raid(bot: Bot, message: Message): + if message.command == Commands.USERNOTICE: + if message.tags["msg-id"] == "raid": + raider_name = message.tags["msg-param-displayName"] + raider_login = message.tags["msg-param-login"] + raider_id = message.tags["user-id"] + raider_game = "" + if raider_id: + raider_channel_info = "channel info here" + viewers = message.tags["msg-param-viewerCount"] + viewers = f"{viewers} viewer" if viewers == "1" else f"{viewers} viewers" + msg_reply = f"POGGERS {raider_name} has raided {message.channel} with {viewers}!!! Why don\'t you check them out at https://twitch.tv/{raider_login}" + if raider_game: + msg_reply += f' they were just playing {raider_game}.' + bot.send_privmsg(message.channel, msg_reply) diff --git a/skgyorugo/tools/smart_privmsg.py b/skgyorugo/tools/smart_privmsg.py new file mode 100644 index 0000000..f2276a6 --- /dev/null +++ b/skgyorugo/tools/smart_privmsg.py @@ -0,0 +1,27 @@ +from aptbot.bot import Bot, Message, Commands + +MAX_LENGTH = 469 + + +def _split_message(message: str) -> list[str]: + split_count = len(message) // MAX_LENGTH + 1 + words = message.split(' ') + word_list = [''] * split_count + index = 0 + for word in words: + if len(word_list[index]) >= MAX_LENGTH: + index += 1 + word_list[index] += word + ' ' + + return word_list + + +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) + message = message.replace("{channel}", message_data.channel) + messages = _split_message(message) + bot.send_privmsg(message_data.channel, messages)