From 5e3201e7c5c4444b08f9b90b1312b884583d2dd1 Mon Sep 17 00:00:00 2001 From: Rafael Bradley Date: Thu, 21 Aug 2025 21:59:37 -0300 Subject: [PATCH] Add support for presence to the framework --- src/framework/bot.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/framework/bot.rs b/src/framework/bot.rs index a72dfad..076628c 100644 --- a/src/framework/bot.rs +++ b/src/framework/bot.rs @@ -3,8 +3,9 @@ use std::{fmt::Debug, sync::Arc}; use async_trait::async_trait; use thiserror::Error; use twilight_cache_inmemory::{DefaultInMemoryCache, ResourceType}; -use twilight_gateway::{Event, EventTypeFlags, Intents, Shard, ShardId, StreamExt}; +use twilight_gateway::{ConfigBuilder, Event, EventTypeFlags, Intents, Shard, ShardId, StreamExt}; use twilight_http::Client; +use twilight_model::gateway::payload::outgoing::update_presence::UpdatePresencePayload; use crate::{ framework::{ @@ -92,6 +93,7 @@ where pub struct Bot { shard: ShardId, state: Arc, + presence: Option, prefixes: Arc + Send + Sync>, intents: Intents, cached_resource_types: Vec, @@ -113,6 +115,7 @@ impl Bot { Self { shard: ShardId::new(0, 1), state: Arc::new(state), + presence: None, prefixes: Arc::new(prefixes), intents: Intents::empty(), cached_resource_types: Vec::new(), @@ -140,6 +143,11 @@ impl Bot { self } + pub fn presence(mut self, presence: UpdatePresencePayload) -> Self { + self.presence = Some(presence); + self + } + pub fn command(mut self, command: impl Command + 'static) -> Self { self.commands.push(Arc::new(command)); self @@ -250,7 +258,15 @@ impl Bot { cache, }); - let mut gateway = Shard::new(bot.shard, token.to_string(), bot.intents); + let mut config = ConfigBuilder::new(token.to_string(), bot.intents); + + if let Some(presence) = &bot.presence { + config = config.presence(presence.clone()); + } + + let config = config.build(); + + let mut gateway = Shard::with_config(bot.shard, config); loop { if let Some(Ok(event)) = gateway.next_event(EventTypeFlags::all()).await {