From 11f6f1edbfd3e6684cd50f4fc13c67347cfa262b Mon Sep 17 00:00:00 2001 From: paring Date: Wed, 7 Jan 2026 20:29:29 +0900 Subject: [PATCH] cache --- Cargo.lock | 2 +- Cargo.toml | 3 ++- src/bot/handler.rs | 18 ++++++++++++++++++ src/bot/main.rs | 6 +++++- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 51acb4b..04021a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1867,7 +1867,7 @@ dependencies = [ [[package]] name = "serenity" version = "0.12.4" -source = "git+https://github.com/serenity-rs/serenity?branch=next#c21fc6f5c94b72ce8c0a6ef8f7e343e87f080d09" +source = "git+https://github.com/paring-chan/serenity.git?branch=fix%2Freaction-cache-next#62962a31fe92ba45d6741c715082fd0a5f12f2a9" dependencies = [ "aformat", "arrayvec", diff --git a/Cargo.toml b/Cargo.toml index 77f03f3..26d7492 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,8 +15,9 @@ figment = { version = "0.10.19", features = ["toml", "env"] } rhai = "1.23.6" secrecy = { version = "0.10", features = ["serde"] } serde = { version = "1.0.228", features = ["derive"] } -serenity = { git = "https://github.com/serenity-rs/serenity", branch = "next", features = [ +serenity = { git = "https://github.com/paring-chan/serenity.git", branch = "fix/reaction-cache-next", features = [ "unstable", + "cache", ] } sqlx = { version = "0.8.6", features = ["runtime-tokio", "postgres"] } tokio = { version = "1.49.0", features = ["full"] } diff --git a/src/bot/handler.rs b/src/bot/handler.rs index 134f761..d92c6af 100644 --- a/src/bot/handler.rs +++ b/src/bot/handler.rs @@ -205,6 +205,11 @@ impl Handler { return Ok(()); } + debug!( + "cached message: {:?}", + ctx.cache.message(channel_id, message_id).as_deref() + ); + let msg = channel_id .message((ctx.cache(), ctx.http()), message_id) .await?; @@ -225,6 +230,14 @@ impl Handler { CloneBuilderMessage::from(msg.clone()) }; + debug!( + "cached channel: {:?}", + ctx.cache + .guild(guild_id) + .as_ref() + .and_then(|x| x.channel(channel_id).clone()) + ); + let channel = channel_id .to_channel((ctx.cache(), ctx.http()), Some(guild_id)) .await?; @@ -269,6 +282,11 @@ impl Handler { .parse() .context("unable to parse counter channel id")?; + debug!( + "cached counter msg: {:?}", + ctx.cache.message(chn_id, msg_id).as_deref() + ); + let existing_message = chn_id.message((ctx.cache(), ctx.http()), msg_id).await?; for comp in existing_message diff --git a/src/bot/main.rs b/src/bot/main.rs index 6818f25..cfb2d86 100644 --- a/src/bot/main.rs +++ b/src/bot/main.rs @@ -11,6 +11,7 @@ use secrecy::ExposeSecret; use serenity::{ Client, all::{GatewayIntents, Token}, + cache, }; use sqlx::{migrate, postgres::PgPoolOptions}; use tracing::Level; @@ -53,18 +54,21 @@ async fn main() -> anyhow::Result<()> { info!("migrated database"); + let mut cache_settings = cache::Settings::default(); + cache_settings.max_messages = 100; + let mut client = Client::builder( Token::from_str(config.bot.token.expose_secret()).unwrap(), GatewayIntents::GUILD_MESSAGES | GatewayIntents::GUILD_MESSAGE_REACTIONS | GatewayIntents::GUILDS - | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT, ) .event_handler(Arc::new(Handler { db, message_lock: Arc::new(DashMap::new()), })) + .cache_settings(cache_settings) .await .expect("Err creating client");