From 2af36ef42995d5c6844f1d9e8642b145390f4445 Mon Sep 17 00:00:00 2001 From: dece Date: Sun, 12 Dec 2021 17:30:14 +0100 Subject: [PATCH] ambience: add plugin --- config.json.example | 13 +++++ edmond/plugins/ambience.py | 97 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 edmond/plugins/ambience.py diff --git a/config.json.example b/config.json.example index bfbce51..73b4c20 100644 --- a/config.json.example +++ b/config.json.example @@ -9,6 +9,19 @@ "resources_dir": "resources", "error_message": "An error occured, sorry!", "plugins": { + "ambience": { + "commands": ["ambience"], + "themes": { + "space": "đŸŒđŸŒ âœšđŸ’«đŸŒŒđŸŽ‘đŸ›žđŸš€đŸ›°ïžđŸȘ", + "nature": "🌬đŸŒșđŸŒžđŸ’źđŸŒżđŸŒŸđŸ đŸ‹đŸŠˆ", + "dancefloor": "đŸ„”đŸ‘ đŸ”„đŸ•șđŸ’ƒđŸ™ŒâœŠđŸ€šđŸ–", + "love": "đŸ€§đŸ˜‚đŸ€—â™„â€âŁđŸ’•đŸ’žđŸ’“đŸ”„đŸŒ¶đŸ’˜đŸ’–đŸ’—", + "music": "đŸŽ¶đŸŽ”", + "misc": "đŸ”đŸ”ƒđŸ”„â«đŸ†™đŸ§đŸĄđŸŠžđŸŠčđŸ§žđŸŒ‹đŸŒƒđŸ™ïžđŸŒ„đŸŒ…đŸŒ‡đŸŽ†đŸ—żđŸŽ‡" + }, + "effect_rate": 33, + "target_word": "for" + }, "beers": { "commands": ["beer"], "beers": ["Paix-Dieu"], diff --git a/edmond/plugins/ambience.py b/edmond/plugins/ambience.py new file mode 100644 index 0000000..f63d065 --- /dev/null +++ b/edmond/plugins/ambience.py @@ -0,0 +1,97 @@ +"""A State Of Trance chat simulator. + +2 to 3 chunks per message. Chunks can receive one effect (low proc). + +Chunks: +- repeat same thing (3 to 6) +- interleave two or three things of one theme, repeat (2 to 4) +- repeat three or four things of one theme (3 to 4 each) + +Effects: +- interleave a word with one thing +- just add a word somewhere + +Words are actually the requester's name but it could be extended. +""" + +import random + +from edmond.plugin import Plugin +from edmond.utils import proc + + +class AmbiencePlugin(Plugin): + + REQUIRED_CONFIGS = ["commands", "themes", "effect_rate", "target_word"] + + def __init__(self, bot): + super().__init__(bot) + + @property + def themes(self): + return self.config["themes"] + + def on_pubmsg(self, event): + if not self.should_handle_command(event.arguments[0]): + return False + target = None + contents = self.command.content.split() + self.bot.log_d(f"{contents}") + if len(contents) == 2 and contents[0] == self.config["target_word"]: + target = contents[1] + self.bot.log_d(f"{target}") + self.bot.say(event.target, self.get_reply(event, target=target)) + return True + + def get_reply(self, event, target=None): + reply = "" + num_chunks = random.randint(2, 3) + if target is not None or proc(self.config["effect_rate"]): + effect_index = random.randint(0, num_chunks - 1) + effect_word = (target or event.source.nick).upper() + else: + effect_index = -1 + for i in range(num_chunks): + chunk = self.get_chunk() + if i == effect_index: + chunk = self.apply_effect(chunk, effect_word) + reply += chunk + return reply + + def get_chunk(self): + return { + 1: self.repeat_same, + 2: self.interleave, + 3: self.repeat_from_theme, + }[random.randint(1, 3)]() + + def repeat_same(self): + random_theme_values = random.choice(list(self.themes.values())) + random_item = random.choice(random_theme_values) + return random_item * random.randint(3, 6) + + def interleave(self): + random_theme_values = random.choice(list(self.themes.values())) + sample_size = min(len(random_theme_values), random.randint(2, 4)) + random_items = "".join(random.sample(random_theme_values, sample_size)) + return random_items * random.randint(2, 4) + + def repeat_from_theme(self): + random_theme_values = random.choice(list(self.themes.values())) + sample_size = min(len(random_theme_values), random.randint(3, 4)) + random_items = random.sample(random_theme_values, sample_size) + reps = random.randint(3, 4) + return "".join(item * reps for item in random_items) + + def apply_effect(self, chunk, word): + return { + 1: self.insert_word, + 2: self.interleave_word, + }[random.randint(1, 2)](chunk, word) + + def insert_word(self, chunk, word): + index = random.randint(0, len(chunk)) + return chunk[:index] + word + chunk[index:] + + def interleave_word(self, chunk, word): + return "".join(a + b for a, b in zip(chunk, word))