reminder-bot/src/event_handlers.rs

81 lines
2.8 KiB
Rust
Raw Permalink Normal View History

use poise::{
serenity_prelude as serenity,
2024-03-24 20:37:29 +00:00
serenity_prelude::{ActivityData, CreateEmbed, CreateMessage, FullEvent},
};
2024-03-24 20:37:29 +00:00
use crate::{
component_models::ComponentDataModel, metrics::COMMAND_COUNTER, Data, Error, THEME_COLOR,
};
pub async fn listener(
ctx: &serenity::Context,
2024-01-06 19:48:17 +00:00
event: &FullEvent,
data: &Data,
) -> Result<(), Error> {
2022-02-19 14:32:03 +00:00
match event {
2024-01-06 19:48:17 +00:00
FullEvent::Ready { .. } => {
ctx.set_activity(Some(ActivityData::watching("for /remind")));
2022-05-13 11:43:27 +00:00
}
2024-01-06 19:48:17 +00:00
FullEvent::ChannelDelete { channel, .. } => {
sqlx::query!("DELETE FROM channels WHERE channel = ?", channel.id.get())
2022-02-19 22:11:21 +00:00
.execute(&data.database)
.await
.unwrap();
2022-02-19 14:32:03 +00:00
}
2024-01-06 19:48:17 +00:00
FullEvent::GuildCreate { guild, is_new } => {
if is_new.unwrap_or(false) {
let guild_id = guild.id.get().to_owned();
2022-04-19 14:23:27 +00:00
2022-09-11 16:38:53 +00:00
sqlx::query!("INSERT IGNORE INTO guilds (guild) VALUES (?)", guild_id)
2022-04-19 14:23:27 +00:00
.execute(&data.database)
2022-09-11 16:38:53 +00:00
.await?;
2022-04-19 14:23:27 +00:00
2022-09-11 16:38:53 +00:00
let default_channel = guild.default_channel_guaranteed();
2022-04-19 14:23:27 +00:00
2022-09-11 16:38:53 +00:00
if let Some(default_channel) = default_channel {
2024-01-06 19:48:17 +00:00
default_channel.send_message(
&ctx,
CreateMessage::new()
.embed(
CreateEmbed::new()
.title("Thank you for adding Reminder Bot!")
.description("To get started:
2022-09-11 16:38:53 +00:00
Set your timezone with `/timezone`
Set up permissions in Server Settings 🠚 Integrations 🠚 Reminder Bot (desktop only)
Create your first reminder with `/remind`
2022-04-19 14:23:27 +00:00
2022-09-11 16:38:53 +00:00
__Support__
If you need any support, please come and ask us! Join our [Discord](https://discord.jellywx.com).
2022-04-19 14:23:27 +00:00
2022-09-11 16:38:53 +00:00
__Updates__
To stay up to date on the latest features and fixes, join our [Discord](https://discord.jellywx.com).
2024-01-06 19:48:17 +00:00
")
.color(*THEME_COLOR)
)
)
2022-09-11 16:38:53 +00:00
.await?;
2022-04-19 14:23:27 +00:00
}
}
}
2024-01-06 19:48:17 +00:00
FullEvent::GuildDelete { incomplete, .. } => {
let _ = sqlx::query!("DELETE FROM guilds WHERE guild = ?", incomplete.id.get())
2022-02-19 14:32:03 +00:00
.execute(&data.database)
.await;
}
2024-01-06 19:48:17 +00:00
FullEvent::InteractionCreate { interaction } => {
if let Some(component) = interaction.clone().message_component() {
let component_model = ComponentDataModel::from_custom_id(&component.data.custom_id);
2024-01-06 19:48:17 +00:00
component_model.act(ctx, data, &component).await;
}
2024-03-24 20:37:29 +00:00
if let Some(command) = interaction.clone().command() {
COMMAND_COUNTER.with_label_values(&[command.data.name.as_str()]).inc();
}
}
2022-02-19 14:32:03 +00:00
_ => {}
}
2022-02-19 14:32:03 +00:00
Ok(())
}