From b353883b07756a6c6cf296b5e86b1dba5e954807 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 22 Mar 2017 19:13:22 +0100 Subject: [PATCH] Added Discord account system --- extra_discord.py | 21 +++++++++++++++++++ grandbot.py | 52 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 extra_discord.py diff --git a/extra_discord.py b/extra_discord.py new file mode 100644 index 00000000..edfe4c12 --- /dev/null +++ b/extra_discord.py @@ -0,0 +1,21 @@ +import discord +import asyncio +loop = asyncio.get_event_loop() + +class ExtraClient: + def __init__(self, token): + self.client = discord.Client() + self.commands = dict() + self.token = token + + @self.client.event + async def on_message(message): + split = message.content.split(" ") + command = split[0].lstrip("!") + if command in self.commands: + await self.commands[command](self.client, message, split[1:]) + + + async def run(self): + await self.client.login(self.token) + await self.client.connect() \ No newline at end of file diff --git a/grandbot.py b/grandbot.py index 44e26f83..6c8d107d 100644 --- a/grandbot.py +++ b/grandbot.py @@ -4,7 +4,7 @@ import json import random import aiohttp import async_timeout -import discord +import extra_discord import markovify import database import royalbotconfig @@ -12,7 +12,7 @@ import telegram loop = asyncio.get_event_loop() b = telegram.Bot(royalbotconfig.telegram_token) -d = discord.Client() +d = extra_discord.ExtraClient(royalbotconfig.discord_token) def currently_logged_in(update): @@ -25,9 +25,11 @@ def currently_logged_in(update): async def start(bot, update, arguments): 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") + await update.message.reply(bot, f"Ciao!\n_Non hai eseguito l'accesso al RYGdb._", parse_mode="Markdown") else: - await update.message.reply(bot, f"Ciao!\nHai eseguito l'accesso come `{user}`.", parse_mode="Markdown") + telegram_status = "🔵" if user.telegram_id is not None else "⚪" + discord_status = "🔵" if user.discord_id is not None else "⚪" + await update.message.reply(bot, f"Ciao!\nHai eseguito l'accesso come `{user}`.\n\n*Account collegati:*\n{telegram_status} Telegram\n{discord_status} Discord", parse_mode="Markdown") async def diario(bot, update, arguments): @@ -166,7 +168,7 @@ Sintassi: `/discord `""" await update.message.reply(bot, "Richiesta inviata.", parse_mode="Markdown") -async def sync(bot, update, arguments): +async def sync_telegram(bot, update, arguments): """Connetti il tuo account Telegram al Database Royal Games. Sintassi: `/sync `""" @@ -179,10 +181,9 @@ Sintassi: `/sync `""" if logged_user is not None: # Add the telegram_id to the user if it's missing if logged_user.telegram_id is None: - # Handle duplicate logged_user.telegram_id = update.message.sent_from.user_id session.commit() - print(f"{logged_user} ha sincronizzato l'account.") + print(f"{logged_user} ha sincronizzato l'account di Telegram.") await update.message.reply(bot, f"Sincronizzazione riuscita!\nSei loggato come `{logged_user}`.", parse_mode="Markdown") else: await update.message.reply(bot, "⚠ L'account è già stato sincronizzato.", parse_mode="Markdown") @@ -190,6 +191,29 @@ Sintassi: `/sync `""" await update.message.reply(bot, "⚠ Username o password non validi.", parse_mode="Markdown") +async def sync_discord(bot, message, arguments): + """Connetti il tuo account Discord al Database Royal Games. + +Sintassi: `!sync `""" + if len(arguments) != 2: + await bot.send_message(message.channel, "⚠ Sintassi del comando non valida.\n`!sync `") + return + # Try to login + session, logged_user = database.login(arguments[0], arguments[1]) + # Check if the login is successful + if logged_user is not None: + # Add the discord_id to the user if it's missing + if logged_user.discord_id is None: + logged_user.discord_id = int(message.author.id) + session.commit() + print(f"{logged_user} ha sincronizzato l'account di Discord.") + await bot.send_message(message.channel, f"Sincronizzazione riuscita!\nSei loggato come `{logged_user}`.") + else: + await bot.send_message(message.channel, "⚠ L'account è già stato sincronizzato.") + else: + await bot.send_message(message.channel, "⚠ Username o password non validi.") + + async def changepassword(bot, update, arguments): """Cambia la tua password del Database Royal Games. @@ -216,11 +240,11 @@ Sintassi: `/cv`""" await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/cv`", parse_mode="Markdown") return # Wait for the Discord bot to login - while not d.is_logged_in: + while not d.client.is_logged_in: await asyncio.sleep(1) # Find all the users in the server # Change this if the bot is logged in more than one server at once? - users = list(d.get_all_members()) + users = list(d.client.get_all_members()) # Find all the channels channels = dict() for user in users: @@ -287,23 +311,27 @@ Sintassi: `/cv`""" to_send += "\n" await update.message.reply(bot, to_send, parse_mode="Markdown", disable_web_page_preview=1) + if __name__ == "__main__": + # Generate the db if it's empty + # Init Telegram bot commands b.commands["start"] = start b.commands["leggi"] = leggi b.commands["diario"] = diario b.commands["discord"] = discord - b.commands["sync"] = sync + b.commands["sync"] = sync_telegram b.commands["changepassword"] = changepassword b.commands["help"] = help_cmd b.commands["markov"] = markov b.commands["cv"] = cv + # Init Discord bot commands + d.commands["sync"] = sync_discord # Init Telegram bot loop.create_task(b.run()) print("Telegram bot start scheduled!") # Init Discord bot - loop.run_until_complete(d.login(royalbotconfig.discord_token)) - loop.create_task(d.connect()) + loop.create_task(d.run()) print("Discord bot start scheduled!") # Run everything! loop.run_forever() \ No newline at end of file