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

Add /dndmarkov

This commit is contained in:
Steffo 2019-02-15 13:35:51 +01:00
parent f814987ccc
commit 457956f3b5
2 changed files with 42 additions and 8 deletions

2
.gitignore vendored
View file

@ -7,5 +7,5 @@ libopus-0.dll
music.opus music.opus
opusfiles/ opusfiles/
ignored/ ignored/
markovmodel.json markovmodels/
logs/ logs/

View file

@ -26,12 +26,17 @@ import time
IKMarkup = telegram.InlineKeyboardMarkup IKMarkup = telegram.InlineKeyboardMarkup
IKButton = telegram.InlineKeyboardButton IKButton = telegram.InlineKeyboardButton
# Markov model # Markov models
try: try:
with open("markovmodel.json") as file: with open("markovmodels/default.json") as file:
model = markovify.Text.from_json(file.read()) default_model = markovify.Text.from_json(file.read())
except Exception: except Exception:
model = None default_model = None
try:
with open("markovmodels/dnd4.json") as file:
dnd4_model = markovify.Text.from_json(file.read())
except Exception:
dnd4_model = None
logging.getLogger().disabled = True logging.getLogger().disabled = True
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -677,14 +682,14 @@ def cmd_calendar(bot: telegram.Bot, update: telegram.Update, session: db.Session
@command @command
def cmd_markov(bot: telegram.Bot, update: telegram.Update): def cmd_markov(bot: telegram.Bot, update: telegram.Update):
if model is None: if default_model is None:
reply(bot, update, strings.MARKOV.ERRORS.NO_MODEL) reply(bot, update, strings.MARKOV.ERRORS.NO_MODEL)
return return
try: try:
first_word = update.message.text.split(" ")[1] first_word = update.message.text.split(" ")[1]
except IndexError: except IndexError:
# Any word # Any word
sentence = model.make_sentence(tries=1000) sentence = default_model.make_sentence(tries=1000)
if sentence is None: if sentence is None:
reply(bot, update, strings.MARKOV.ERRORS.GENERATION_FAILED) reply(bot, update, strings.MARKOV.ERRORS.GENERATION_FAILED)
return return
@ -692,7 +697,7 @@ def cmd_markov(bot: telegram.Bot, update: telegram.Update):
return return
# Specific word # Specific word
try: try:
sentence = model.make_sentence_with_start(first_word, tries=1000) sentence = default_model.make_sentence_with_start(first_word, tries=1000)
except KeyError: except KeyError:
reply(bot, update, strings.MARKOV.ERRORS.MISSING_WORD) reply(bot, update, strings.MARKOV.ERRORS.MISSING_WORD)
return return
@ -702,6 +707,34 @@ def cmd_markov(bot: telegram.Bot, update: telegram.Update):
reply(bot, update, sentence) reply(bot, update, sentence)
@command
def cmd_dndmarkov(bot: telegram.Bot, update: telegram.Update):
if dnd4_model is None:
reply(bot, update, strings.MARKOV.ERRORS.NO_MODEL)
return
try:
first_word = update.message.text.split(" ")[1]
except IndexError:
# Any word
sentence = dnd4_model.make_sentence(tries=1000)
if sentence is None:
reply(bot, update, strings.MARKOV.ERRORS.GENERATION_FAILED)
return
reply(bot, update, sentence)
return
# Specific word
try:
sentence = dnd4_model.make_sentence_with_start(first_word, tries=1000)
except KeyError:
reply(bot, update, strings.MARKOV.ERRORS.MISSING_WORD)
return
if sentence is None:
reply(bot, update, strings.MARKOV.ERRORS.SPECIFIC_WORD_FAILED)
return
reply(bot, update, sentence)
def exec_roll(roll) -> str: def exec_roll(roll) -> str:
result = int(roll.evaluate()) result = int(roll.evaluate())
string = "" string = ""
@ -767,6 +800,7 @@ def process(arg_discord_connection):
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(CommandHandler("markov", cmd_markov))
u.dispatcher.add_handler(CommandHandler("dndmarkov", cmd_dndmarkov))
u.dispatcher.add_handler(CommandHandler("roll", cmd_roll)) u.dispatcher.add_handler(CommandHandler("roll", cmd_roll))
u.dispatcher.add_handler(CommandHandler("r", cmd_roll)) u.dispatcher.add_handler(CommandHandler("r", cmd_roll))
u.dispatcher.add_handler(CommandHandler("mm", cmd_mm)) u.dispatcher.add_handler(CommandHandler("mm", cmd_mm))