This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
74 lines (56 loc) · 2.47 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
import os
from discord.ext import commands
import discord
import signal
import asyncio
from loguru import logger
from DynamicCommandPrefix import dynamic_command_prefix
import Observ
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')