From 6b73cccda0279524e1ca97bcad5a4867d9e0e3b6 Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Sat, 30 Nov 2024 23:18:35 -0800 Subject: [PATCH] markov: Add !markov help and help_timeout config * !markov help will display a pretty crude help message. It's funny to me, I dunno. * help_timeout config option limits the number of times that `!markov help` is allowed to run. This is because the help message is a lot of lines, and it could cause problems if `!markov help` is spammed or abused. It can be set to 0 or less to disable. Signed-off-by: Alek Ratzloff --- config.example.toml | 9 ++++++++- plugins/markov.py | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/config.example.toml b/config.example.toml index 2409611..60e172f 100644 --- a/config.example.toml +++ b/config.example.toml @@ -118,4 +118,11 @@ module = "plugins.markov" # Additionally, users may set their reply chance between 0.0 and the value they # set. e.g. with a value of 0.01, a user may set their reply chance to 0.001. # default: 0.01 -# reply_chance = 0.01 \ No newline at end of file +# reply_chance = 0.01 + +# help_timeout +# The number of seconds between a "help" command that's allowed. This is only +# here because running `!markov help` sends like 10 messages in a row and it's +# to prevent a denial-of-service attack. Generally you should not need to worry +# about changing this. +# help_timeout = 300 diff --git a/plugins/markov.py b/plugins/markov.py index 4aa7c80..793b158 100644 --- a/plugins/markov.py +++ b/plugins/markov.py @@ -1,17 +1,16 @@ -from collections import defaultdict import logging import math -from pathlib import Path import random import sqlite3 +import time +from collections import defaultdict +from pathlib import Path from typing import Any, DefaultDict, List, Sequence from asyncirc.protocol import IrcProtocol from irclib.parser import Prefix - from omnibot.plugin import Plugin - log = logging.getLogger(__name__) @@ -244,6 +243,8 @@ class Markov(Plugin): self.sql_path = Path(self.plugin_config.get("sql_path", "data/markov/db.sql")) self.reply_chance = float(self.plugin_config.get("reply_chance", 0.01)) self.chain = Chain(self.order, self.reply_chance, self.data_path, self.sql_path) + self.help_timeout_expire = 0 + self.help_timeout = self.plugin_config.get("help_timeout", 300) async def on_message(self, conn: IrcProtocol, channel: str, who: Prefix, line: str): line = line.strip() @@ -273,6 +274,34 @@ class Markov(Plugin): ): # handle markov commands match parts[1:]: + case ["help"]: + print(time.time()) + if self.help_timeout_expire > time.time(): + return + self.help_timeout_expire = time.time() + self.help_timeout + # TODO - add help messages config to the TOML file + help_messages = [ + "Markov is filmed in front of a live studio audience.", + "Help! I'm having a heart attack!", + "Where is that gun?", + "I'm walkin' heah!", + "Any resemblance to persons, living or fictional, is entirely coincidental.", + "ge8hg809wga987haw4go9hag897hagndfnam3n342ui128", + "intercal die", + ] + lines = [ + random.choice(help_messages), + "`!markov help` - idiot", + "`!markov force` - force markov to say something", + "`!markov force username` - force markov to say something, using someone else's words", + "`!markov trigger` - synonym for `!markov force`", + f"`!markov chance n` - set your reply chance to some number between 0.0 and {self.chain.reply_chance}", + "`!markov listen on|off` - tell markov to start or stop listening to what you say", + "`!markov all` - force markov to say something based off of everything said in the server", + "Also, kill yourself!", + ] + for line in lines: + self.send_to(conn, channel, line) case ["force"]: message = self.chain.generate(channel, who.nick) if message: