2024-01-30 21:36:36 +01:00
|
|
|
from obspython import *
|
|
|
|
from services.Service import Service
|
|
|
|
import os.path
|
|
|
|
import random
|
|
|
|
import re
|
|
|
|
|
|
|
|
KNOWN_COMMANDS = ["cmd"]
|
|
|
|
|
|
|
|
SEPARATOR = ";"
|
|
|
|
SUBST_SEP = ".,"
|
|
|
|
PATTERN_ARGS = "\{[0-9]+\}"
|
|
|
|
|
|
|
|
class CommandService(Service):
|
|
|
|
|
|
|
|
title = "Command Service"
|
|
|
|
enabled = False
|
|
|
|
|
|
|
|
msg_cant_manage = ""
|
|
|
|
msg_unknown = ""
|
|
|
|
msg_wrong_arg_count = ""
|
|
|
|
|
|
|
|
file = ""
|
|
|
|
commands = {}
|
|
|
|
|
|
|
|
# Service -----
|
|
|
|
|
|
|
|
def knows(self, command):
|
|
|
|
return command.lower() in self.list_commands()
|
|
|
|
|
|
|
|
def eval(self, command, response, users):
|
|
|
|
if command == "cmd":
|
|
|
|
self.queue.append(self.manage(response))
|
|
|
|
else:
|
|
|
|
self.queue.append(self.custom(response["message"]))
|
|
|
|
|
|
|
|
def list_commands(self):
|
|
|
|
return KNOWN_COMMANDS + list(self.commands.keys())
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
if self.file == "" or not os.path.isfile(self.file):
|
|
|
|
return
|
|
|
|
with open(self.file, "r", encoding="utf-8") as file:
|
|
|
|
content = file.read().splitlines()
|
|
|
|
for line in content:
|
|
|
|
self.load_command(line)
|
|
|
|
|
|
|
|
def load_command(self, line):
|
|
|
|
components = line.split(SEPARATOR)
|
|
|
|
self.commands[components[0].lower()] = components[1].replace(SUBST_SEP, SEPARATOR).split("||")
|
|
|
|
|
|
|
|
# OBS subset -----
|
|
|
|
|
|
|
|
def create_properties(self):
|
|
|
|
props = obs_properties_create()
|
|
|
|
obs_properties_add_path(props, "cmd_file", "Commands list", OBS_PATH_FILE, "Text file (*.txt)", None)
|
|
|
|
obs_properties_add_text(props, "cmd_msg_cant_manage", "Message: Unauthorized", OBS_TEXT_DEFAULT)
|
|
|
|
obs_properties_add_text(props, "cmd_msg_unknown", "Message: Unknown command", OBS_TEXT_DEFAULT)
|
|
|
|
obs_properties_add_text(props, "cmd_msg_wrong_arg_count", "Message: Wrong argument count", OBS_TEXT_DEFAULT)
|
|
|
|
return props
|
|
|
|
|
|
|
|
def update(self, settings):
|
|
|
|
self.file = obs_data_get_string(settings, "cmd_file")
|
|
|
|
|
|
|
|
# Commands -----
|
|
|
|
|
|
|
|
def manage(self, response):
|
|
|
|
can_manage = False
|
|
|
|
for badge in response["tags"]["badges"]:
|
|
|
|
if badge["name"] in ["broadcaster", "moderator"]:
|
|
|
|
can_manage = True
|
|
|
|
if not can_manage:
|
|
|
|
return f"{self.msg_cant_manage}, @{response['username']}"
|
|
|
|
components = response["message"].split(" ", 3)
|
|
|
|
if len(components) < 3:
|
|
|
|
return f"{self.msg_wrong_arg_count}, @{response['username']}"
|
|
|
|
command, action, target_command = components[0], components[1], components[2].lower()
|
|
|
|
if len(components) == 4:
|
|
|
|
args = components[3]
|
|
|
|
if action in ["add", "edit", "update"]:
|
|
|
|
return self.add_command(target_command, args) + f", @{response['username']}"
|
|
|
|
elif action in ["del", "delete", "remove"]:
|
|
|
|
return self.del_command(target_command) + f", @{response['username']}"
|
|
|
|
else:
|
|
|
|
return f"{self.msg_unknown}, @{response['username']}"
|
|
|
|
|
|
|
|
def add_command(self, command, args):
|
|
|
|
self.commands[command.lstrip("!")] = args.split("||")
|
|
|
|
self.write()
|
2024-03-26 14:57:42 +01:00
|
|
|
str_command = command.lstrip("!")
|
|
|
|
return f"La commande '!{str_command}' a été ajoutée"
|
2024-01-30 21:36:36 +01:00
|
|
|
|
|
|
|
def del_command(self, command):
|
|
|
|
if command not in self.commands:
|
|
|
|
return f"La commande '!{command}' n'existe pas"
|
|
|
|
self.commands.pop(command)
|
|
|
|
self.write()
|
|
|
|
return f"La commande '!{command}' a été supprimée"
|
|
|
|
|
|
|
|
def write(self):
|
|
|
|
with open(self.file, "w", encoding="utf-8") as file:
|
|
|
|
for name in self.commands:
|
|
|
|
file.write(f"{name}{SEPARATOR}{'||'.join(self.commands[name]).replace(SEPARATOR, SUBST_SEP)}\n")
|
|
|
|
|
|
|
|
def custom(self, message):
|
|
|
|
components = message.split(" ", 1)
|
|
|
|
command_name = components[0][1:].lower()
|
|
|
|
command = self.commands[command_name][random.randint(0, len(self.commands[command_name]) - 1)]
|
|
|
|
args = list(dict.fromkeys(re.findall(PATTERN_ARGS, command)))
|
|
|
|
args.sort()
|
|
|
|
count = len(args)
|
|
|
|
if count >= 1:
|
|
|
|
msg_components = []
|
|
|
|
if len(components) > 1:
|
|
|
|
msg_components = components[1].split(" ")
|
|
|
|
if len(msg_components) != count:
|
|
|
|
return self.msg_wrong_arg_count
|
|
|
|
for i in range(0, count):
|
|
|
|
command = command.replace(args[i], msg_components[i].lstrip("@"))
|
|
|
|
return command
|