From e739c443acf85dcb3a3a3063be95c565681f8cf2 Mon Sep 17 00:00:00 2001 From: paring Date: Tue, 6 Jan 2026 11:58:55 +0900 Subject: [PATCH] feat: send category --- src/bot/commands.rs | 1 + src/bot/config.rs | 1 + src/bot/db.rs | 4 ++-- src/bot/handler.rs | 24 ++++++++++++++++++++---- src/bot/main.rs | 16 ++++++++++++---- src/bot/modal.rs | 6 +++--- src/script.rs | 14 ++++++++++++++ 7 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/bot/commands.rs b/src/bot/commands.rs index f0024e4..a82ff03 100644 --- a/src/bot/commands.rs +++ b/src/bot/commands.rs @@ -46,6 +46,7 @@ impl Handler { 사용 가능 변수: - `reactions`: map (이모지 - 개수) - `channel`: string (채널 ID) +- `category`: string (카테고리 ID, 없을 시 UNIT) `result("채널ID", 리액션 개수, "메시지에 사용될 아이콘(이모지 권장)")` 를 리턴해주세요(unit이나 다른 값 반환 시 무시됩니다) diff --git a/src/bot/config.rs b/src/bot/config.rs index 8df1bd4..26ab205 100644 --- a/src/bot/config.rs +++ b/src/bot/config.rs @@ -3,6 +3,7 @@ use serde::Deserialize; #[derive(Deserialize, Debug)] pub struct Config { + pub debug: Option, pub bot: BotConfig, pub db: DatabaseConfig, } diff --git a/src/bot/db.rs b/src/bot/db.rs index 044e0e8..45b20c4 100644 --- a/src/bot/db.rs +++ b/src/bot/db.rs @@ -2,14 +2,14 @@ use sqlx::prelude::FromRow; #[derive(Debug, FromRow)] pub struct GuildRow { - pub id: String, + // pub id: String, pub script: Option, } #[derive(Debug, FromRow)] pub struct MessageRow { // pub guild_id: String, - pub message_id: String, + // pub message_id: String, pub counter_id: String, pub counter_channel_id: String, } diff --git a/src/bot/handler.rs b/src/bot/handler.rs index 966249f..843d9ee 100644 --- a/src/bot/handler.rs +++ b/src/bot/handler.rs @@ -7,8 +7,8 @@ use serenity::{ Attachment, CacheHttp, ChannelId, Component, Context, CreateComponent, CreateMediaGallery, CreateMediaGalleryItem, CreateSeparator, CreateTextDisplay, CreateUnfurledMediaItem, CreateWebhook, EditWebhookMessage, EventHandler, ExecuteWebhook, FullEvent, - GenericChannelId, GuildId, Interaction, InteractionType, Message, MessageFlags, MessageId, - MessageSnapshot, Reaction, StickerFormatType, StickerItem, Webhook, WebhookId, + GenericChannelId, GuildId, Interaction, Message, MessageFlags, MessageId, MessageSnapshot, + Reaction, StickerFormatType, StickerItem, Webhook, WebhookId, }, async_trait, futures::lock::Mutex, @@ -151,12 +151,28 @@ impl Handler { CloneBuilderMessage::from(msg.clone()) }; - let Some(result) = - paringboard::script::check(script, reactions, reaction.channel_id.to_string())? + let channel = reaction.channel(&ctx.http).await?; + + debug!("channel: {channel:?}"); + + let Some(result) = paringboard::script::check( + script, + reactions, + reaction.channel_id.to_string(), + channel + .guild() + .and_then(|x| x.parent_id) + .map(|x| x.to_string()), + ) + .inspect_err(|res| { + error!("check failed: {res:?}"); + })? else { return Ok(()); }; + debug!("check result: {result:?}"); + let channel_id = result .channel_id .parse() diff --git a/src/bot/main.rs b/src/bot/main.rs index 8c1bc92..5a1114d 100644 --- a/src/bot/main.rs +++ b/src/bot/main.rs @@ -1,5 +1,6 @@ use std::{str::FromStr, sync::Arc}; +use crate::{config::Config, handler::Handler}; use anyhow::Context; use dashmap::DashMap; use figment::{ @@ -12,8 +13,7 @@ use serenity::{ all::{GatewayIntents, Token}, }; use sqlx::{migrate, postgres::PgPoolOptions}; - -use crate::{config::Config, handler::Handler}; +use tracing::Level; mod commands; mod config; @@ -26,7 +26,7 @@ extern crate tracing; #[tokio::main] async fn main() -> anyhow::Result<()> { - tracing_subscriber::fmt::init(); + let mut fmt = tracing_subscriber::fmt(); let figment = Figment::new() .merge(Toml::file("config.toml")) @@ -34,6 +34,12 @@ async fn main() -> anyhow::Result<()> { let config: Config = figment.extract()?; + if config.debug.unwrap_or(false) { + fmt = fmt.with_max_level(Level::DEBUG); + } + + fmt.init(); + info!("config: {config:?}"); let db = PgPoolOptions::new() @@ -49,7 +55,9 @@ async fn main() -> anyhow::Result<()> { let mut client = Client::builder( Token::from_str(config.bot.token.expose_secret()).unwrap(), - GatewayIntents::GUILD_MESSAGES | GatewayIntents::GUILD_MESSAGE_REACTIONS, + GatewayIntents::GUILD_MESSAGES + | GatewayIntents::GUILD_MESSAGE_REACTIONS + | GatewayIntents::GUILDS, ) .event_handler(Arc::new(Handler { db, diff --git a/src/bot/modal.rs b/src/bot/modal.rs index 4c0348d..efec04e 100644 --- a/src/bot/modal.rs +++ b/src/bot/modal.rs @@ -1,10 +1,10 @@ use anyhow::Context as _; use serenity::all::{ - Component, Context, CreateInteractionResponseMessage, Label, LabelComponent, ModalInteraction, + Component, Context, CreateInteractionResponseMessage, LabelComponent, ModalInteraction, Permissions, }; -use crate::{db, handler::Handler}; +use crate::handler::Handler; impl Handler { pub async fn process_modal( @@ -54,8 +54,8 @@ impl Handler { .context("unable to get value text from input")?; sqlx::query("insert into guilds (id, script) values ($1, $2) on conflict (id) do update set script = excluded.script") - .bind(value.as_str()) .bind(guild_id.to_string()) + .bind(value.as_str()) .execute(&self.db) .await?; diff --git a/src/script.rs b/src/script.rs index 3402cfc..97b8bd2 100644 --- a/src/script.rs +++ b/src/script.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use anyhow::anyhow; use rhai::{Dynamic, Engine, Map, Scope}; use serde::Deserialize; +use tracing::debug; #[derive(Debug, Deserialize, Clone)] pub struct ReactionResult { @@ -15,7 +16,9 @@ pub fn check( script: &str, reactions: HashMap, channel: String, + category: Option, ) -> anyhow::Result> { + debug!("script: {script}"); let mut engine = Engine::new(); engine.set_max_operations(1000); engine.register_type::(); @@ -45,10 +48,21 @@ pub fn check( scope.set_value("reactions", Dynamic::from(emotes_input)); scope.set_value("channel", Dynamic::from(channel)); + scope.set_value( + "category", + if let Some(value) = category { + Dynamic::from(value) + } else { + Dynamic::UNIT + }, + ); + let result: Dynamic = engine .eval_with_scope(&mut scope, script) .map_err(|e| anyhow!("{e:?}"))?; + debug!("result: {result:?}"); + let result: Option = if result.is_unit() { None } else {