78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
import asyncio
|
|
import os
|
|
import signal
|
|
|
|
import discord
|
|
from discord.ext import commands
|
|
from loguru import logger
|
|
|
|
import Observ
|
|
from DynamicCommandPrefix import dynamic_command_prefix
|
|
|
|
LOG_FILE_ENABLED = os.getenv('LOG_FILE_ENABLED', 'true').lower() == 'true'
|
|
|
|
if LOG_FILE_ENABLED:
|
|
logger.add('offlineTTSBot.log', backtrace=True, diagnose=False, rotation='5MB')
|
|
|
|
"""
|
|
while msg := input('$ '):
|
|
start = time.time()
|
|
audio = tts.synthesize_text(msg, speaker=Speakers.kseniya)
|
|
print('synthesize took ', str(time.time() - start))
|
|
utils.play_bytes_io(audio)
|
|
"""
|
|
|
|
|
|
class DiscordTTSBot(commands.Bot, Observ.Subject):
|
|
def __init__(self, **kwargs):
|
|
super().__init__(**kwargs)
|
|
signal.signal(signal.SIGTERM, self.shutdown)
|
|
signal.signal(signal.SIGINT, self.shutdown)
|
|
logger.info('Shutdown callbacks registered')
|
|
|
|
def shutdown(self, sig, frame):
|
|
logger.info(f'Shutting down by signal: {sig}')
|
|
asyncio.create_task(self.close())
|
|
|
|
async def on_ready(self):
|
|
logger.debug(f'Bot is ready: {self.user.name}')
|
|
|
|
async def on_message(self, message: discord.Message) -> None:
|
|
if message.guild is None:
|
|
return
|
|
|
|
await super(DiscordTTSBot, self).on_message(message)
|
|
if message.author.bot: # because on_command_error will not be called if author is bot
|
|
# so it isn't a command, so, pass it next
|
|
await self.notify(message)
|
|
|
|
async def on_command_error(self, ctx: commands.Context, exception: commands.errors.CommandError) -> None:
|
|
if isinstance(exception, commands.errors.CommandNotFound):
|
|
ctx.message.content = ctx.message.content[len(ctx.prefix):] # skip prefix
|
|
await self.notify(ctx.message)
|
|
|
|
else:
|
|
logger.opt(exception=exception).warning(f'Global error caught:')
|
|
|
|
|
|
intents = discord.Intents.default()
|
|
intents.message_content = True
|
|
|
|
discord_client = DiscordTTSBot(command_prefix=dynamic_command_prefix, intents=intents)
|
|
|
|
|
|
async def main():
|
|
for filename in os.listdir("./cogs"):
|
|
if filename.endswith(".py"):
|
|
logger.debug(f'Loading extension {filename}')
|
|
await discord_client.load_extension(f"cogs.{filename[:-3]}")
|
|
|
|
await discord_client.start(os.environ['DISCORD_TOKEN'])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
loop = asyncio.new_event_loop()
|
|
loop.run_until_complete(main())
|
|
logger.debug('Shutdown completed')
|