Skip to content

Commit

Permalink
feat: reaction system logic
Browse files Browse the repository at this point in the history
  • Loading branch information
m1sk9 committed Oct 11, 2023
1 parent 39b2a3b commit a66ab48
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
47 changes: 45 additions & 2 deletions src/events.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,58 @@
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;

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!(
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit a66ab48

Please sign in to comment.