diff --git a/src/client/discord.rs b/src/client/discord.rs index f9842bb..b7f63c3 100644 --- a/src/client/discord.rs +++ b/src/client/discord.rs @@ -4,7 +4,8 @@ use serenity::prelude::GatewayIntents; use serenity::Client; pub async fn create_discord_client(token: &str) -> anyhow::Result { - let intents = GatewayIntents::GUILD_MESSAGE_REACTIONS | GatewayIntents::GUILD_MESSAGES; + let intents = + GatewayIntents::GUILD_MESSAGES | GatewayIntents::GUILDS | GatewayIntents::MESSAGE_CONTENT; let client = Client::builder(token, intents) .event_handler(EvHandler) .await diff --git a/src/config.rs b/src/config.rs index 6b2ee4d..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 { @@ -29,7 +28,6 @@ pub fn load_config() -> anyhow::Result { panic!("No reactions found in config file"); } - info!("Loaded config: {:?}", config.reactions); Ok(config) } 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 e69f95e..cada59d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ 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; mod config; @@ -18,7 +19,8 @@ async fn main() -> anyhow::Result<()> { let mut client = create_discord_client(&ENV_CONFIG.get().unwrap().discord_api_token).await?; - load_config()?; + let config = load_config()?; + info!("Loaded config: {:?}", config.reactions); if let Err(why) = client.start().await { println!("Failed run discord client: {:?}", why);