diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..72b09a0f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: python +python: + - "3.6" +install: + - pip install -r requirements.txt +script: + - python3.6 -m py_compile grandbot.py \ No newline at end of file diff --git a/README.md b/README.md index 46eb6acb..ad271be2 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Royal Bot the Third +[![Build Status](https://travis-ci.org/Steffo99/royal-bot-the-third.svg?branch=master)](https://travis-ci.org/Steffo99/royal-bot-the-third) + A _(not yet)_ modular multiservice bot written in **Python 3.6**! Development has just started, so it isn't ready yet for use. diff --git a/database.py b/database.py index 98ed61f8..4101c534 100644 --- a/database.py +++ b/database.py @@ -1,3 +1,4 @@ +import sqlalchemy.exc from sqlalchemy import create_engine, Column, Integer, String, Boolean from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base @@ -91,30 +92,7 @@ def login(username, password, enable_exceptions=False): def init_royal_db(): - create_user("steffo", "uno", True) - create_user("adry", "due", True) - create_user("cate", "tre", True) - create_user("protoh", "quattro", True) - create_user("infopz", "cinque", True) - create_user("kappa", "sei", True) - create_user("manu", "sette", True) - create_user("frank", "otto", True) - create_user("paltri", "nove", True) - create_user("mestakes", "dieci", True) - create_user("tauei", "undici", True) - create_user("sensei", "dodici", True) - create_user("gattopardo", "tredici", True) - create_user("dima", "quattordici", True) - create_user("spaggia", "quindici", True) - create_user("igor", "sedici", True) - create_user("nemesis", "diciassette", True) - create_user("comiso", "diciotto", True) - create_user("fulz", "diciannove", True) - create_user("dailir", "venti", True) - create_user("fedececco", "ventuno", True) - create_user("albertwerk", "ventidue", True) - create_user("voltaggio", "ventitre", True) - create_user("doc", "ventiquattro", True) + create_user("test", "test", True) session = Session() # Generate the database if it's empty diff --git a/grandbot.py b/grandbot.py index cddf3bf2..ca39f58a 100644 --- a/grandbot.py +++ b/grandbot.py @@ -32,6 +32,8 @@ def currently_logged_in(thing): async def start_telegram(bot, update, arguments): + # Set status to typing + await update.message.chat.set_chat_action(bot, "typing") user = currently_logged_in(update) if user is None: await update.message.reply(bot, f"Ciao!\n_Non hai eseguito l'accesso al RYGdb._", parse_mode="Markdown") @@ -47,6 +49,8 @@ async def diario_telegram(bot, update, arguments): Devi essere un Royal per poter eseguire questo comando. Sintassi: `/diario `""" + # Set status to typing + await update.message.chat.set_chat_action(bot, "typing") # Check if the user is logged in if not currently_logged_in(update): await update.message.reply(bot, "⚠ Non hai ancora eseguito l'accesso! Usa `/sync`.", parse_mode="Markdown") @@ -121,6 +125,8 @@ async def leggi_telegram(bot, update, arguments): Puoi visualizzare il diario [qui](https://royal.steffo.me/diario.htm), leggere una frase casuale scrivendo `/leggi random` o leggere una frase specifica scrivendo `/leggi `. Sintassi: `/leggi `""" + # Set status to typing + await update.message.chat.set_chat_action(bot, "typing") if len(arguments) == 0 or len(arguments) > 1: await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/leggi `", parse_mode="Markdown") return @@ -152,9 +158,9 @@ Sintassi: `/leggi `""" async def leggi_discord(bot, message, arguments): """Leggi una frase dal diario Royal Games. - Puoi visualizzare il diario [qui](https://royal.steffo.me/diario.htm), leggere una frase casuale scrivendo `/leggi random` o leggere una frase specifica scrivendo `/leggi `. +Puoi visualizzare il diario [qui](https://royal.steffo.me/diario.htm), leggere una frase casuale scrivendo `/leggi random` o leggere una frase specifica scrivendo `/leggi `. - Sintassi: `!leggi `""" +Sintassi: `!leggi `""" if len(arguments) == 0 or len(arguments) > 1: await bot.send_message(message.channel, "⚠ Sintassi del comando non valida.\n`!leggi `") return @@ -190,6 +196,8 @@ Puoi specificare con che parole (massimo 2) deve iniziare la frase generata. Se non vengono specificate, verrà scelta una parola a caso. Sintassi: `/markov [inizio]`""" + # Set status to typing + await update.message.chat.set_chat_action(bot, "typing") if len(arguments) > 2: await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/markov [inizio]`") file = open("diario.txt", "r", encoding="utf8") @@ -225,6 +233,8 @@ async def help_telegram(bot, update, arguments): """Visualizza la descrizione di un comando. Sintassi: `/help [comando]`""" + # Set status to typing + await update.message.chat.set_chat_action(bot, "typing") if len(arguments) == 0: await update.message.reply(bot, help.__doc__, parse_mode="Markdown") elif len(arguments) > 1: @@ -255,6 +265,8 @@ async def discord_telegram(bot, update, arguments): """Manda un messaggio a #chat di Discord. Sintassi: `/discord `""" + # Set status to typing + await update.message.chat.set_chat_action(bot, "typing") # Try to login logged_user = currently_logged_in(update) # Check if the user is logged in @@ -309,6 +321,8 @@ async def sync_telegram(bot, update, arguments): """Connetti il tuo account Telegram al Database Royal Games. Sintassi: `/sync `""" + # Set status to typing + await update.message.chat.set_chat_action(bot, "typing") if len(arguments) != 2: await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/sync `", parse_mode="Markdown") return @@ -357,6 +371,8 @@ async def changepassword_telegram(bot, update, arguments): """Cambia la tua password del Database Royal Games. Sintassi: `/changepassword `""" + # Set status to typing + await update.message.chat.set_chat_action(bot, "typing") if len(arguments) != 2: await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/changepassword `", parse_mode="Markdown") return @@ -375,6 +391,8 @@ async def cv_telegram(bot, update, arguments): """Visualizza lo stato attuale della chat vocale Discord. Sintassi: `/cv`""" + # Set status to typing + await update.message.chat.set_chat_action(bot, "typing") if len(arguments) != 0: await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/cv`", parse_mode="Markdown") return @@ -489,17 +507,53 @@ Sintassi: /buycoins""" f"NOTA LEGALE: @Steffo non si assume responsabilità per il contenuto delle app sponsorizzate. Fate attenzione!", parse_mode="Markdown") +async def addcoins_telegram(bot, update, arguments): + """Aggiungi ℝ a un utente! + +Devi essere un Royal per eseguire questo comando. + +Sintassi: `/addcoins `""" + # Check if the user is logged in + if not currently_logged_in(update): + await update.message.reply(bot, "⚠ Non hai ancora eseguito l'accesso! Usa `/sync`.", parse_mode="Markdown") + return + # Check if the currently logged in user is a Royal Games member + if not currently_logged_in(update).royal: + await update.message.reply(bot, "⚠ Non sei autorizzato a eseguire questo comando.") + return + # Check the command syntax + if len(arguments) != 2: + await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/addcoins `", parse_mode="Markdown") + return + # Create a new database session + session = database.Session() + # Find the user + user = session.query(database.User).filter_by(username=arguments[0]).first() + # Check if the user exists + if user is None: + await update.message.reply(bot, "⚠ L'utente specificato non esiste.") + return + # Add coins + user.coins += int(arguments[1]) + # Save the change + session.commit() + # Answer on Telegram + await update.message.reply(bot, f"✅ Hai aggiunto {arguments[1]} ℝ a {user.username}.", parse_mode="Markdown") + + async def roll_telegram(bot, update, arguments): """Lancia un dado a N facce. Sintassi: `/roll `""" + # Set status to typing + await update.message.chat.set_chat_action(bot, "typing") # Check the command syntax if len(arguments) != 1: await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/roll `", parse_mode="Markdown") return # Roll the dice! - await update.message.reply(bot, f"*Numero generato:* {random.randrange(0, int(arguments[0])) + 1}") + await update.message.reply(bot, f"*Numero generato:* {random.randrange(0, int(arguments[0])) + 1}", parse_mode="Markdown") async def roll_discord(bot, message, arguments): @@ -514,6 +568,70 @@ Sintassi: `!roll `""" await bot.send_message(message.channel, f"*Numero generato:* {random.randrange(0, int(arguments[0])) + 1}") +async def adduser_telegram(bot, update, arguments): + """Aggiungi un utente al database Royal Games! + +Devi essere un Royal per poter eseguire questo comando. + +Sintassi: `/adduser `""" + # Check if the user is logged in + if not currently_logged_in(update): + await update.message.reply(bot, "⚠ Non hai ancora eseguito l'accesso! Usa `/sync`.", parse_mode="Markdown") + return + # Check if the currently logged in user is a Royal Games member + if not currently_logged_in(update).royal: + await update.message.reply(bot, "⚠ Non sei autorizzato a eseguire questo comando.") + return + # Check the command syntax + if len(arguments) != 2: + await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/adduser `", parse_mode="Markdown") + return + # Try to create a new user + try: + database.create_user(arguments[0], arguments[1], False) + except database.sqlalchemy.exc.DBAPIError: + await update.message.reply(bot, "⚠ Qualcosa è andato storto nella creazione dell'utente. Per altre info, guarda i log del bot.") + raise + else: + await update.message.reply(bot, "✅ Creazione riuscita!") + + +async def toggleroyal_telegram(bot, update, arguments): + """Inverti lo stato di Royal di un utente. + +Devi essere un Royal per poter eseguire questo comando. + +Sintassi: `/toggleroyal `""" + # Check if the user is logged in + if not currently_logged_in(update): + await update.message.reply(bot, "⚠ Non hai ancora eseguito l'accesso! Usa `/sync`.", parse_mode="Markdown") + return + # Check if the currently logged in user is a Royal Games member + if not currently_logged_in(update).royal: + await update.message.reply(bot, "⚠ Non sei autorizzato a eseguire questo comando.") + return + # Check the command syntax + if len(arguments) != 1: + await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/toggleroyal `", parse_mode="Markdown") + return + # Create a new database session + session = database.Session() + # Find the user + user = session.query(database.User).filter_by(username=arguments[0]).first() + # Check if the user exists + if user is None: + await update.message.reply(bot, "⚠ L'utente specificato non esiste.") + return + # Toggle his Royal status + user.royal = not user.royal + # Save the change + session.commit() + # Answer on Telegram + if user.royal: + await update.message.reply(bot, f"✅ L'utente `{user.username}` ora è un Royal.", parse_mode="Markdown") + else: + await update.message.reply(bot, f"✅ L'utente `{user.username}` non è più un Royal.", parse_mode="Markdown") + if __name__ == "__main__": # Init Telegram bot commands b.commands["start"] = start_telegram @@ -526,13 +644,24 @@ if __name__ == "__main__": b.commands["markov"] = markov_telegram b.commands["cv"] = cv_telegram b.commands["roll"] = roll_telegram + b.commands["adduser"] = adduser_telegram + b.commands["toggleroyal"] = toggleroyal_telegram b.commands["buycoins"] = buycoins_telegram + b.commands["addcoins"] = addcoins_telegram # Init Discord bot commands d.commands["sync"] = sync_discord d.commands["roll"] = roll_discord d.commands["help"] = help_discord d.commands["leggi"] = leggi_discord d.commands["diario"] = diario_discord + # Set coins to 0 if None in inventory + session = database.Session() + users = session.query(database.User).all() + for user in users: + if user.coins is None: + user.coins = 0 + session.commit() + del session # Init Telegram bot loop.create_task(b.run()) print("Telegram bot start scheduled!") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..c01fd0c2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +aiohttp +async_timeout +markovify +discord.py[voice] +sqlalchemy +bcrypt \ No newline at end of file diff --git a/telegram.py b/telegram.py index e2ac9cda..d51b5085 100644 --- a/telegram.py +++ b/telegram.py @@ -5,7 +5,11 @@ import datetime class TelegramAPIError(Exception): - pass + def __init__(self, code, description): + # Error code + self.code = code + # Error description + self.description = description class UpdateError(Exception): @@ -142,14 +146,8 @@ class Bot: # Parse the json data as soon it's ready data = await response.json() # Check for errors in the request - if "description" in data: - error = data["description"] - if response.status != 200: - raise TelegramAPIError(f"Request returned {response.status} {response.reason}") - # Check for errors in the response - if not data["ok"]: - error = data["description"] - raise TelegramAPIError(f"Response returned an error: {error}") + if response.status != 200 or not data["ok"]: + raise TelegramAPIError(data["error_code"], data["description"]) # Return a dictionary containing the data return data["result"] @@ -231,11 +229,29 @@ class Chat: # TODO: This could give problems if a class inherits Bot if not isinstance(bot, Bot): raise TypeError("bot is not an instance of Bot.") - # Catch TelegramAPI exceptions - try: - await bot.api_request("sendMessage", text=text, chat_id=self.chat_id, **params) - except TelegramAPIError as e: - print(f"[Telegram] sendMessage failed: {e.args[0]}") + await bot.api_request("sendMessage", text=text, chat_id=self.chat_id, **params) + + + async def set_chat_action(self, bot, action): + """Set a status for the chat. + +Valid actions are: +typing +upload_photo +record_video +upload_video +record_audio +upload_audio +upload_document +find_location""" + # TODO: This could give problems if a class inherits Bot + if not isinstance(bot, Bot): + raise TypeError("bot is not an instance of Bot.") + # Check if the action is valid + if action not in ["typing", "upload_photo", "record_video", "upload_video", "record_audio", "upload_audio", "upload_document", "find_location"]: + raise ValueError("Invalid action") + # Send the request + await bot.api_request("sendChatAction", chat_id=self.chat_id, action=action) class User: