runabot/RUNA/Cogs/Event.py
2024-05-09 13:05:22 +09:00

66 lines
3.0 KiB
Python

import random
import pendulum
import onnxruntime as rt
from discord import ApplicationContext, DiscordException, Game, Guild, Color
from discord.ext.commands import Cog
from discord.ext import tasks
import sentry_sdk
from discord.ext.commands import CommandOnCooldown, MissingPermissions
from RUNA.UI.Embed import Default_Embed, Music_Embed, RunaEmbed
from RUNA.Database import database
model = rt.InferenceSession("RUNA/Tagging/model.onnx", provider_options="CPUExecutionProvider")
class Event(Cog):
def __init__(self, bot):
self.bot = bot
@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)
elif isinstance(exception, MissingPermissions):
need_permissions = ""
for perm in exception.missing_permissions:
need_permissions += f"{perm}, "
await ctx.respond(embed=RunaEmbed(title="❌ 에러!", description=f"권한이 부족해요! 필요한 권한: {need_permissions}", color=Color.red()),ephemeral=True)
else:
await ctx.respond(embed=RunaEmbed(title="❌ 에러!", description=f"알수 없는 에러가 발생했어요! 봇 소유자에게 연락해주세요.", color=Color.red()),ephemeral=True)
sentry_sdk.capture_exception(exception)
@Cog.listener()
async def on_ready(self) -> None:
print("Ready!")
await self.bot.change_presence(activity=Game(name="루나봇"))
@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))