240 lines
9.8 KiB
Python
240 lines
9.8 KiB
Python
import random
|
|
import re
|
|
import utilities_script as utilities
|
|
|
|
from discord import message
|
|
from discord.client import Client
|
|
import asyncio
|
|
|
|
import config as config
|
|
import db
|
|
import tts
|
|
|
|
import commands.loader as command_loader
|
|
from commands.command_base import AbstractCommand
|
|
|
|
import credentials
|
|
|
|
import discord
|
|
import discord.message
|
|
import discord.channel
|
|
import discord.abc
|
|
|
|
from cooldowns import Cooldown_Module
|
|
|
|
class Discord_Module(discord.Client):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.loop = asyncio.get_event_loop()
|
|
self.dbCredential: credentials.DB_Credential
|
|
self.discordCredential: credentials.Discord_Credential
|
|
|
|
self.cooldownModule:Cooldown_Module = Cooldown_Module()
|
|
self.cooldownModule.setupCooldown("discordRateLimit", 10, 1)
|
|
|
|
self.commands = command_loader.load_commands_new(AbstractCommand.CommandType.DISCORD)
|
|
|
|
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)
|
|
|
|
def main(self):
|
|
print("starting loop")
|
|
self.loop.create_task(self.startup())
|
|
self.loop.run_forever()
|
|
|
|
async def on_ready(self):
|
|
print('Logged on as', self.user)
|
|
|
|
async def on_message(self, message: discord.Message):
|
|
print("{" + message.guild.name + "}[ " + str(message.channel) + " ](" + message.author.display_name + ")> ")
|
|
#print(message.author.mention)
|
|
print(message.content)
|
|
#print(message.channel.id)
|
|
#Message ID
|
|
#print(str(message.id))
|
|
#print(str(message.channel.id))
|
|
if message.content == "//test":
|
|
#pass
|
|
await message.channel.send('test response')
|
|
#test = self.get_channel(431129571308339210)
|
|
#await test.send("testerino")
|
|
temp = message.channel.last_message
|
|
delayTime:float = 3
|
|
await message.delete(delay=delayTime)
|
|
await temp.delete(delay=delayTime)
|
|
|
|
|
|
if not await self.isSenderBot(message):
|
|
# This will check for the praxis_bot-tts channel and will TTS stuff from there.
|
|
await self.eval_triggeredEvents(message)
|
|
|
|
if self.cooldownModule.isCooldownActive("discordRateLimit") == False:
|
|
await self.eval_commands(message)
|
|
#await self.tts_message(message)
|
|
|
|
|
|
async def eval_triggeredEvents(self, message: discord.Message):
|
|
|
|
# This will check for the selected channels and will TTS stuff from there.
|
|
await self.tts_message(message)
|
|
foundChannel = False
|
|
|
|
for channel in self.selected_ttsChannels:
|
|
if channel == message.channel.id:
|
|
# await self.tts_message(message)
|
|
pass
|
|
|
|
|
|
|
|
async def eval_automaticEvents(self, message: discord.Message):
|
|
pass
|
|
|
|
|
|
async def eval_commands(self, message: discord.Message):
|
|
# containsURL: bool = self.contains_url(message)
|
|
try:
|
|
#first_space_idx = message.text.index(' ')
|
|
|
|
# This fixes a error where if you send a command without arguments it fails because
|
|
# it cant find the substring.
|
|
if message.content.find(" ") != -1:
|
|
first_space_idx = message.content.index(' ')
|
|
else:
|
|
first_space_idx = -1
|
|
|
|
command_text = ' '
|
|
if first_space_idx > -1:
|
|
command_text = message.content[0:first_space_idx]
|
|
else:
|
|
command_text = message.content
|
|
|
|
command = self.commands[command_text]
|
|
if command is not None and command.command_type is AbstractCommand.CommandType.DISCORD:
|
|
await command.do_command(self, message)
|
|
except Exception as e:
|
|
# Undo the following for debug stuff
|
|
#print(e)
|
|
pass # we don't care
|
|
|
|
async def send_message(self, message: discord.Message, response):
|
|
isBlocked = await self.isChannel_inConfigList(str(message.channel.id), config.block_DiscordChannelsMessaging)
|
|
if self.cooldownModule.isCooldownActive("discordRateLimit") == False and not isBlocked and not config.blockAll_DiscordChannelsMessaging:
|
|
await message.channel.send(response)
|
|
self.cooldownModule.actionTrigger("discordRateLimit")
|
|
|
|
|
|
async def tts_message(self, message: discord.Message):
|
|
isBlocked = await self.isChannel_inConfigList(str(message.channel.id), config.block_DiscordChannelsTTS)
|
|
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)) and (await self.isTTS_URL_Enabled(message)):
|
|
print(message.channel.id, message.channel.id, message.channel.id)
|
|
isMessageChannelInList = False
|
|
for TTS_C_id in config.selected_DiscordTTSChannels:
|
|
print(TTS_C_id)
|
|
if int(TTS_C_id) == int(message.channel.id):
|
|
isMessageChannelInList = True
|
|
if self.tts_enabled and (isMessageChannelInList) 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)
|
|
channel_text = "%s user msg" % message.channel
|
|
|
|
tts.tts(text_to_say)
|
|
|
|
|
|
if not await self.contains_slur(message):
|
|
if self.tts_enabled:
|
|
if not message.content.startswith('!'):
|
|
pass
|
|
#text_to_say: str = "%s says, %s" % (message.author.display_name, message.content)
|
|
#channel_text = "%s user msg" % message.channel
|
|
|
|
#tts.tts(text_to_say)
|
|
|
|
# Checks for basic slurs.
|
|
async def contains_slur(self, message: discord.Message):
|
|
containsSlur: bool = False
|
|
|
|
if await self.slur_check(message.content) or await self.slur_check(message.author.display_name):
|
|
containsSlur = True
|
|
|
|
return containsSlur
|
|
|
|
async def slur_check(self, text):
|
|
containsSlur: bool = False
|
|
parsedMessage = text.split(" ")
|
|
for word in parsedMessage:
|
|
for slur in config.slurList:
|
|
if word.lower() == slur:
|
|
containsSlur = True
|
|
break # we want to immediately escape if we found a slur
|
|
if containsSlur:
|
|
break
|
|
|
|
if containsSlur:
|
|
print("<{ slur detected! }>")
|
|
#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
|
|
for bot in config.botList:
|
|
if message.author.display_name.lower() == bot.lower():
|
|
isBot = True
|
|
print("<{ bot detected! }> ")
|
|
return isBot
|
|
|
|
async def isChannel_inConfigList(self, selectedChannel, selectedList):
|
|
#print(channel)
|
|
#print(selectedList)
|
|
is_Self = False
|
|
for discordChannel in selectedList:
|
|
#print("isSelf: " + str(discordChannel) + " vs " + str(selectedChannel))
|
|
if discordChannel == selectedChannel:
|
|
is_Self = True
|
|
#if is_Self:
|
|
# print("Is Self")
|
|
#if not is_Self:
|
|
# print("Is Not Self")
|
|
return is_Self
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
testModule = Discord_Module()
|
|
|
|
credentials_manager = credentials.Credentials_Module()
|
|
credentials_manager.load_credentials()
|
|
testModule.dbCredential = credentials_manager.find_DB_Credential(config.credentialsNickname)
|
|
testModule.discordCredential = credentials_manager.find_Discord_Credential(config.credentialsNickname)
|
|
|
|
for ttsChannel in config.selected_discordTTSChannels:
|
|
testModule.selected_ttsChannels.append(int(ttsChannel))
|
|
|
|
testModule.main() |