Skip to content

Commit

Permalink
Merge pull request #3 from GiganticMinecraft/feat/reaction
Browse files Browse the repository at this point in the history
feat: リアクションロジックの追加
  • Loading branch information
m1sk9 authored Oct 11, 2023
2 parents b41c43d + a66ab48 commit 044ac3b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/client/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use serenity::prelude::GatewayIntents;
use serenity::Client;

pub async fn create_discord_client(token: &str) -> anyhow::Result<Client> {
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
Expand Down
2 changes: 0 additions & 2 deletions 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 All @@ -29,7 +28,6 @@ pub fn load_config() -> anyhow::Result<IdeaReactionConfig> {
panic!("No reactions found in config file");
}

info!("Loaded config: {:?}", config.reactions);
Ok(config)
}

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
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 044ac3b

Please sign in to comment.