2017-02-27 22:16:42 +00:00
import asyncio
2017-03-05 17:14:45 +00:00
import datetime
2017-03-09 11:51:45 +00:00
import json
2017-03-22 17:04:21 +00:00
import random
import aiohttp
import async_timeout
2017-03-29 09:23:20 +00:00
import extradiscord
2017-03-22 17:04:21 +00:00
import markovify
import database
import royalbotconfig
import telegram
2017-03-05 17:14:45 +00:00
2017-03-22 16:23:17 +00:00
loop = asyncio . get_event_loop ( )
2017-03-05 18:12:02 +00:00
b = telegram . Bot ( royalbotconfig . telegram_token )
2017-03-29 09:23:20 +00:00
d = extradiscord . ExtraClient ( royalbotconfig . discord_token )
2017-02-27 22:16:42 +00:00
2017-03-22 16:23:17 +00:00
2017-03-24 11:45:54 +00:00
def currently_logged_in ( thing ) :
2017-03-10 14:52:26 +00:00
""" Trova l ' utente connesso all ' account di Telegram che ha mandato l ' update. """
2017-03-24 11:45:54 +00:00
# Create a new database session
2017-03-10 10:52:20 +00:00
session = database . Session ( )
2017-03-24 11:45:54 +00:00
# Check if thing is a Telegram update
if isinstance ( thing , telegram . Update ) :
user = session . query ( database . User ) . filter_by ( telegram_id = thing . message . sent_from . user_id ) . first ( )
# Check if thing is a Discord message
2017-03-29 09:23:20 +00:00
elif isinstance ( thing , extradiscord . discord . Message ) :
2017-03-24 11:45:54 +00:00
user = session . query ( database . User ) . filter_by ( discord_id = thing . author . id ) . first ( )
# I don't know what thing is.
else :
raise TypeError ( " thing must be either a telegram.Update or a discord.Message " )
2017-03-10 11:00:37 +00:00
return user
2017-03-10 10:52:20 +00:00
2017-03-30 10:53:55 +00:00
async def answer ( bot , thing , text ) :
""" Rispondi al messaggio con il canale corretto. """
# Answer on Telegram
if isinstance ( thing , telegram . Update ) :
await thing . message . reply ( bot , text , parse_mode = " Markdown " )
# Answer on Discord
elif isinstance ( thing , extradiscord . discord . Message ) :
await bot . send_message ( thing . channel , text )
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 ` """
2017-03-27 10:52:02 +00:00
# Set status to typing
2017-03-30 10:53:55 +00:00
await status_typing ( bot , thing )
# Find the currently logged in user
user = currently_logged_in ( thing )
# Answer appropriately
2017-03-22 16:23:17 +00:00
if user is None :
2017-03-30 10:53:55 +00:00
await answer ( bot , thing , f " Ciao! \n _Non hai eseguito l ' accesso al RYGdb._ " )
2017-03-22 16:23:17 +00:00
else :
2017-03-30 10:53:55 +00:00
# Check the user's connected accounts
2017-03-22 18:13:22 +00:00
telegram_status = " 🔵 " if user . telegram_id is not None else " ⚪ "
discord_status = " 🔵 " if user . discord_id is not None else " ⚪ "
2017-03-30 10:53:55 +00:00
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 " )
2017-03-22 16:23:17 +00:00
2017-03-30 10:53:55 +00:00
async def diario ( bot , thing , arguments ) :
2017-03-05 17:14:45 +00:00
""" Aggiungi una frase al diario Royal Games.
2017-03-10 11:00:37 +00:00
Devi essere un Royal per poter eseguire questo comando .
2017-03-30 10:53:55 +00:00
Sintassi : ` { symbol } diario < frase > ` """
2017-03-27 10:52:02 +00:00
# Set status to typing
2017-03-30 10:53:55 +00:00
await status_typing ( bot , thing )
2017-03-23 18:43:26 +00:00
# Check if the user is logged in
2017-03-30 10:53:55 +00:00
if not currently_logged_in ( thing ) :
await answer ( bot , thing , " ⚠ Non hai ancora eseguito l ' accesso! Usa `/sync`. " )
2017-03-23 18:43:26 +00:00
return
# Check if the currently logged in user is a Royal Games member
2017-03-30 10:53:55 +00:00
if not currently_logged_in ( thing ) . royal :
await answer ( bot , thing , " ⚠ Non sei autorizzato a eseguire questo comando. " )
2017-03-10 11:00:37 +00:00
return
2017-03-23 18:43:26 +00:00
# Check the command syntax
2017-03-05 17:14:45 +00:00
if len ( arguments ) == 0 :
2017-03-30 10:53:55 +00:00
await display_help ( bot , thing , diario )
2017-03-05 17:14:45 +00:00
return
2017-03-30 10:13:10 +00:00
# Find the user
2017-03-30 10:53:55 +00:00
user = currently_logged_in ( thing )
2017-03-30 10:13:10 +00:00
# Prepare the text
text = " " . join ( arguments ) . strip ( )
# Add the new entry
2017-03-30 10:53:55 +00:00
database . new_diario_entry ( find_date ( thing ) , text , user )
2017-03-23 18:43:26 +00:00
# Answer on Telegram
2017-03-30 10:53:55 +00:00
await answer ( bot , thing , " ✅ Aggiunto al diario! " )
2017-03-05 17:14:45 +00:00
2017-03-24 11:45:54 +00:00
2017-03-30 10:53:55 +00:00
async def leggi ( bot , thing , arguments ) :
""" Leggi una frase con un id specifico dal diario Royal Games.
2017-03-24 11:45:54 +00:00
2017-03-30 10:53:55 +00:00
Sintassi : { symbol } leggi < numero > """
# Set status to typing
await status_typing ( bot , thing )
# Create a new database session
session = database . Session ( )
# Cast the number to an int
try :
n = int ( arguments [ 0 ] )
except ValueError :
await answer ( bot , thing , " ⚠ Il numero specificato non è valido. " )
2017-03-24 11:45:54 +00:00
return
2017-03-30 10:53:55 +00:00
# Query the diario table for the entry with the specified id
entry = session . query ( database . Diario ) . filter_by ( id = n ) . first ( )
# Display the entry
# TODO: FINISH THIS
2017-03-24 11:45:54 +00:00
2017-03-24 11:24:39 +00:00
async def leggi_telegram ( bot , update , arguments ) :
2017-03-05 17:14:45 +00:00
""" 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 < numero > ` .
2017-03-13 18:13:42 +00:00
Sintassi : ` / leggi < random | numerofrase > ` """
2017-03-27 10:52:02 +00:00
# Set status to typing
2017-03-27 10:57:06 +00:00
await update . message . chat . set_chat_action ( bot , " typing " )
2017-03-05 17:14:45 +00:00
if len ( arguments ) == 0 or len ( arguments ) > 1 :
2017-03-30 10:53:55 +00:00
await update . message . reply ( bot , " ⚠ Sintassi del comando non valida. \n "
" `/leggi <random | numerofrase>` " , parse_mode = " Markdown " )
2017-03-05 17:14:45 +00:00
return
2017-03-23 18:43:26 +00:00
# Open the file
2017-03-26 11:52:49 +00:00
file = open ( " diario.txt " , " r " , encoding = " utf8 " )
2017-03-23 18:43:26 +00:00
# Split the data in lines
2017-03-13 18:13:42 +00:00
entries = file . read ( ) . split ( " \n " )
2017-03-05 17:14:45 +00:00
file . close ( )
2017-03-23 18:43:26 +00:00
# Choose an entry
2017-03-13 18:13:42 +00:00
if arguments [ 0 ] == " random " :
2017-03-23 18:43:26 +00:00
# either randomly...
2017-03-13 18:13:42 +00:00
entry_number = random . randrange ( len ( entries ) )
2017-03-05 17:14:45 +00:00
else :
2017-03-23 18:43:26 +00:00
# ...or a specific one
2017-03-24 11:56:05 +00:00
# TODO: check if the entry actually exists
# TODO: check if the first argument is a number
entry_number = int ( arguments [ 0 ] )
2017-03-23 18:43:26 +00:00
# Split the timestamp from the text
2017-03-13 18:13:42 +00:00
entry = entries [ entry_number ] . split ( " | " , 1 )
2017-03-23 18:43:26 +00:00
# Parse the timestamp
2017-03-24 11:56:05 +00:00
date = datetime . datetime . fromtimestamp ( int ( entry [ 0 ] ) ) . isoformat ( )
2017-03-23 18:43:26 +00:00
# Get the text
2017-03-05 17:14:45 +00:00
text = entry [ 1 ]
2017-03-23 18:43:26 +00:00
# Sanitize the text to prevent TelegramErrors
text = text . replace ( " _ " , " \ _ " ) . replace ( " * " , " \ * " ) . replace ( " ` " , " \ ` " ) . replace ( " [ " , " \ [ " )
2017-03-13 11:57:04 +00:00
await update . message . reply ( bot , f " Frase # { entry_number } | { date } \n { text } " , parse_mode = " Markdown " )
2017-03-05 17:14:45 +00:00
2017-03-24 11:30:34 +00:00
async def leggi_discord ( bot , message , arguments ) :
""" Leggi una frase dal diario Royal Games.
2017-03-28 15:43:07 +00:00
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 < numero > ` .
2017-03-24 11:30:34 +00:00
2017-03-28 15:43:07 +00:00
Sintassi : ` ! leggi < random | numerofrase > ` """
2017-03-24 11:30:34 +00:00
if len ( arguments ) == 0 or len ( arguments ) > 1 :
2017-03-24 11:52:38 +00:00
await bot . send_message ( message . channel , " ⚠ Sintassi del comando non valida. \n `!leggi <random | numerofrase>` " )
2017-03-24 11:30:34 +00:00
return
# Open the file
2017-03-26 11:52:49 +00:00
file = open ( " diario.txt " , " r " , encoding = " utf8 " )
2017-03-24 11:30:34 +00:00
# Split the data in lines
entries = file . read ( ) . split ( " \n " )
file . close ( )
# Choose an entry
if arguments [ 0 ] == " random " :
# either randomly...
entry_number = random . randrange ( len ( entries ) )
else :
# ...or a specific one
2017-03-24 11:56:05 +00:00
# TODO: check if the entry actually exists
# TODO: check if the first argument is a number
entry_number = int ( arguments [ 0 ] )
2017-03-24 11:30:34 +00:00
# Split the timestamp from the text
entry = entries [ entry_number ] . split ( " | " , 1 )
# Parse the timestamp
2017-03-24 11:56:05 +00:00
date = datetime . datetime . fromtimestamp ( int ( entry [ 0 ] ) ) . isoformat ( )
2017-03-24 11:30:34 +00:00
# Get the text
text = entry [ 1 ]
# Sanitize the text to prevent TelegramErrors
text = text . replace ( " _ " , " \ _ " ) . replace ( " * " , " \ * " ) . replace ( " ` " , " \ ` " ) . replace ( " [ " , " \ [ " )
await bot . send_message ( message . channel , f " Frase # { entry_number } | { date } \n { text } " )
2017-03-24 11:24:39 +00:00
async def markov_telegram ( bot , update , arguments ) :
2017-03-13 18:13:42 +00:00
""" Genera una frase del diario utilizzando le catene di Markov.
2017-03-22 17:02:59 +00:00
Puoi specificare con che parole ( massimo 2 ) deve iniziare la frase generata .
Se non vengono specificate , verrà scelta una parola a caso .
2017-03-13 18:13:42 +00:00
Sintassi : ` / markov [ inizio ] ` """
2017-03-27 10:52:02 +00:00
# Set status to typing
2017-03-27 10:57:06 +00:00
await update . message . chat . set_chat_action ( bot , " typing " )
2017-03-22 17:02:59 +00:00
if len ( arguments ) > 2 :
await update . message . reply ( bot , " ⚠ Sintassi del comando non valida. \n `/markov [inizio]` " )
2017-03-13 18:13:42 +00:00
file = open ( " diario.txt " , " r " , encoding = " utf8 " )
# Clean the diario
clean_diario = str ( )
# Remove the timestamps in each row
for row in file :
2017-03-18 20:32:34 +00:00
clean_diario + = row . split ( " | " , 1 ) [ 1 ] . lower ( )
2017-03-13 18:13:42 +00:00
# The text is split by newlines
generator = markovify . NewlineText ( clean_diario )
file . close ( )
if len ( arguments ) == 0 :
# Generate a sentence with a random start
2017-03-14 13:02:10 +00:00
text = generator . make_sentence ( tries = 50 )
2017-03-13 18:13:42 +00:00
else :
# Generate a sentence with a specific start
2017-03-22 17:02:59 +00:00
start_with = " " . join ( arguments )
2017-03-14 13:02:10 +00:00
try :
2017-03-22 17:02:59 +00:00
text = generator . make_sentence_with_start ( start_with , tries = 100 )
2017-03-14 13:02:10 +00:00
# No entry can start in that word.
except KeyError :
await update . message . reply ( bot , f " ⚠ Non sono state trovate corrispondenze nel diario dell ' inizio che hai specificato. " , parse_mode = " Markdown " )
return
if text is not None :
2017-03-23 18:43:26 +00:00
# Sanitize the text to prevent TelegramErrors
text = text . replace ( " _ " , " \ _ " ) . replace ( " * " , " \ * " ) . replace ( " ` " , " \ ` " ) . replace ( " [ " , " \ [ " )
2017-03-14 13:02:10 +00:00
await update . message . reply ( bot , f " *Frase generata:* \n { text } " , parse_mode = " Markdown " )
else :
await update . message . reply ( bot , f " ⚠ Il bot non è riuscito a generare una nuova frase. \n Se è la prima volta che vedi questo errore, riprova, altrimenti prova a cambiare configurazione. " )
2017-03-13 18:13:42 +00:00
2017-03-24 11:24:39 +00:00
async def help_telegram ( bot , update , arguments ) :
2017-03-05 17:14:45 +00:00
""" Visualizza la descrizione di un comando.
Sintassi : ` / help [ comando ] ` """
2017-03-27 10:52:02 +00:00
# Set status to typing
2017-03-27 10:57:06 +00:00
await update . message . chat . set_chat_action ( bot , " typing " )
2017-03-05 17:14:45 +00:00
if len ( arguments ) == 0 :
2017-03-13 11:57:04 +00:00
await update . message . reply ( bot , help . __doc__ , parse_mode = " Markdown " )
2017-03-05 17:14:45 +00:00
elif len ( arguments ) > 1 :
2017-03-13 11:57:04 +00:00
await update . message . reply ( bot , " ⚠ Sintassi del comando non valida. \n `/help [comando]` " , parse_mode = " Markdown " )
2017-03-05 17:14:45 +00:00
else :
if arguments [ 0 ] in b . commands :
2017-03-26 11:52:07 +00:00
await update . message . reply ( bot , b . commands [ arguments [ 0 ] + " _telegram " ] . __doc__ , parse_mode = " Markdown " )
2017-03-05 17:14:45 +00:00
else :
2017-03-23 18:43:26 +00:00
await update . message . reply ( bot , " ⚠ Il comando specificato non esiste. " )
2017-03-05 17:14:45 +00:00
2017-02-27 22:16:42 +00:00
2017-03-24 11:27:54 +00:00
async def help_discord ( bot , message , arguments ) :
""" Visualizza la descrizione di un comando.
Sintassi : ` ! help [ comando ] ` """
if len ( arguments ) == 0 :
bot . send_message ( message . channel , help . __doc__ )
elif len ( arguments ) > 1 :
2017-03-24 11:52:38 +00:00
bot . send_message ( message . channel , " ⚠ Sintassi del comando non valida. \n `!help [comando]` " )
2017-03-24 11:27:54 +00:00
else :
if arguments [ 0 ] in b . commands :
2017-03-26 11:52:07 +00:00
bot . send_message ( message . channel , b . commands [ arguments [ 0 ] + " _discord " ] . __doc__ )
2017-03-24 11:27:54 +00:00
else :
bot . send_message ( message . channel , " ⚠ Il comando specificato non esiste. " )
2017-03-24 11:24:39 +00:00
async def discord_telegram ( bot , update , arguments ) :
2017-03-05 18:12:02 +00:00
""" Manda un messaggio a #chat di Discord.
Sintassi : ` / discord < messaggio > ` """
2017-03-27 10:52:02 +00:00
# Set status to typing
2017-03-27 10:57:06 +00:00
await update . message . chat . set_chat_action ( bot , " typing " )
2017-03-23 18:43:26 +00:00
# Try to login
logged_user = currently_logged_in ( update )
# Check if the user is logged in
if not logged_user :
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 logged_user . royal :
await update . message . reply ( bot , " ⚠ Non sei autorizzato a eseguire questo comando. " )
return
# Check the command syntax
2017-03-05 18:12:02 +00:00
if len ( arguments ) == 0 :
2017-03-13 11:57:04 +00:00
await update . message . reply ( bot , " ⚠ Sintassi del comando non valida. \n `/discord <messaggio>` " , parse_mode = " Markdown " )
2017-03-05 18:12:02 +00:00
return
message = " " . join ( arguments )
2017-03-23 18:43:26 +00:00
# Find the message sender's Discord username
users = list ( d . client . get_all_members ( ) )
for user in users :
if user . id == logged_user . discord_id :
username = user . name
break
else :
# Use the telegram username
username = f " { update . message . sent_from } "
2017-03-05 18:12:02 +00:00
# Parameters to send
params = {
2017-03-23 18:43:26 +00:00
" username " : username ,
" content " : f " { message } "
2017-03-05 18:12:02 +00:00
}
# Headers to send
headers = {
" Content-Type " : " application/json "
}
# Request timeout is 10 seconds.
with async_timeout . timeout ( 10 ) :
# Create a new session for each request.
async with aiohttp . ClientSession ( ) as session :
# Send the request to the Discord webhook
async with session . request ( " POST " , royalbotconfig . discord_webhook , data = json . dumps ( params ) , headers = headers ) as response :
# Check if the request was successful
if response . status != 204 :
# Request failed
# Answer on Telegram
2017-03-13 11:57:04 +00:00
await update . message . reply ( bot , " ⚠ L ' invio del messaggio è fallito. Oops! " , parse_mode = " Markdown " )
2017-03-05 18:12:02 +00:00
# TODO: handle Discord webhooks errors
raise Exception ( " Qualcosa è andato storto durante l ' invio del messaggio a Discord. " )
# Answer on Telegram
2017-03-23 18:43:26 +00:00
await update . message . reply ( bot , " ✅ Richiesta inviata. " , parse_mode = " Markdown " )
2017-03-05 18:12:02 +00:00
2017-03-22 18:13:22 +00:00
async def sync_telegram ( bot , update , arguments ) :
2017-03-10 09:11:06 +00:00
""" Connetti il tuo account Telegram al Database Royal Games.
2017-03-09 14:52:02 +00:00
2017-03-10 09:11:06 +00:00
Sintassi : ` / sync < username > < password > ` """
2017-03-27 10:52:02 +00:00
# Set status to typing
2017-03-27 10:57:06 +00:00
await update . message . chat . set_chat_action ( bot , " typing " )
2017-03-09 14:52:02 +00:00
if len ( arguments ) != 2 :
2017-03-13 11:57:04 +00:00
await update . message . reply ( bot , " ⚠ Sintassi del comando non valida. \n `/sync <username> <password>` " , parse_mode = " Markdown " )
2017-03-09 14:52:02 +00:00
return
2017-03-10 09:11:06 +00:00
# Try to login
session , logged_user = database . login ( arguments [ 0 ] , arguments [ 1 ] )
# Check if the login is successful
2017-03-09 14:52:02 +00:00
if logged_user is not None :
2017-03-10 09:11:06 +00:00
# Add the telegram_id to the user if it's missing
if logged_user . telegram_id is None :
logged_user . telegram_id = update . message . sent_from . user_id
session . commit ( )
2017-03-22 18:13:22 +00:00
print ( f " { logged_user } ha sincronizzato l ' account di Telegram. " )
2017-03-13 11:57:04 +00:00
await update . message . reply ( bot , f " Sincronizzazione riuscita! \n Sei loggato come ` { logged_user } `. " , parse_mode = " Markdown " )
2017-03-10 09:11:06 +00:00
else :
2017-03-13 11:57:04 +00:00
await update . message . reply ( bot , " ⚠ L ' account è già stato sincronizzato. " , parse_mode = " Markdown " )
2017-03-09 14:52:02 +00:00
else :
2017-03-13 11:57:04 +00:00
await update . message . reply ( bot , " ⚠ Username o password non validi. " , parse_mode = " Markdown " )
2017-03-09 14:52:02 +00:00
2017-03-22 18:13:22 +00:00
async def sync_discord ( bot , message , arguments ) :
""" Connetti il tuo account Discord al Database Royal Games.
Sintassi : ` ! sync < username > < password > ` """
if len ( arguments ) != 2 :
await bot . send_message ( message . channel , " ⚠ Sintassi del comando non valida. \n `!sync <username> <password>` " )
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! \n Sei 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. " )
2017-03-24 11:24:39 +00:00
async def changepassword_telegram ( bot , update , arguments ) :
2017-03-10 09:34:27 +00:00
""" Cambia la tua password del Database Royal Games.
2017-03-10 10:52:20 +00:00
Sintassi : ` / changepassword < newpassword > ` """
2017-03-27 10:52:02 +00:00
# Set status to typing
2017-03-27 10:57:06 +00:00
await update . message . chat . set_chat_action ( bot , " typing " )
2017-03-10 10:52:20 +00:00
if len ( arguments ) != 2 :
2017-03-13 11:57:04 +00:00
await update . message . reply ( bot , " ⚠ Sintassi del comando non valida. \n `/changepassword <oldpassword> <newpassword>` " , parse_mode = " Markdown " )
2017-03-10 09:34:27 +00:00
return
2017-03-10 10:52:20 +00:00
# TODO: this can be improved, maybe?
2017-03-10 11:00:37 +00:00
logged_user = currently_logged_in ( update )
2017-03-10 09:34:27 +00:00
# Check if the login is successful
if logged_user is not None :
# Change the password
2017-03-10 10:52:20 +00:00
database . change_password ( logged_user . username , arguments [ 1 ] )
2017-03-29 09:22:17 +00:00
await update . message . reply ( bot , f " Il cambio password è riuscito! \n \n La tua password è hashata nel database come_ ` { logged_user . password } `_, io non la posso vedere in nessun modo!_ " , parse_mode = " Markdown " )
2017-03-10 09:34:27 +00:00
else :
2017-03-13 11:57:04 +00:00
await update . message . reply ( bot , " ⚠ Username o password non validi. " , parse_mode = " Markdown " )
2017-03-10 09:34:27 +00:00
2017-03-24 11:24:39 +00:00
async def cv_telegram ( bot , update , arguments ) :
2017-03-18 20:32:34 +00:00
""" Visualizza lo stato attuale della chat vocale Discord.
Sintassi : ` / cv ` """
2017-03-27 10:52:02 +00:00
# Set status to typing
2017-03-27 10:57:06 +00:00
await update . message . chat . set_chat_action ( bot , " typing " )
2017-03-18 20:32:34 +00:00
if len ( arguments ) != 0 :
await update . message . reply ( bot , " ⚠ Sintassi del comando non valida. \n `/cv` " , parse_mode = " Markdown " )
return
# Wait for the Discord bot to login
2017-03-22 18:13:22 +00:00
while not d . client . is_logged_in :
2017-03-18 20:32:34 +00:00
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?
2017-03-22 18:13:22 +00:00
users = list ( d . client . get_all_members ( ) )
2017-03-18 20:32:34 +00:00
# Find all the channels
channels = dict ( )
for user in users :
if user . voice_channel is not None :
if user . voice_channel . name not in channels :
channels [ user . voice_channel . name ] = list ( )
channels [ user . voice_channel . name ] . append ( user )
# Create the string to send to Telegram
to_send = str ( )
for channel in channels :
# Channel header
to_send + = f " * { channel } :* \n "
# Users in channel
for user in channels [ channel ] :
# Online status
if user . status . name == " online " :
2017-03-18 21:11:28 +00:00
# Online
2017-03-18 20:32:34 +00:00
status = " 🔵 "
2017-03-22 18:34:47 +00:00
elif user . status . name == " dnd " or ( user . game is not None and user . game . type == 1 ) :
2017-03-18 21:11:28 +00:00
# Do not disturb or streaming
2017-03-18 20:39:59 +00:00
status = " 🔴 "
2017-03-18 20:32:34 +00:00
elif user . status . name == " idle " :
2017-03-18 21:11:28 +00:00
# Idle
2017-03-18 20:32:34 +00:00
status = " ⚫ "
elif user . status . name == " offline " :
2017-03-18 21:11:28 +00:00
# Invisible
2017-03-18 20:32:34 +00:00
status = " ⚪ "
else :
2017-03-18 21:11:28 +00:00
# Unknown
2017-03-18 20:32:34 +00:00
status = " ❓ "
# Voice status
if user . bot :
2017-03-18 21:11:28 +00:00
# Music bot
2017-03-18 20:32:34 +00:00
volume = " 🎵 "
elif user . voice . deaf or user . voice . self_deaf :
2017-03-18 21:11:28 +00:00
# Deafened
2017-03-18 20:32:34 +00:00
volume = " 🔇 "
elif user . voice . mute or user . voice . self_mute :
2017-03-18 21:11:28 +00:00
# Muted
2017-03-18 20:32:34 +00:00
volume = " 🔈 "
else :
2017-03-18 21:11:28 +00:00
# Speaking
2017-03-18 20:32:34 +00:00
volume = " 🔊 "
# Game, is formatted
if user . game is not None :
2017-03-18 21:11:28 +00:00
# Playing
if user . game . type == 0 :
# Game name
game = f " - * { user . game . name } * "
# Streaming
elif user . game . type == 1 :
# Stream name and url
game = f " - [ { user . game . name } ]( { user . game . url } ) "
2017-03-18 20:32:34 +00:00
else :
game = " "
2017-03-18 21:11:28 +00:00
# Nickname if available, otherwise use the username
2017-03-18 20:32:34 +00:00
if user . nick is not None :
name = user . nick
else :
name = user . name
# Add the user
to_send + = f " { volume } { status } { name } { game } \n "
# Channel footer
to_send + = " \n "
2017-03-18 21:11:28 +00:00
await update . message . reply ( bot , to_send , parse_mode = " Markdown " , disable_web_page_preview = 1 )
2017-03-18 20:32:34 +00:00
2017-03-22 18:13:22 +00:00
2017-03-24 11:24:39 +00:00
async def roll_telegram ( bot , update , arguments ) :
""" Lancia un dado a N facce.
Sintassi : ` / roll < max > ` """
2017-03-27 10:52:02 +00:00
# Set status to typing
2017-03-27 10:57:06 +00:00
await update . message . chat . set_chat_action ( bot , " typing " )
2017-03-24 11:24:39 +00:00
# Check the command syntax
2017-03-26 12:08:11 +00:00
if len ( arguments ) != 1 :
2017-03-24 11:24:39 +00:00
await update . message . reply ( bot , " ⚠ Sintassi del comando non valida. \n `/roll <max>` " ,
parse_mode = " Markdown " )
return
# Roll the dice!
2017-03-28 15:43:07 +00:00
await update . message . reply ( bot , f " *Numero generato:* { random . randrange ( 0 , int ( arguments [ 0 ] ) ) + 1 } " , parse_mode = " Markdown " )
2017-03-24 11:24:39 +00:00
async def roll_discord ( bot , message , arguments ) :
""" Lancia un dado a N facce.
Sintassi : ` ! roll < max > ` """
# Check the command syntax
2017-03-26 12:08:11 +00:00
if len ( arguments ) != 1 :
2017-03-24 11:52:38 +00:00
await bot . send_message ( message . channel , " ⚠ Sintassi del comando non valida. \n `!roll <max>` " )
2017-03-24 11:24:39 +00:00
return
# Roll the dice!
2017-03-26 12:10:32 +00:00
await bot . send_message ( message . channel , f " *Numero generato:* { random . randrange ( 0 , int ( arguments [ 0 ] ) ) + 1 } " )
2017-03-24 11:24:39 +00:00
2017-03-24 11:27:54 +00:00
2017-03-30 10:13:10 +00:00
async def register_telegram ( bot , update , arguments ) :
""" Registrati al database Royal Games!
2017-03-28 15:43:07 +00:00
2017-03-30 10:13:10 +00:00
Sintassi : ` / register < username > < password > ` """
# One account per user only!
if currently_logged_in ( update ) is not None :
await update . message . reply ( bot , " ⚠ Sei già registrato! " )
2017-03-28 15:43:07 +00:00
return
2017-03-30 10:13:10 +00:00
# Check if the username is printable
if not arguments [ 0 ] . isprintable ( ) :
await update . message . reply ( bot , " ⚠ Username non valido. " )
2017-03-28 15:43:07 +00:00
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 :
2017-03-30 10:13:10 +00:00
session , logged_user = database . login ( arguments [ 0 ] , arguments [ 1 ] )
logged_user . telegram_id = update . message . sent_from . user_id
session . commit ( )
print ( f " { logged_user } ha sincronizzato l ' account di Telegram. " )
await update . message . reply ( bot , f " Sincronizzazione riuscita! \n Sei loggato come ` { logged_user } `. " , parse_mode = " Markdown " )
2017-03-28 15:43:07 +00:00
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 < username > ` """
# 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 <username>` " , parse_mode = " Markdown " )
return
2017-03-29 07:44:32 +00:00
# Find the specified user
session , user = database . find_user ( arguments [ 0 ] )
2017-03-28 15:43:07 +00:00
# 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 " )
2017-03-09 11:51:45 +00:00
if __name__ == " __main__ " :
2017-03-30 10:53:55 +00:00
# Init universal bot commands
b . commands [ " start " ] = start
d . commands [ " start " ] = start
b . commands [ " diario " ] = diario
d . commands [ " diario " ] = diario
2017-03-18 20:32:34 +00:00
# Init Telegram bot commands
2017-03-24 11:24:39 +00:00
b . commands [ " leggi " ] = leggi_telegram
b . commands [ " discord " ] = discord_telegram
2017-03-22 18:13:22 +00:00
b . commands [ " sync " ] = sync_telegram
2017-03-24 11:24:39 +00:00
b . commands [ " changepassword " ] = changepassword_telegram
b . commands [ " help " ] = help_telegram
b . commands [ " markov " ] = markov_telegram
b . commands [ " cv " ] = cv_telegram
b . commands [ " roll " ] = roll_telegram
2017-03-30 10:13:10 +00:00
b . commands [ " register " ] = register_telegram
2017-03-28 15:43:07 +00:00
b . commands [ " toggleroyal " ] = toggleroyal_telegram
2017-03-22 18:13:22 +00:00
# Init Discord bot commands
d . commands [ " sync " ] = sync_discord
2017-03-24 11:24:39 +00:00
d . commands [ " roll " ] = roll_discord
2017-03-24 11:27:54 +00:00
d . commands [ " help " ] = help_discord
2017-03-24 11:30:34 +00:00
d . commands [ " leggi " ] = leggi_discord
2017-03-18 20:32:34 +00:00
# Init Telegram bot
loop . create_task ( b . run ( ) )
print ( " Telegram bot start scheduled! " )
# Init Discord bot
2017-03-22 18:13:22 +00:00
loop . create_task ( d . run ( ) )
2017-03-18 20:32:34 +00:00
print ( " Discord bot start scheduled! " )
# Run everything!
loop . run_forever ( )