TTS URL Check

This commit is contained in:
Alex Orid 2021-01-21 08:18:09 -05:00
parent a96c263665
commit aeb50dcc6e
4 changed files with 37 additions and 5 deletions

View File

@ -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

View File

@ -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:

View File

@ -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)

View File

@ -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)