feat: add !q and !qadd commands
This commit is contained in:
parent
e152aa5f28
commit
e01d9e4082
4 changed files with 89 additions and 3 deletions
|
|
@ -17,6 +17,8 @@ class Commands(StrEnum):
|
|||
GENERATE = "!g"
|
||||
BLACKLIST = "!blacklist"
|
||||
GENERATE_HELP = "!ghelp"
|
||||
QUOTE = "!q"
|
||||
QUOTE_ADD = "!qadd"
|
||||
|
||||
|
||||
class MarkovChain:
|
||||
|
|
@ -160,6 +162,45 @@ class MarkovChain:
|
|||
"Attempted to output automatic generation message, but there is not enough learned information yet.",
|
||||
)
|
||||
|
||||
def _command_quote(self):
|
||||
"""Retrieve a random quote from the `quotes` table and format it as
|
||||
|
||||
> «<quote>» - <author>
|
||||
"""
|
||||
data = self.db.execute(
|
||||
"SELECT quote, author FROM quotes ORDER BY RANDOM() LIMIT 1;", fetch=True
|
||||
)
|
||||
if data:
|
||||
data = data[0]
|
||||
quote, author = data[0], data[1]
|
||||
self.ws.send_message(f"«{quote}» - {author}")
|
||||
|
||||
def _command_add_quote(self, message: str):
|
||||
"""Add a quote to the quotes table. The message should follow the format:
|
||||
|
||||
!qadd quote author
|
||||
|
||||
The last word will be parsed as the author and anything in between !qadd and the author will be considered
|
||||
as the quote itself
|
||||
"""
|
||||
# Split the message into quote and author
|
||||
parts = message.split()
|
||||
author = parts[-1]
|
||||
quote = " ".join(parts[1:-1])
|
||||
|
||||
data = self.db.execute(
|
||||
"SELECT 1 FROM quotes WHERE quote = ?", (quote,), fetch=True
|
||||
)
|
||||
if data:
|
||||
self.ws.send_message(f"Quote «{quote}» was already added.")
|
||||
return
|
||||
|
||||
self.db.execute(
|
||||
"INSERT INTO quotes (quote, author) VALUES (?, ?)",
|
||||
(quote, author), # type: ignore[arg-type]
|
||||
)
|
||||
self.ws.send_message(f"Quote «{quote}» by {author} added.")
|
||||
|
||||
def store_sentence(self, message: str):
|
||||
logger.info(f"Processing {message} in order to store it")
|
||||
stripped_message = message.strip()
|
||||
|
|
@ -206,7 +247,7 @@ class MarkovChain:
|
|||
# Add <END> at the end of the sentence
|
||||
self.db.add_rule_queue([*key, self.end_tag])
|
||||
|
||||
def message_handler(self, message: Message): # noqa: C901, PLR0912
|
||||
def message_handler(self, message: Message): # noqa: C901, PLR0911, PLR0912
|
||||
try:
|
||||
if not message.user or message.user in self.s.denied_users:
|
||||
logger.debug(f"User {message.user} can't send messages")
|
||||
|
|
@ -264,6 +305,28 @@ class MarkovChain:
|
|||
message=message.message,
|
||||
username=message.user,
|
||||
)
|
||||
|
||||
case Commands.QUOTE:
|
||||
if not self._enabled:
|
||||
logger.info("Bot not enabled, skipping")
|
||||
return
|
||||
if message.user not in self.s.denied_users:
|
||||
logger.info(
|
||||
f"User {message.user} allowed to generate, executing _command_quote()",
|
||||
)
|
||||
self._command_quote()
|
||||
|
||||
case Commands.QUOTE_ADD:
|
||||
if self.is_mod(message.user, message.channel):
|
||||
logger.info(
|
||||
f"User {message.user} allowed to create quote, executing _command_quote()",
|
||||
)
|
||||
self._command_add_quote(message.message)
|
||||
return
|
||||
self.ws.send_message(
|
||||
f"@{message.user} you're not in the modlist, you can't add quotes"
|
||||
)
|
||||
|
||||
case _:
|
||||
logger.debug(
|
||||
f"Not a command: {msgs[0]}. Storing into db as a plain message",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue