from obspython import * from services.Service import Service import random import re KNOWN_COMMANDS = ["lastrolls", "clearrolls"] USER_KEY = "last_dice_roll" PATTERN_DICE = "^([0-9]+)d([0-9]+)$" class DiceService(Service): title = "Dice Service" enabled = False msg_cleared = "" # Service ----- def knows(self, command): return command in KNOWN_COMMANDS or re.match(PATTERN_DICE, command) def eval(self, command, response, users): if command == "lastrolls": self.queue.append(self.get_last_rolls(users)) elif command == "clearrolls": self.queue.append(self.clear_rolls(users) + f", @{response['username']}") else: m = re.match(PATTERN_DICE, command) dices = self.roll_dices(int(m.group(1)), int(m.group(2))) self.save_last_roll(users, response["username"], dices) self.queue.append(f"🎲 {response['username']}: {' + '.join(dices)} 🎲") def list_commands(self): return ["d (n=nombre de dés, f=nombre de faces)"] + KNOWN_COMMANDS # OBS subset ----- def create_properties(self): props = obs_properties_create() obs_properties_add_text(props, "dice_msg_cleared", "Message: rolls cleared", OBS_TEXT_DEFAULT) return props def update(self, settings): self.msg_cleared = obs_data_get_string(settings, "dice_msg_cleared") # Commands ----- def get_last_rolls(self, users): results = [] for user in users: if USER_KEY in users[user]: results.append(f"{user}: {' + '.join(users[user][USER_KEY])}") return " 🎲".join(results) def clear_rolls(self, users): for username in users: if USER_KEY in users[username]: users[username].pop(USER_KEY) return self.msg_cleared def roll_dices(self, number, faces): dices = [] for i in range(0, number): dices.append(str(random.randint(1, faces))) return (dices) def save_last_roll(self, users, username, dices): if username not in users: users[username] = {} users[username][USER_KEY] = dices