Praxis_Bot/discord_script.py
Alex Orid ce702b8b01 Working TTS on Discord
I also cleaned up tts usage in a few other places.
2020-10-20 06:12:26 -04:00

149 lines
5.0 KiB
Python

import random
import re
from discord import message
from discord.client import Client
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
from cooldowns import Cooldown_Module
class Discord_Module(discord.Client):
def __init__(self):
super().__init__()
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()
self.tts_enabled: bool = False
def main(self):
self.run(self.discordCredential.token)
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.content)
#Message ID
#print(str(message.id))
#Channel ID
#print(str(message.channel.id))
if message.content == "//test":
await message.channel.send('test response')
if not await self.isSenderBot(message):
if self.cooldownModule.isCooldownActive("discordRateLimit") == False:
await self.eval_commands(message)
await self.tts_message(message)
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, response):
if self.cooldownModule.isCooldownActive("discordRateLimit") == False:
await message.channel.send(response)
self.cooldownModule.actionTrigger("discordRateLimit")
async def tts_message(self, message: discord.Message):
if not await self.contains_slur(message):
if self.tts_enabled:
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
if message.author.display_name.lower() == message.channel:
tts.tts(text_to_say)
else:
# tts.tts(message.sender + " says, " + message.text)
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
# 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
if __name__ == "__main__":
testModule = Discord_Module()
credentials_manager = credentials.Credentials_Module()
credentials_manager.load_credentials()
testModule.dbCredential = credentials_manager.find_DB_Credential("praxis_bot")
testModule.discordCredential = credentials_manager.find_Discord_Credential("praxis_bot")
testModule.main()
testModule.main()