itte/ramenkan.py

180 lines
6.5 KiB
Python
Executable File

from mastodon import Mastodon
from random import randint
from itte import IRC, Util
class Ramen:
"""Requests with a ramen theme."""
def main(self):
"""Instantiate an IRC object and attach the listeners."""
# Load yaml sources used by request handlers
self.util = Util()
self.rand = self.util.rand
self.misc = self.util.yml("ramenkan/misc.yml")
self.links = self.util.yml("ramenkan/links.yml")
self.photos = self.util.yml("ramenkan/photos.yml")
self.dishes = self.util.yml("ramenkan/dishes.yml")
# Init irc object
self.irc = IRC()
self.cfg = self.irc.config("ramenkan/config.yml")
# Init mastodon object
self.masto = Mastodon(
api_base_url=self.cfg["mastodon"]["base_url"],
access_token=self.cfg["mastodon"]["access_token"],
client_id=self.cfg["mastodon"]["client_id"],
client_secret=self.cfg["mastodon"]["client_secret"]
)
# Init request listeners
self.irc.run(self.add_listeners)
def add_listeners(self, cxt):
"""Map triggers to handlers."""
self.irc.listen(cxt, "exit " + self.cfg["admin"]["code"], self.quit, \
admin=True)
self.irc.listen(cxt, "rollcall", self.rollcall)
self.irc.listen(cxt, "help", self.rollcall)
self.irc.listen(cxt, "water " + self.cfg["bot_nick"], self.water)
self.irc.listen(cxt, "botsnack " + self.cfg["bot_nick"], self.botsnack)
self.irc.listen(cxt, "ramen", self.ramen)
self.irc.listen(cxt, "vramen", self.ramen_veggie)
self.irc.listen(cxt, "rk", self.ramen)
self.irc.listen(cxt, "rkveg", self.ramen_veggie)
self.irc.listen(cxt, "rklink", self.link)
self.irc.listen(cxt, "rkselfie", self.selfie)
self.irc.listen(cxt, "rktoot", self.toot)
self.irc.listen(cxt, "rkvtoot", self.toot_veggie)
def quit(self, cxt):
"""Disconnect from the server and quit."""
self.irc.disconnect("Okay, okay, I'll leave. (´・ω・`)", "noodling off")
def rollcall(self, cxt):
"""Handle request for app info."""
self.irc.reply(cxt, self.misc["rollcall"])
def water(self, cxt):
"""Handle water offer."""
resp = self.misc["water"]
for index, r in enumerate(resp):
if "{{ nick }}" in r:
resp.append(r.replace("{{ nick }}", cxt["msg"]["nick"]))
resp.pop(index)
self.irc.reply(cxt, self.rand(resp))
def botsnack(self, cxt):
"""Handle snack offer."""
self.irc.reply(cxt, self.rand(self.misc["botsnack"]))
def make_ramen_combo(self, *args, **kwargs):
"""Generate a ramen dish. Optionally pass `veggie=True` for a
vegetarian dish."""
dish = self.dishes
# Check vegetarian flag
is_veggie = kwargs.get("veggie", False)
combo = "ramen in "
# Noodle type and broth richness
if randint(0, 1):
combo = self.rand(dish["noodle-shape"]) + ", " + \
self.rand(dish["noodle-broth-type"]) + " "
combo = combo.capitalize()
# Broth type
if is_veggie:
combo += self.rand(dish["broth-veggie"])
else:
combo += self.rand(dish["broth"] + dish["broth-veggie"])
combo += " broth"
# Topping
n_top = randint(2, 5)
for n in range(2, n_top+1):
if n == 2:
if is_veggie:
prev_top = self.rand(dish["topping"])
else:
prev_top = self.rand(dish["topping"] + dish["meat"])
combo += " with " + prev_top
else:
if n > 2 and n < n_top:
combo += ", "
else:
combo += " and "
if is_veggie:
next_top = self.rand(dish["topping"])
else:
next_top = self.rand(dish["topping"])
# Check for duplicate
if next_top == prev_top:
next_top = self.rand(dish["topping"])
combo += next_top
prev_top = next_top
# Condiment and side dish
if randint(0, 1):
combo += ", sprinkled with " + self.rand(dish["condiment"]) + "."
else:
combo += "."
if randint(0, 1):
combo += " Served with "
if is_veggie:
combo += self.rand(dish["tapa-veggie"]) + "."
else:
combo += self.rand(dish["tapa"] + dish["tapa-veggie"]) + "."
return combo
def pick_ramen(self, *args, **kwargs):
"""Pick a ramen dish. Optionally set vegetarian selection with
`veggie=True`."""
roll = randint(1, 100)
veggie = kwargs.get("veggie", False)
# 1% possibility of regional preset
if roll == 100 and veggie:
pick = (self.dishes["set-veggie"] + ".").capitalize()
elif roll == 100 and not veggie:
pick = (self.rand(self.dishes["set"] + \
self.dishes["set-veggie"]) + ".").capitalize()
elif roll <= 99 and veggie:
pick = self.make_ramen_combo(veggie=True)
else:
pick = self.make_ramen_combo()
return pick
def ramen(self, cxt):
"""Handle ramen request."""
self.irc.reply(cxt, self.pick_ramen())
def ramen_veggie(self, cxt):
"""Handle vegetarian ramen request."""
self.irc.reply(cxt, self.pick_ramen(veggie=True))
def link(self, cxt):
"""Handle to display a titled link."""
index = randint(0, len(self.links)-1)
self.irc.reply(cxt, self.links[index]["title"] + " " + \
self.links[index]["link"])
def selfie(self, cxt):
"""Handle to display a photo link."""
self.irc.reply(cxt, self.rand(self.photos["ticket"]))
def toot_wrapper(self, cxt, txt):
"""Wrap around the Mastodon library's toot command for basic error
handling."""
try:
self.masto.toot(txt)
self.irc.reply(cxt, txt + " " + self.misc["toot"]["success"])
except:
self.irc.reply(cxt, self.misc["toot"]["error"])
pass
def toot(self, cxt):
"""Handle post ramen to Mastodon."""
self.toot_wrapper(cxt, self.pick_ramen())
def toot_veggie(self, cxt):
"""Handle post veggie ramen to Mastodon."""
self.toot_wrapper(cxt, self.pick_ramen(veggie=True))
app = Ramen()
app.main()