From a66ab4821c4cde5abaeb8dab57ff4bcad01813c2 Mon Sep 17 00:00:00 2001 From: m1sk9 Date: Thu, 12 Oct 2023 00:43:46 +0900 Subject: [PATCH] feat: reaction system logic --- src/config.rs | 1 - src/events.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++-- src/main.rs | 2 +- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index 55e36f7..75942f1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,7 +3,6 @@ use once_cell::sync::OnceCell; use serde::{Deserialize, Serialize}; use std::fs::File; use std::io::BufReader; -use tracing::log::info; #[derive(Default, Deserialize, Debug)] pub struct IdeaReactionConfig { diff --git a/src/events.rs b/src/events.rs index d2048e9..81a4a3e 100644 --- a/src/events.rs +++ b/src/events.rs @@ -1,8 +1,10 @@ -use crate::config::ENV_CONFIG; +use crate::config::{load_config, ENV_CONFIG}; use serenity::async_trait; use serenity::client::Context; +use serenity::model::channel::Message; use serenity::model::gateway::{Activity, Ready}; -use serenity::model::prelude::{ChannelId, GuildId, Webhook}; +use serenity::model::id::WebhookId; +use serenity::model::prelude::{ChannelId, GuildId, ReactionType}; use serenity::prelude::EventHandler; use tracing::log::info; @@ -10,6 +12,47 @@ pub struct EvHandler; #[async_trait] impl EventHandler for EvHandler { + async fn message(&self, ctx: Context, message: Message) { + if message.is_private() { + return; + } + + let env_config = ENV_CONFIG.get().unwrap(); + + let channel_id = message.channel_id; + if channel_id != ChannelId(env_config.target_channel_id) { + return; + } + + match message.webhook_id { + Some(id) => { + if id != WebhookId(env_config.target_webhook_id) { + return; + } + + let embed = message.embeds.first().unwrap(); + let embed_title = embed.title.as_ref().unwrap(); + if !embed_title.contains("[New issue]") { + return; + } + + let reactions = load_config().unwrap().reactions; + for reaction in reactions { + if let Err(why) = message + .react(&ctx.http, ReactionType::Unicode(reaction)) + .await + { + info!("Failed to react: {:?}", why); + } + } + + info!("Reacted to message: {}", message.id); + } + None => { + return; + } + } + } async fn ready(&self, ctx: Context, bot: Ready) { let env_config = ENV_CONFIG.get().unwrap(); info!( diff --git a/src/main.rs b/src/main.rs index dbe697c..cada59d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use crate::client::discord::create_discord_client; use crate::config::{load_config, IdeaReactionEnv, ENV_CONFIG}; use dotenvy::dotenv; -use std::env; + use tracing::info; mod client;