runabot/Christmas/Cogs/Event.py
2023-12-05 23:35:48 +09:00

71 lines
2.8 KiB
Python

import random
import wavelink
import pendulum
import onnxruntime as rt
from discord import ApplicationContext, DiscordException, Game, Guild
from discord.ext.commands import Cog
from discord.ext import tasks
from discord.ext.commands import CommandOnCooldown
from Christmas.UI.Embed import Default_Embed, Music_Embed
from Christmas.config import ChristmasConfig
from Christmas.Database import database
model = rt.InferenceSession("Christmas/Tagging/model.onnx", provider_options="CPUExecutionProvider")
class Event(Cog):
def __init__(self, bot):
self.bot = bot
self.config = ChristmasConfig()
@Cog.listener()
async def on_application_command_error(self, ctx: ApplicationContext, exception: DiscordException) -> None:
if isinstance(exception, CommandOnCooldown):
await ctx.respond(Default_Embed.cooldown(exception.retry_after), ephemeral=True)
else:
print(exception)
@Cog.listener()
async def on_ready(self) -> None:
print("Ready!")
await self.bot.change_presence(activity=Game(name="크리스마스에 함께!"))
# connect wavelink
@Cog.listener()
async def on_connect(self) -> None:
await self.bot.wait_until_ready()
nodes = []
for node in self.config.LAVALINK:
nodes.append(wavelink.Node(uri=node["HOST"], password=node["PASSWORD"], identifier=node["IDENTIFIER"]))
await wavelink.Pool.connect(nodes=nodes, client=self.bot)
@Cog.listener()
async def on_guild_join(self, guild: Guild) -> None:
if guild.system_channel is not None:
await guild.system_channel.send(embed=Default_Embed.default_join_embed())
else:
await random.choice(guild.text_channels).send(embed=Default_Embed.default_join_embed())
@Cog.listener()
async def on_guild_update(self, before: Guild, after: Guild) -> None:
if before.name != after.name:
await database.update_guild_name(after.id, after.name)
@tasks.loop(seconds=600)
async def notify_all_guild_christmas(self) -> None:
if pendulum.now() > pendulum.datetime(2023, 12, 24, 12, 0, 0):
self.notify_all_guild_christmas.cancel()
return
for guild in self.bot.guilds:
if not await database.get_guild(guild.id): continue
if not await database.get_guild_christmas(guild.id):
await database.update_guild_christmas(guild.id, True)
if guild.system_channel is not None:
await guild.system_channel.send(embed=Music_Embed.changed_christmas())
else:
await random.choice(guild.text_channels).send(embed=Music_Embed.changed_christmas())
def setup(bot):
bot.add_cog(Event(bot))