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

many things

This commit is contained in:
Steffo 2018-08-08 21:59:47 +02:00
parent 6510b59ecd
commit 483591f633
6 changed files with 50 additions and 10 deletions

1
.gitignore vendored
View file

@ -6,3 +6,4 @@ libopus-0.dll
music.opus music.opus
opusfiles/ opusfiles/
ignored/* ignored/*
markovmodel.json

3
db.py
View file

@ -891,7 +891,8 @@ class GameOrigins(enum.Enum):
class LibraryGame(Base): class LibraryGame(Base):
__tablename__ = "librarygames" __tablename__ = "librarygames"
owner_id = Column(Integer, ForeignKey("royals_id"), nullable=False) id = Column(BigInteger, primary_key=True)
owner_id = Column(Integer, ForeignKey("royals.id"), nullable=False)
owner = relationship("Royal", lazy="joined") owner = relationship("Royal", lazy="joined")
name = Column(String) name = Column(String)
platform = Column(String) platform = Column(String)

View file

@ -10,4 +10,5 @@ async_timeout
raven raven
bcrypt bcrypt
markdown2 markdown2
pygments pygments
markovify

View file

@ -1,6 +1,5 @@
import datetime import datetime
import random import random
import math
import typing import typing
import db import db
import errors import errors
@ -14,9 +13,20 @@ import os
import time import time
import cast import cast
import re import re
import logging
import configparser
import markovify
# Markov model
try:
with open("markovmodel.json") as file:
model = markovify.Text.from_json(file.read())
except Exception:
model = None
logging.getLogger().setLevel(level=20)
# Init the config reader # Init the config reader
import configparser
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read("config.ini") config.read("config.ini")
@ -561,8 +571,30 @@ def cmd_calendar(bot: Bot, update: Update):
bot.send_message(update.message.chat.id, msg, parse_mode="HTML", disable_web_page_preview=True) bot.send_message(update.message.chat.id, msg, parse_mode="HTML", disable_web_page_preview=True)
def cmd_markov(bot: Bot, update: Update):
if model is None:
bot.send_message(update.message.chat.id, "⚠️ Il modello Markov non è disponibile.")
return
try:
_, first_word = update.message.text.split(" ", 1)
except IndexError:
sentence = model.make_sentence(tries=1000)
if sentence is None:
bot.send_message(update.message.chat.id, "⚠ Complimenti! Hai vinto la lotteria di Markov!\n"
"O forse l'hai persa.\n"
"Non sono riuscito a generare una frase, riprova.")
return
bot.send_message(update.message.chat.id, sentence)
else:
sentence = model.make_sentence_with_start(first_word, tries=1000)
if sentence is None:
bot.send_message(update.message.chat.id, "⚠ Non è stato possibile generare frasi partendo da questa"
" parola.")
return
bot.send_message(update.message.chat.id, sentence)
def process(arg_discord_connection): def process(arg_discord_connection):
print("Telegrambot starting...")
if arg_discord_connection is not None: if arg_discord_connection is not None:
global discord_connection global discord_connection
discord_connection = arg_discord_connection discord_connection = arg_discord_connection
@ -586,7 +618,9 @@ def process(arg_discord_connection):
u.dispatcher.add_handler(CommandHandler("wheel", cmd_wheel)) u.dispatcher.add_handler(CommandHandler("wheel", cmd_wheel))
u.dispatcher.add_handler(CommandHandler("newevent", cmd_newevent)) u.dispatcher.add_handler(CommandHandler("newevent", cmd_newevent))
u.dispatcher.add_handler(CommandHandler("calendar", cmd_calendar)) u.dispatcher.add_handler(CommandHandler("calendar", cmd_calendar))
u.dispatcher.add_handler(CommandHandler("markov", cmd_markov))
u.dispatcher.add_handler(CallbackQueryHandler(on_callback_query)) u.dispatcher.add_handler(CallbackQueryHandler(on_callback_query))
logging.info("Handlers registered.")
u.bot.send_message(config["Telegram"]["main_group"], u.bot.send_message(config["Telegram"]["main_group"],
f" Royal Bot avviato e pronto a ricevere comandi!\n" f" Royal Bot avviato e pronto a ricevere comandi!\n"
f"Ultimo aggiornamento: `{version}: {commit_msg}`", f"Ultimo aggiornamento: `{version}: {commit_msg}`",
@ -594,11 +628,12 @@ def process(arg_discord_connection):
while True: while True:
try: try:
u.start_polling() u.start_polling()
logging.info("Polling started.")
u.idle() u.idle()
except telegram.error.TimedOut: except telegram.error.TimedOut:
print("Telegrambot timed out.") logging.warning("Timed out, restarting in 1 minute.")
time.sleep(60) time.sleep(60)
print("Telegrambot restarting...") logging.info("Now restarting...")
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -28,7 +28,7 @@
Bio Bio
</div> </div>
<div class="lower-box"> <div class="lower-box">
{{ css.bio }} {{ bio }}
</div> </div>
</div> </div>
{% endif %} {% endif %}

View file

@ -84,8 +84,10 @@ def page_profile(name: str):
tg = db_session.query(db.Telegram).filter_by(royal=user).one_or_none() tg = db_session.query(db.Telegram).filter_by(royal=user).one_or_none()
discord = db_session.execute(query_discord_music.one_query, {"royal": user.id}).fetchone() discord = db_session.execute(query_discord_music.one_query, {"royal": user.id}).fetchone()
db_session.close() db_session.close()
converted_bio = Markup(markdown2.markdown(css.bio.replace("<", "&lt;"),
extras=["spoiler", "tables", "smarty-pants", "fenced-code-blocks"]))
return render_template("profile.html", ryg=user, css=css, osu=osu, rl=rl, dota=dota, lol=lol, steam=steam, ow=ow, return render_template("profile.html", ryg=user, css=css, osu=osu, rl=rl, dota=dota, lol=lol, steam=steam, ow=ow,
tg=tg, discord=discord, config=config) tg=tg, discord=discord, config=config, bio=converted_bio)
@app.route("/login") @app.route("/login")
@ -248,7 +250,7 @@ def page_wiki(key: str):
f' modificata da' f' modificata da'
f' <a href="https://ryg.steffo.eu/profile/{user.username}">{user.username}</a>:' f' <a href="https://ryg.steffo.eu/profile/{user.username}">{user.username}</a>:'
f' {"<i>Nessun motivo specificato.</i>" if not edit_reason else edit_reason}\n', f' {"<i>Nessun motivo specificato.</i>" if not edit_reason else edit_reason}\n',
parse_mode="HTML") parse_mode="HTML", disable_web_page_preview=True, disable_notification=True)
except Exception: except Exception:
pass pass
return redirect(url_for("page_wiki", key=key)) return redirect(url_for("page_wiki", key=key))