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

Merge branch 'master' into primo

# Conflicts:
#	grandbot.py
This commit is contained in:
Steffo 2017-03-28 17:49:14 +02:00
commit a4547019ee
6 changed files with 143 additions and 42 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,11 +125,13 @@ 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
# Open the file
file = open("diario.txt", "r")
file = open("diario.txt", "r", encoding="utf8")
# Split the data in lines
entries = file.read().split("\n")
file.close()
@ -159,7 +165,7 @@ async def leggi_discord(bot, message, arguments):
await bot.send_message(message.channel, "⚠ Sintassi del comando non valida.\n`!leggi <random | numerofrase>`")
return
# Open the file
file = open("diario.txt", "r")
file = open("diario.txt", "r", encoding="utf8")
# Split the data in lines
entries = file.read().split("\n")
file.close()
@ -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,13 +233,15 @@ 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:
await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/help [comando]`", parse_mode="Markdown")
else:
if arguments[0] in b.commands:
await update.message.reply(bot, b.commands[arguments[0]].__doc__, parse_mode="Markdown")
await update.message.reply(bot, b.commands[arguments[0] + "_telegram"].__doc__, parse_mode="Markdown")
else:
await update.message.reply(bot, "⚠ Il comando specificato non esiste.")
@ -246,7 +256,7 @@ Sintassi: `!help [comando]`"""
bot.send_message(message.channel, "⚠ Sintassi del comando non valida.\n`!help [comando]`")
else:
if arguments[0] in b.commands:
bot.send_message(message.channel, b.commands[arguments[0]].__doc__)
bot.send_message(message.channel, b.commands[arguments[0] + "_discord"].__doc__)
else:
bot.send_message(message.channel, "⚠ Il comando specificato non esiste.")
@ -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
@ -493,13 +511,15 @@ 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) != 0:
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, 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):
@ -507,13 +527,77 @@ async def roll_discord(bot, message, arguments):
Sintassi: `!roll <max>`"""
# Check the command syntax
if len(arguments) != 0:
if len(arguments) != 1:
await bot.send_message(message.channel, "⚠ Sintassi del comando non valida.\n`!roll <max>`")
return
# Roll the dice!
await bot.send_message(message.channel, f"*Numero generato:* {random.randrange(0, arguments[0]) + 1}")
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,6 +610,8 @@ 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
# Init Discord bot commands
d.commands["sync"] = sync_discord

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):
@ -139,15 +143,11 @@ class Bot:
# Send the request to the Telegram API
token = self.token
async with session.request("GET", f"https://api.telegram.org/bot{token}/{endpoint}", params=params) as response:
# Check for errors in the request
if response.status != 200:
raise TelegramAPIError(f"Request returned {response.status} {response.reason}\n{response.text()}")
# Parse the json data as soon it's ready
data = await response.json()
# Check for errors in the response
if not data["ok"]:
error = data["description"]
raise TelegramAPIError(f"Response returned an error: {error}")
# Check for errors in the request
if response.status != 200 or not data["ok"]:
raise TelegramAPIError(data["error_code"], data["description"])
# Return a dictionary containing the data
return data["result"]
@ -232,6 +232,28 @@ class Chat:
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:
def __init__(self, user_dict):
self.user_id = user_dict["id"]