diff --git a/torchlight_changes_unloze/torchlight3/Torchlight/CommandHandler.py b/torchlight_changes_unloze/torchlight3/Torchlight/CommandHandler.py index e9b73ffa..5171b87d 100755 --- a/torchlight_changes_unloze/torchlight3/Torchlight/CommandHandler.py +++ b/torchlight_changes_unloze/torchlight3/Torchlight/CommandHandler.py @@ -10,106 +10,110 @@ from importlib import reload from . import Commands class CommandHandler(): - def __init__(self, Torchlight): - self.Logger = logging.getLogger(__class__.__name__) - self.Torchlight = Torchlight - self.Commands = [] - self.NeedsReload = False + def __init__(self, Torchlight): + self.Logger = logging.getLogger(__class__.__name__) + self.Torchlight = Torchlight + self.Commands = [] + self.NeedsReload = False - def Setup(self): - Counter = len(self.Commands) - self.Commands.clear() - if Counter: - self.Logger.info(sys._getframe().f_code.co_name + " Unloaded {0} commands!".format(Counter)) + def Setup(self): + Counter = len(self.Commands) + self.Commands.clear() + if Counter: + self.Logger.info(sys._getframe().f_code.co_name + " Unloaded {0} commands!".format(Counter)) - Counter = 0 - for subklass in sorted(Commands.BaseCommand.__subclasses__(), key = lambda x: x.Order, reverse = True): - try: - Command = subklass(self.Torchlight) - if hasattr(Command, "_setup"): - Command._setup() - except Exception as e: - self.Logger.error(traceback.format_exc()) - else: - self.Commands.append(Command) - Counter += 1 + Counter = 0 + for subklass in sorted(Commands.BaseCommand.__subclasses__(), key = lambda x: x.Order, reverse = True): + try: + Command = subklass(self.Torchlight) + if hasattr(Command, "_setup"): + Command._setup() + except Exception as e: + self.Logger.error(traceback.format_exc()) + else: + self.Commands.append(Command) + Counter += 1 - self.Logger.info(sys._getframe().f_code.co_name + " Loaded {0} commands!".format(Counter)) + self.Logger.info(sys._getframe().f_code.co_name + " Loaded {0} commands!".format(Counter)) - def Reload(self): - try: - reload(Commands) - except Exception as e: - self.Logger.error(traceback.format_exc()) - else: - self.Setup() + def Reload(self): + try: + reload(Commands) + except Exception as e: + self.Logger.error(traceback.format_exc()) + else: + self.Setup() - async def HandleCommand(self, line, player): - Message = line.split(sep = ' ', maxsplit = 1) - if len(Message) < 2: - Message.append("") - Message[1] = Message[1].strip() + async def HandleCommand(self, line, player): + Message = line.split(sep = ' ', maxsplit = 1) + if len(Message) < 2: + Message.append("") + Message[1] = Message[1].strip() - if Message[1] and self.Torchlight().LastUrl: - Message[1] = Message[1].replace("!last", self.Torchlight().LastUrl) - line = Message[0] + ' ' + Message[1] + if Message[1] and self.Torchlight().LastUrl: + Message[1] = Message[1].replace("!last", self.Torchlight().LastUrl) + line = Message[0] + ' ' + Message[1] - Level = 0 - if player.Access: - Level = player.Access["level"] + Level = 0 + if player.Access: + Level = player.Access["level"] - RetMessage = None - Ret = None - for Command in self.Commands: - for Trigger in Command.Triggers: - Match = False - RMatch = None - if isinstance(Trigger, tuple): - if Message[0].lower().startswith(Trigger[0], 0, Trigger[1]): - Match = True - elif isinstance(Trigger, str): - if Message[0].lower() == Trigger.lower(): - Match = True - else: # compiled regex - RMatch = Trigger.search(line) - if RMatch: - Match = True + RetMessage = None + Ret = None + for Command in self.Commands: + for Trigger in Command.Triggers: + Match = False + RMatch = None + if isinstance(Trigger, tuple): + if Message[0].lower().startswith(Trigger[0], 0, Trigger[1]): + Match = True + elif isinstance(Trigger, str): + if Message[0].lower() == Trigger.lower(): + Match = True + else: # compiled regex + RMatch = Trigger.search(line) + if RMatch: + Match = True - if not Match: - continue + if not Match: + continue - self.Logger.debug(sys._getframe().f_code.co_name + " \"{0}\" Match -> {1} | {2}".format(player.Name, Command.__class__.__name__, Trigger)) + self.Logger.debug(sys._getframe().f_code.co_name + " \"{0}\" Match -> {1} | {2}".format(player.Name, Command.__class__.__name__, Trigger)) - if Level < Command.Level: - RetMessage = "You do not have access to this command! (You: {0} | Required: {1})".format(Level, Command.Level) - continue + if Level < Command.Level: + RetMessage = "You do not have access to this command! (You: {0} | Required: {1})".format(Level, Command.Level) + continue - try: - if RMatch: - Ret = await Command._rfunc(line, RMatch, player) - else: - Ret = await Command._func(Message, player) - except Exception as e: - self.Logger.error(traceback.format_exc()) - self.Torchlight().SayChat("Error: {0}".format(str(e))) + try: + if RMatch: + Ret = await Command._rfunc(line, RMatch, player) + else: + #self.Logger.debug(f"_rfunc line: {line}") + if line.startswith("!yt"): #transfering the line if its !yt or !yts in case people want to include pitch or tempo. + Ret = await Command._func(Message, player, line) + else: + Ret = await Command._func(Message, player) + except Exception as e: + self.Logger.error(traceback.format_exc()) + self.Torchlight().SayChat("Error: {0}".format(str(e))) - RetMessage = None + RetMessage = None - if isinstance(Ret, str): - Message = Ret.split(sep = ' ', maxsplit = 1) - Ret = None + if isinstance(Ret, str): + Message = Ret.split(sep = ' ', maxsplit = 1) + Ret = None - if Ret != None and Ret > 0: - break + if Ret != None and Ret > 0: + break - if Ret != None and Ret >= 0: - break + if Ret != None and Ret >= 0: + break - if RetMessage: - self.Torchlight().SayPrivate(player, RetMessage) + if RetMessage: + self.Torchlight().SayPrivate(player, RetMessage) - if self.NeedsReload: - self.NeedsReload = False - self.Reload() + if self.NeedsReload: + self.NeedsReload = False + self.Reload() - return Ret + return Ret diff --git a/torchlight_changes_unloze/torchlight3/Torchlight/Commands.py b/torchlight_changes_unloze/torchlight3/Torchlight/Commands.py index e8270cb8..bb4f0be5 100755 --- a/torchlight_changes_unloze/torchlight3/Torchlight/Commands.py +++ b/torchlight_changes_unloze/torchlight3/Torchlight/Commands.py @@ -8,6 +8,28 @@ import math from .Utils import Utils, DataHolder import traceback +def get_rubberBand(message): + rubberband = [] + try: + for msg in message[1].split(" "): #checking if pitch= or tempo= is specified + if "tempo=" in msg: + tempo = float(msg.split("tempo=",1)[1]) + if tempo < 0.0: + tempo = 0.01 + if tempo > 20: + tempo = 20 + rubberband.append(f"rubberband=tempo={tempo}") + elif "pitch=" in msg: + pitch = float(msg.split("pitch=",1)[1]) + if pitch < 0.0: + pitch = 0.1 + if pitch > 5.0: + pitch = 5.0 + rubberband.append(f"rubberband=pitch={pitch}") + except Exception: + pass + return rubberband + class BaseCommand(): Order = 0 def __init__(self, torchlight): @@ -525,25 +547,8 @@ class VoiceCommands(BaseCommand): elif Level < 2: return 0 - rubberband = [] - try: - for msg in message[1].split(" "): #checking if pitch= or tempo= is specified - if "tempo=" in msg: - tempo = float(msg.split("tempo=",1)[1]) - if tempo < 0.0: - tempo = 0.01 - if tempo > 20: - tempo = 20 - rubberband.append(f"rubberband=tempo={tempo}") - elif "pitch=" in msg: - pitch = float(msg.split("pitch=",1)[1]) - if pitch < 0.0: - pitch = 0.1 - if pitch > 99: - pitch = 99 - rubberband.append(f"rubberband=pitch={pitch}") - except Exception: - pass + rubberband = get_rubberBand(message) + if message[0] == "!random": Trigger = self.random.choice(list(self.VoiceTriggers.values())) if isinstance(Trigger, list): @@ -561,6 +566,7 @@ class VoiceCommands(BaseCommand): if isinstance(Sounds, list): if Num and Num > 0 and Num <= len(Sounds): Sound = Sounds[Num - 1] + elif message[1] and not message[1].startswith("tempo=") and not message[1].startswith("pitch="): #it does not start with pitch or with tempo, so must be a number or alias. searching = message[1].startswith('?') search = message[1][1:] if searching else message[1].split(" ")[0] @@ -617,7 +623,7 @@ class YouTube(BaseCommand): self.Triggers = ["!yt"] self.Level = 6 - async def _func(self, message, player): + async def _func(self, message, player, line = None): self.Logger.debug(sys._getframe().f_code.co_name + ' ' + str(message)) if self.check_disabled(player): @@ -637,8 +643,11 @@ class YouTube(BaseCommand): AudioClip = self.Torchlight().AudioManager.AudioClip(player, message[1]) if not AudioClip: return 1 - - return AudioClip.Play(Time) + + #turning the string into a list where get_rubberband just picks the second element to search in + dline = ['', line.split(" ", 1)[1]] + rubberband = get_rubberBand(dline) + return AudioClip.Play(Time, rubberband = rubberband) class YouTubeSearch(BaseCommand): import json @@ -648,7 +657,7 @@ class YouTubeSearch(BaseCommand): self.Triggers = ["!yts"] self.Level = 6 #adjusting to new levels - async def _func(self, message, player): + async def _func(self, message, player, line = None): self.Logger.debug(sys._getframe().f_code.co_name + ' ' + str(message)) if self.check_disabled(player): @@ -663,7 +672,8 @@ class YouTubeSearch(BaseCommand): Time = Utils.ParseTime(TimeStr) message[1] = message[1][:Temp.value] - Proc = await asyncio.create_subprocess_exec("yt-dlp", "--dump-json", "--username", "oauth2", "--password", "''", "-xg", "ytsearch:" + message[1], + search_term = message[1].split("pitch=")[0].split("tempo=")[0] + Proc = await asyncio.create_subprocess_exec("yt-dlp", "--dump-json", "--username", "oauth2", "--password", "''", "-xg", "ytsearch:" + search_term, stdout = asyncio.subprocess.PIPE) Out, _ = await Proc.communicate() @@ -679,7 +689,11 @@ class YouTubeSearch(BaseCommand): if not AudioClip: return 1 self.Torchlight().LastUrl = url - return AudioClip.Play(Time) + + #turning the string into a list where get_rubberband just picks the second element to search in + dline = ['', line.split(" ", 1)[1]] + rubberband = get_rubberBand(dline) + return AudioClip.Play(Time, rubberband = rubberband) class Say(BaseCommand): @@ -692,7 +706,8 @@ class Say(BaseCommand): self.Level = 2 async def Say(self, player, language, message): - GTTS = self.gtts.gTTS(text = message, lang = language) + actual_message = message.split("pitch=")[0].split("tempo=")[0] + GTTS = self.gtts.gTTS(text = actual_message, lang = language) TempFile = self.tempfile.NamedTemporaryFile(delete = False) GTTS.write_to_fp(TempFile) @@ -702,8 +717,11 @@ class Say(BaseCommand): if not AudioClip: os.unlink(TempFile.name) return 1 + #turning the string into a list where get_rubberband just picks the second element to search in + dline = ['', message.split(" ", 1)[1]] + rubberband = get_rubberBand(dline) - if AudioClip.Play(): + if AudioClip.Play(rubberband = rubberband): AudioClip.AudioPlayer.AddCallback("Stop", lambda: os.unlink(TempFile.name)) return 0 else: