Skip to content

Commit 08aece1

Browse files
feat: publish RatelimitEvent to NATS on Discord HTTP rate limits
The ratelimit handler previously only logged a warning. Now it also publishes a RatelimitEvent (path, timeout_ms, global) to the discord.<prefix>.bot.ratelimit subject so agents can observe and react to Discord API rate limits. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 4d16c2d commit 08aece1

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

rsworkspace/crates/discord-bot/src/bridge.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,15 @@ impl DiscordBridge {
22032203
Ok(())
22042204
}
22052205

2206+
pub async fn publish_ratelimit(&self, path: String, timeout_ms: u64, global: bool) -> Result<()> {
2207+
let meta = EventMetadata::new("", self.next_sequence());
2208+
let ev = RatelimitEvent { metadata: meta, path, timeout_ms, global };
2209+
let subject = subjects::bot::ratelimit(self.prefix());
2210+
self.publisher.publish(&subject, &ev).await?;
2211+
debug!("Published ratelimit to {}", subject);
2212+
Ok(())
2213+
}
2214+
22062215
}
22072216

22082217
fn convert_soundboard(sound: &SerenitySoundboard) -> SoundInfo {

rsworkspace/crates/discord-bot/src/handlers/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,13 +1596,20 @@ impl EventHandler for Handler {
15961596
}
15971597
}
15981598

1599-
async fn ratelimit(&self, data: RatelimitInfo) {
1599+
async fn ratelimit(&self, ctx: Context, data: RatelimitInfo) {
1600+
let timeout_ms = data.timeout.as_millis() as u64;
16001601
warn!(
16011602
path = %data.path,
1602-
timeout_ms = data.timeout.as_millis(),
1603+
timeout_ms = timeout_ms,
16031604
global = data.global,
16041605
"Discord HTTP rate limit hit"
16051606
);
1607+
let data_read = ctx.data.read().await;
1608+
if let Some(bridge) = data_read.get::<DiscordBridge>() {
1609+
if let Err(e) = bridge.publish_ratelimit(data.path.clone(), timeout_ms, data.global).await {
1610+
error!("Failed to publish ratelimit: {}", e);
1611+
}
1612+
}
16061613
}
16071614
}
16081615

0 commit comments

Comments
 (0)