Edm0nd/edmond/plugins/ambience.py

98 lines
3.2 KiB
Python
Raw Normal View History

2021-12-12 17:30:14 +01:00
"""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 = " ".join(contents[1:])
2021-12-12 17:30:14 +01:00
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))