runabot/Christmas/Cogs/Event.py

71 lines
2.8 KiB
Python
Raw Normal View History

2023-11-26 04:27:34 +00:00
import random
2023-12-02 06:03:50 +00:00
import wavelink
2023-12-03 10:06:21 +00:00
import pendulum
2023-12-02 06:03:50 +00:00
import onnxruntime as rt
2023-11-26 04:27:34 +00:00
from discord import ApplicationContext, DiscordException, Game, Guild
2023-11-25 17:28:48 +00:00
from discord.ext.commands import Cog
2023-12-03 10:06:21 +00:00
from discord.ext import tasks
2023-11-25 17:28:48 +00:00
2023-12-02 06:03:50 +00:00
from discord.ext.commands import CommandOnCooldown
from Christmas.UI.Embed import Default_Embed, Music_Embed
2023-12-02 06:03:50 +00:00
from Christmas.config import ChristmasConfig
2023-12-02 10:42:36 +00:00
from Christmas.Database import database
2023-11-26 04:27:34 +00:00
2023-12-02 06:03:50 +00:00
model = rt.InferenceSession("Christmas/Tagging/model.onnx", provider_options="CPUExecutionProvider")
2023-11-25 17:28:48 +00:00
class Event(Cog):
def __init__(self, bot):
self.bot = bot
2023-12-02 06:03:50 +00:00
self.config = ChristmasConfig()
2023-11-25 17:28:48 +00:00
@Cog.listener()
2023-11-26 04:27:34 +00:00
async def on_application_command_error(self, ctx: ApplicationContext, exception: DiscordException) -> None:
2023-12-02 06:03:50 +00:00
if isinstance(exception, CommandOnCooldown):
2023-12-05 14:35:48 +00:00
await ctx.respond(Default_Embed.cooldown(exception.retry_after), ephemeral=True)
2023-12-02 10:42:36 +00:00
else:
print(exception)
2023-11-25 17:28:48 +00:00
@Cog.listener()
2023-11-26 04:27:34 +00:00
async def on_ready(self) -> None:
2023-11-25 17:28:48 +00:00
print("Ready!")
await self.bot.change_presence(activity=Game(name="크리스마스에 함께!"))
2023-12-02 10:42:36 +00:00
2023-12-02 06:03:50 +00:00
# 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)
2023-11-25 17:28:48 +00:00
2023-11-26 04:27:34 +00:00
@Cog.listener()
async def on_guild_join(self, guild: Guild) -> None:
if guild.system_channel is not None:
2023-12-05 14:35:48 +00:00
await guild.system_channel.send(embed=Default_Embed.default_join_embed())
2023-11-26 04:27:34 +00:00
else:
2023-12-05 14:35:48 +00:00
await random.choice(guild.text_channels).send(embed=Default_Embed.default_join_embed())
2023-12-02 06:03:50 +00:00
2023-12-02 10:42:36 +00:00
@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())
2023-12-03 10:06:21 +00:00
2023-11-25 17:28:48 +00:00
def setup(bot):
bot.add_cog(Event(bot))