1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00
royalnet/keipack/utils/conversation.py

112 lines
3.8 KiB
Python
Raw Normal View History

2019-12-12 23:02:49 +00:00
import re
from typing import *
2019-12-09 23:39:33 +00:00
from royalnet.commands import CommandInterface
2019-12-12 23:02:49 +00:00
from royalnet.utils import *
2019-12-09 01:22:29 +00:00
from .emotion import Emotion
2019-12-12 23:02:49 +00:00
from ..tables import KeiPerson, KeiMessage
from ..utils import any_in_string
2019-12-09 01:22:29 +00:00
2019-12-09 23:39:33 +00:00
2019-12-09 01:22:29 +00:00
class Conversation:
2019-12-09 23:39:33 +00:00
def __init__(self, interface: CommandInterface):
2019-12-09 01:22:29 +00:00
self.generator = self._generator()
2019-12-09 23:39:33 +00:00
self.interface: CommandInterface = interface
2019-12-12 23:02:49 +00:00
self._person: Optional[KeiPerson] = None
self._message: Optional[KeiMessage] = None
self._previous: Optional[str] = None
2019-12-09 23:39:33 +00:00
self._session = None
2019-12-09 01:22:29 +00:00
async def _generator(self):
yield
raise NotImplementedError()
@classmethod
2019-12-09 23:39:33 +00:00
async def create(cls, interface: CommandInterface):
conv = cls(interface=interface)
2019-12-09 01:22:29 +00:00
await conv.generator.asend(None)
return conv
2019-12-10 01:24:58 +00:00
async def next(self, session, person, message, previous):
2019-12-09 23:39:33 +00:00
self._session = session
self._person = person
self._message = message
2019-12-10 01:24:58 +00:00
self._previous = previous
2019-12-09 23:39:33 +00:00
reply = await self.generator.asend(None)
2019-12-09 01:22:29 +00:00
return reply
# noinspection PyTupleAssignmentBalance
class ExampleConversation(Conversation):
async def _generator(self):
2019-12-09 23:39:33 +00:00
yield
response = await self.interface.call_herald_event("discord", "discord_cv")
yield Emotion.SURPRISED, f"Ci sono {len(response['guild']['members'])} persone in RYG."
yield Emotion.NEUTRAL, "Questa è una conversazione di prova."
yield await ExampleConversation.create(self.interface)
yield Emotion.WORRIED, "Questo non dovrebbe mai venire fuori."
2019-12-12 23:02:49 +00:00
# noinspection PyTupleAssignmentBalance
class FirstConversation(Conversation):
async def _generator(self):
yield
yield Emotion.NEUTRAL, "Ciao!"
yield Emotion.QUESTION, "Come sei arrivato qui...?"
yield Emotion.HAPPY, "Capisco... Ad ogni modo, sono Kei! Tu come ti chiami?"
yield NameConversation.create(self.interface)
class NameConversation(Conversation):
async def _generator(self):
yield
while True:
name = self._message.message.strip().strip(".,;:!?").lower()
name = re.sub(r"\s*mi\s*chiamo\s*", "", name)
name = re.sub(r"\s*il\s*mio\s*nome\s*[eèé]\s*", "", name)
name = re.sub(r"\s*sono\s*", "", name)
name = re.sub(r"\W", "", name)
if name == "kei":
yield Emotion.SURPRISED, "Davvero ti chiami come me?\n" \
"Perche' non mi dici un nome diverso?\n" \
"Altrimenti rischiamo di confonderci..."
continue
self._person.name = name
await asyncify(self._session.commit())
break
yield Emotion.GRIN, f"O-kei! {self._person.name}!"
yield Emotion.HAPPY, "Saro' sempre a tua disposizione quando mi vorrai dire qualcosa!"
yield Emotion.QUESTION, "Pero' prima ti vorrei chiedere un favore..."
yield Emotion.NEUTRAL, "Qualcuno ha criptato con delle password tutti i miei file...\n" \
"Se ne trovi qualcuna in giro, potresti dirmela?\n"
while True:
if self._message.message == "no":
yield Emotion.CRY, "Non farmi questo... Per piacere, accetta!"
else:
break
yield Emotion.HAPPY, "Grazie! Prometto che quando riavro' tutto ti ricompensero' adeguatamente!"
class MainConversation(Conversation):
async def _generator(self):
yield
yield Emotion.HAPPY, "Di cosa vuoi parlare?"
while True:
msg = self._message.message
if any_in_string([r"gatt[oiae]", "ny[ae]+", "mi+a+o+", "me+o+w+", "felin[oi]", "mici[ao]"], msg):
yield Emotion.CAT, "Nyan!"
else:
yield Emotion.QUESTION, "...?"