Add support for presence to the framework
This commit is contained in:
+18
-2
@@ -3,8 +3,9 @@ use std::{fmt::Debug, sync::Arc};
|
|||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use twilight_cache_inmemory::{DefaultInMemoryCache, ResourceType};
|
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_http::Client;
|
||||||
|
use twilight_model::gateway::payload::outgoing::update_presence::UpdatePresencePayload;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
framework::{
|
framework::{
|
||||||
@@ -92,6 +93,7 @@ where
|
|||||||
pub struct Bot<T: Send + Sync> {
|
pub struct Bot<T: Send + Sync> {
|
||||||
shard: ShardId,
|
shard: ShardId,
|
||||||
state: Arc<T>,
|
state: Arc<T>,
|
||||||
|
presence: Option<UpdatePresencePayload>,
|
||||||
prefixes: Arc<dyn PrefixGetter<T> + Send + Sync>,
|
prefixes: Arc<dyn PrefixGetter<T> + Send + Sync>,
|
||||||
intents: Intents,
|
intents: Intents,
|
||||||
cached_resource_types: Vec<ResourceType>,
|
cached_resource_types: Vec<ResourceType>,
|
||||||
@@ -113,6 +115,7 @@ impl<T: 'static + Send + Sync> Bot<T> {
|
|||||||
Self {
|
Self {
|
||||||
shard: ShardId::new(0, 1),
|
shard: ShardId::new(0, 1),
|
||||||
state: Arc::new(state),
|
state: Arc::new(state),
|
||||||
|
presence: None,
|
||||||
prefixes: Arc::new(prefixes),
|
prefixes: Arc::new(prefixes),
|
||||||
intents: Intents::empty(),
|
intents: Intents::empty(),
|
||||||
cached_resource_types: Vec::new(),
|
cached_resource_types: Vec::new(),
|
||||||
@@ -140,6 +143,11 @@ impl<T: 'static + Send + Sync> Bot<T> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn presence(mut self, presence: UpdatePresencePayload) -> Self {
|
||||||
|
self.presence = Some(presence);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn command(mut self, command: impl Command<T> + 'static) -> Self {
|
pub fn command(mut self, command: impl Command<T> + 'static) -> Self {
|
||||||
self.commands.push(Arc::new(command));
|
self.commands.push(Arc::new(command));
|
||||||
self
|
self
|
||||||
@@ -250,7 +258,15 @@ impl<T: 'static + Send + Sync> Bot<T> {
|
|||||||
cache,
|
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 {
|
loop {
|
||||||
if let Some(Ok(event)) = gateway.next_event(EventTypeFlags::all()).await {
|
if let Some(Ok(event)) = gateway.next_event(EventTypeFlags::all()).await {
|
||||||
|
|||||||
Reference in New Issue
Block a user