1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Ho fatto talmente tante cose che non lo so più neanche io

This commit is contained in:
Steffo 2017-03-30 12:53:55 +02:00
parent ded7a2c0e1
commit d37bd4a664
2 changed files with 103 additions and 57 deletions

View file

@ -31,84 +31,127 @@ def currently_logged_in(thing):
return user return user
async def start_telegram(bot, update, _): async def answer(bot, thing, text):
# Set status to typing """Rispondi al messaggio con il canale corretto."""
await update.message.chat.set_chat_action(bot, "typing") # Answer on Telegram
user = currently_logged_in(update) if isinstance(thing, telegram.Update):
if user is None: await thing.message.reply(bot, text, parse_mode="Markdown")
await update.message.reply(bot, f"Ciao!\n_Non hai eseguito l'accesso al RYGdb._", parse_mode="Markdown") # Answer on Discord
elif isinstance(thing, extradiscord.discord.Message):
await bot.send_message(thing.channel, text)
else: else:
raise TypeError("thing must be either a telegram.Update or a discord.Message")
async def status_typing(bot, thing):
"""Imposta lo stato a Bot sta scrivendo..."""
# Set typing status on Telegram
if isinstance(thing, telegram.Update):
await thing.message.chat.set_chat_action(bot, "typing")
# Set typing status on Discord
elif isinstance(thing, extradiscord.discord.Message):
await bot.send_typing(thing.channel)
else:
raise TypeError("thing must be either a telegram.Update or a discord.Message")
async def display_help(bot, thing, function):
"""Display the help command of a function"""
# Telegram bot commands start with /
if isinstance(thing, telegram.Update):
symbol = "/"
# Discord bot commands start with !
elif isinstance(thing, extradiscord.discord.Message):
symbol = "!"
# Unknown service
else:
raise TypeError("thing must be either a telegram.Update or a discord.Message")
# Display the help message
await answer(bot, thing, function.__doc__.format(symbol=symbol))
def find_date(thing):
"""Find the date of a message."""
if isinstance(thing, telegram.Update):
date = thing.message.date
elif isinstance(thing, extradiscord.discord.Message):
date = thing.timestamp
else:
raise TypeError("thing must be either a telegram.Update or a discord.Message")
return date
async def start(bot, thing, _):
"""Saluta l'utente e visualizza lo stato account sincronizzati.
Sintassi: `{symbol}start`"""
# Set status to typing
await status_typing(bot, thing)
# Find the currently logged in user
user = currently_logged_in(thing)
# Answer appropriately
if user is None:
await answer(bot, thing, f"Ciao!\n_Non hai eseguito l'accesso al RYGdb._")
else:
# Check the user's connected accounts
telegram_status = "🔵" if user.telegram_id is not None else "" telegram_status = "🔵" if user.telegram_id is not None else ""
discord_status = "🔵" if user.discord_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") await answer(bot, thing, f"Ciao!\n"
f"Hai eseguito l'accesso come `{user}`.\n"
f"\n"
f"*Account collegati:*\n"
f"{telegram_status} Telegram\n"
f"{discord_status} Discord")
async def diario_telegram(bot, update, arguments): async def diario(bot, thing, arguments):
"""Aggiungi una frase al diario Royal Games. """Aggiungi una frase al diario Royal Games.
Devi essere un Royal per poter eseguire questo comando. Devi essere un Royal per poter eseguire questo comando.
Sintassi: `/diario <frase>`""" Sintassi: `{symbol}diario <frase>`"""
# Set status to typing # Set status to typing
await update.message.chat.set_chat_action(bot, "typing") await status_typing(bot, thing)
# Check if the user is logged in # Check if the user is logged in
if not currently_logged_in(update): if not currently_logged_in(thing):
await update.message.reply(bot, "⚠ Non hai ancora eseguito l'accesso! Usa `/sync`.", parse_mode="Markdown") await answer(bot, thing, "⚠ Non hai ancora eseguito l'accesso! Usa `/sync`.")
return return
# Check if the currently logged in user is a Royal Games member # Check if the currently logged in user is a Royal Games member
if not currently_logged_in(update).royal: if not currently_logged_in(thing).royal:
await update.message.reply(bot, "⚠ Non sei autorizzato a eseguire questo comando.") await answer(bot, thing, "⚠ Non sei autorizzato a eseguire questo comando.")
return return
# Check the command syntax # Check the command syntax
if len(arguments) == 0: if len(arguments) == 0:
await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/diario <random | markov | numerofrase>`", parse_mode="Markdown") await display_help(bot, thing, diario)
return return
# Find the user # Find the user
user = currently_logged_in(update) user = currently_logged_in(thing)
# Prepare the text # Prepare the text
text = " ".join(arguments).strip() text = " ".join(arguments).strip()
# Add the new entry # Add the new entry
database.new_diario_entry(update.message.date, text, user) database.new_diario_entry(find_date(thing), text, user)
# Answer on Telegram # Answer on Telegram
await update.message.reply(bot, "✅ Aggiunto al diario!") await answer(bot, thing, "✅ Aggiunto al diario!")
async def diario_discord(bot, message, arguments): async def leggi(bot, thing, arguments):
"""Aggiungi una frase al diario Royal Games. """Leggi una frase con un id specifico dal diario Royal Games.
Devi essere un Royal per poter eseguire questo comando. Sintassi: {symbol}leggi <numero>"""
# Set status to typing
Sintassi: `!diario <frase>`""" await status_typing(bot, thing)
# Check if the user is logged in # Create a new database session
if not currently_logged_in(message): session = database.Session()
bot.send_message(message.channel, "⚠ Non hai ancora eseguito l'accesso! Usa `!sync`.") # Cast the number to an int
try:
n = int(arguments[0])
except ValueError:
await answer(bot, thing, "⚠ Il numero specificato non è valido.")
return return
# Check if the currently logged in user is a Royal Games member # Query the diario table for the entry with the specified id
if not currently_logged_in(message).royal: entry = session.query(database.Diario).filter_by(id=n).first()
bot.send_message(message.channel, "⚠ Non sei autorizzato a eseguire questo comando.") # Display the entry
return # TODO: FINISH THIS
# Check the command syntax
if len(arguments) == 0:
bot.send_message(message.channel, "⚠ Sintassi del comando non valida.\n`!diario <random | markov | numerofrase>`")
return
# Check for non-ASCII characters
entry = " ".join(arguments)
if not entry.isprintable():
bot.send_message(message.channel, "⚠ La frase che stai provando ad aggiungere contiene caratteri non ASCII, quindi non è stata aggiunta.\nToglili e riprova!")
return
# Remove endlines
entry = entry.replace("\n", " ")
# TODO: check if a end-of-file character can be sent on Discord
# Generate a timestamp
time = message.timestamp
# Write on the diario file
file = open("diario.txt", "a", encoding="utf8")
file.write(f"{int(time)}|{entry}\n")
file.close()
del file
# Answer on Telegram
bot.send_message(message.channel, "✅ Aggiunto al diario!")
async def leggi_telegram(bot, update, arguments): async def leggi_telegram(bot, update, arguments):
"""Leggi una frase dal diario Royal Games. """Leggi una frase dal diario Royal Games.
@ -119,7 +162,8 @@ Sintassi: `/leggi <random | numerofrase>`"""
# Set status to typing # Set status to typing
await update.message.chat.set_chat_action(bot, "typing") await update.message.chat.set_chat_action(bot, "typing")
if len(arguments) == 0 or len(arguments) > 1: if len(arguments) == 0 or len(arguments) > 1:
await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/leggi <random | numerofrase>`", parse_mode="Markdown") await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n"
"`/leggi <random | numerofrase>`", parse_mode="Markdown")
return return
# Open the file # Open the file
file = open("diario.txt", "r", encoding="utf8") file = open("diario.txt", "r", encoding="utf8")
@ -545,10 +589,13 @@ Sintassi: `/toggleroyal <username>`"""
await update.message.reply(bot, f"✅ L'utente `{user.username}` non è più un Royal.", parse_mode="Markdown") await update.message.reply(bot, f"✅ L'utente `{user.username}` non è più un Royal.", parse_mode="Markdown")
if __name__ == "__main__": if __name__ == "__main__":
# Init universal bot commands
b.commands["start"] = start
d.commands["start"] = start
b.commands["diario"] = diario
d.commands["diario"] = diario
# Init Telegram bot commands # Init Telegram bot commands
b.commands["start"] = start_telegram
b.commands["leggi"] = leggi_telegram b.commands["leggi"] = leggi_telegram
b.commands["diario"] = diario_telegram
b.commands["discord"] = discord_telegram b.commands["discord"] = discord_telegram
b.commands["sync"] = sync_telegram b.commands["sync"] = sync_telegram
b.commands["changepassword"] = changepassword_telegram b.commands["changepassword"] = changepassword_telegram
@ -563,7 +610,6 @@ if __name__ == "__main__":
d.commands["roll"] = roll_discord d.commands["roll"] = roll_discord
d.commands["help"] = help_discord d.commands["help"] = help_discord
d.commands["leggi"] = leggi_discord d.commands["leggi"] = leggi_discord
d.commands["diario"] = diario_discord
# Init Telegram bot # Init Telegram bot
loop.create_task(b.run()) loop.create_task(b.run())
print("Telegram bot start scheduled!") print("Telegram bot start scheduled!")

View file

@ -98,7 +98,7 @@ class Bot:
command = split_msg[0].lstrip("/").split("@")[0] command = split_msg[0].lstrip("/").split("@")[0]
if command in self.commands: if command in self.commands:
arguments = split_msg[1:] arguments = split_msg[1:]
loop.create_task(self.commands[command](bot=self, update=update, arguments=arguments)) loop.create_task(self.commands[command](self, update, arguments))
# Update message status if a service message is received # Update message status if a service message is received
if isinstance(update.message.content, ServiceMessage): if isinstance(update.message.content, ServiceMessage):
# New user in chat # New user in chat