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

Merge remote-tracking branch 'origin/primo' into primo

# Conflicts:
#	grandbot.py
This commit is contained in:
Steffo 2017-03-29 09:46:35 +02:00
commit 2c19679536
6 changed files with 179 additions and 41 deletions

7
.travis.yml Normal file
View file

@ -0,0 +1,7 @@
language: python
python:
- "3.6"
install:
- pip install -r requirements.txt
script:
- python3.6 -m py_compile grandbot.py

View file

@ -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.

View file

@ -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

View file

@ -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 <frase>`"""
# 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 <numero>`.
Sintassi: `/leggi <random | numerofrase>`"""
# 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 <random | numerofrase>`", parse_mode="Markdown")
return
@ -152,9 +158,9 @@ Sintassi: `/leggi <random | numerofrase>`"""
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 <numero>`.
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>`.
Sintassi: `!leggi <random | numerofrase>`"""
Sintassi: `!leggi <random | numerofrase>`"""
if len(arguments) == 0 or len(arguments) > 1:
await bot.send_message(message.channel, "⚠ Sintassi del comando non valida.\n`!leggi <random | numerofrase>`")
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 <messaggio>`"""
# 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 <username> <password>`"""
# 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 <username> <password>`", 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 <newpassword>`"""
# 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 <oldpassword> <newpassword>`", 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 <username> <coins>`"""
# 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 <username> <coins>`", 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 <max>`"""
# 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 <max>`",
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 <max>`"""
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 <username> <password>`"""
# 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 <username> <password>`", 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 <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
# 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!")

6
requirements.txt Normal file
View file

@ -0,0 +1,6 @@
aiohttp
async_timeout
markovify
discord.py[voice]
sqlalchemy
bcrypt

View file

@ -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]}")
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: