From aeb50dcc6e25c2566d7f891431156785dfc8c82e Mon Sep 17 00:00:00 2001 From: Alex Orid Date: Thu, 21 Jan 2021 08:18:09 -0500 Subject: [PATCH] TTS URL Check --- config.py | 6 +++++- discord_script.py | 24 ++++++++++++++++++++++-- main.py | 2 +- twitch_script.py | 10 +++++++++- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/config.py b/config.py index 1355278..be7a816 100644 --- a/config.py +++ b/config.py @@ -7,7 +7,7 @@ discord_module: bool = False test_module: bool = False -autojoin_TwitchChannels = ["thecuriousnerd"] +autoJoin_TwitchChannels = ["thecuriousnerd"] whitelisted_TwitchPowerUsers = ["thecuriousnerd", "theredpoint", "lakotor"] block_TwitchChannelsMessaging = [""] @@ -19,6 +19,8 @@ blockAll_TwitchChatChannelsTTS = False # blockAll supersedes the force bool and force_TwitchChannelsTTS = [""] # force supersedes the block list forceAll_TwitchChatChannelsTTS = False # forceAll supersedes the blockAll bool and block list and force list +blockAll_TTS_URL_Twitch = True + block_DiscordChannelsMessaging = [""] blockAll_DiscordChannelsMessaging = False blockAll_DiscordPrivateMessaging = False # Private Messaging not yet implemented @@ -30,6 +32,8 @@ blockAll_DiscordChannelsTTS = False # blockAll supersedes the force bool and for force_DiscordChannelsTTS = [""] # force supersedes the block list forceAll_DiscordChatChannelsTTS = False # forceAll supersedes the blockAll bool and block list and force list +blockAll_TTS_URL_Discord = True + class Speaker(Enum): GOOGLE_TEXT_TO_SPEECH = 1 STREAMLABS_API = 2 diff --git a/discord_script.py b/discord_script.py index dafb5fd..28a212d 100644 --- a/discord_script.py +++ b/discord_script.py @@ -36,6 +36,12 @@ class Discord_Module(discord.Client): self.tts_enabled: bool = False self.selected_ttsChannels:list = [] + self.block_tts_url: bool = False + + # don't freak out, this is *merely* a regex for matching urls that will hit just about everything + self._urlMatcher = re.compile( + "(https?:(/{1,3}|[a-z0-9%])|[a-z0-9.-]+[.](com|net|org|edu|gov|mil|aero|asia|biz|cat|coop|info|int|jobs|mobi|museum|name|post|pro|tel|travel|xxx|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|Ja|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw))") + async def startup(self): await self.start(self.discordCredential.token) @@ -131,7 +137,7 @@ class Discord_Module(discord.Client): isForced = (await self.isChannel_inConfigList(str(message.channel.id), config.force_DiscordChannelsTTS) and not config.blockAll_DiscordChannelsTTS) #print("isBlocked: " + str(isBlocked)) #print("isForced: " + str(isForced)) - if (not await self.contains_slur(message)): + if (not await self.contains_slur(message)) and (await self.isTTS_URL_Enabled(message)): if self.tts_enabled and not isBlocked and not config.blockAll_DiscordChannelsTTS or isForced or config.forceAll_DiscordChatChannelsTTS: if not message.content.startswith('!'): text_to_say: str = "%s says, %s" % (message.author.display_name, message.content) @@ -174,6 +180,20 @@ class Discord_Module(discord.Client): #print("<{ slur detected! }> " + " [#" + message.channel + "](" + message.author.display_name + ") used a slur in chat") return containsSlur + async def contains_url(self, message: discord.Message): + containsURL = re.search(self._urlMatcher, message.content.lower()) is not None + if containsURL: + print("<{ link detected! }> " + " [#" + str(message.channel) + "](" + message.author.display_name + ") sent a link in chat") + return containsURL + + async def isTTS_URL_Enabled(self, message: discord.Message): + is_ttsEnabled = False + if not config.blockAll_TTS_URL_Discord or not self.block_tts_url: + if not await self.contains_url(message): + is_ttsEnabled = True + return is_ttsEnabled + + # Checks if Sender is bot. async def isSenderBot(self, message: discord.Message): isBot = False @@ -188,7 +208,7 @@ class Discord_Module(discord.Client): #print(selectedList) is_Self = False for discordChannel in selectedList: - print("isSelf: " + str(discordChannel) + " vs " + str(selectedChannel)) + #print("isSelf: " + str(discordChannel) + " vs " + str(selectedChannel)) if discordChannel == selectedChannel: is_Self = True #if is_Self: diff --git a/main.py b/main.py index 5479772..68efa76 100644 --- a/main.py +++ b/main.py @@ -34,7 +34,7 @@ def twitch_module_init(dbCert, twitchCert): twitchModule_.tts_enabled = config.autoEnabled_TwitchChannelsTTS twitchModule_.whitelisted_users = config.whitelisted_TwitchPowerUsers - for twitchChannel in config.autojoin_TwitchChannels: + for twitchChannel in config.autoJoin_TwitchChannels: print("joining channel function") twitchModule_.join_channel(None, twitchChannel) diff --git a/twitch_script.py b/twitch_script.py index a17094b..cf66dc5 100644 --- a/twitch_script.py +++ b/twitch_script.py @@ -26,6 +26,7 @@ class Twitch_Module(): self.chat: twitch.Chat self.commands = command_loader.load_commands_new(AbstractCommand.CommandType.TWITCH) self.tts_enabled: bool = False + self.block_tts_url: bool = False self.tts_whitelist_enabled: bool = False self.links_allowed: bool = True self.whitelisted_users: list = ["thecuriousnerd"] @@ -114,7 +115,7 @@ class Twitch_Module(): def tts_message(self, message: twitch.chat.Message): isBlocked = self.isChannel_inConfigList(self.chat.channel, config.block_TwitchChannelsTTS) isForced = (self.isChannel_inConfigList(self.chat.channel, config.force_TwitchChannelsTTS) and not config.blockAll_TwitchChatChannelsTTS) - if (not self.contains_slur(message)): + if (not self.contains_slur(message)) and (self.isTTS_URL_Enabled(message)): if self.tts_enabled and not isBlocked and not config.blockAll_TwitchChatChannelsTTS or isForced or config.forceAll_TwitchChatChannelsTTS: if not message.text.startswith('!'): text_to_say: str = "%s says, %s" % (message.sender, message.text) @@ -153,6 +154,13 @@ class Twitch_Module(): print("<{ slur detected! }> " + " [#" + message.channel + "](" + message.sender + ") used a slur in chat") return containsSlur + def isTTS_URL_Enabled(self, message: twitch.chat.Message): + is_ttsEnabled = False + if not config.blockAll_TTS_URL_Twitch or not self.block_tts_url: + if not self.contains_url(message): + is_ttsEnabled = True + return is_ttsEnabled + def isChannel_inConfigList(self, selectedChannel, selectedList): #print(channel) #print(selectedList)