Add permission checks to p.prefix command
This commit is contained in:
+25
-1
@@ -2,6 +2,7 @@ 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_http::Client;
|
||||
|
||||
@@ -88,11 +89,12 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Bot<T: Send + Sync = ()> {
|
||||
pub struct Bot<T: Send + Sync> {
|
||||
shard: ShardId,
|
||||
state: Arc<T>,
|
||||
prefixes: Arc<dyn PrefixGetter<T> + Send + Sync>,
|
||||
intents: Intents,
|
||||
cached_resource_types: Vec<ResourceType>,
|
||||
commands: Vec<Arc<dyn Command<T>>>,
|
||||
components: Vec<Arc<dyn Component<T>>>,
|
||||
on_ready_handlers: Vec<Arc<dyn OnReadyHandler<T>>>,
|
||||
@@ -113,6 +115,7 @@ impl<T: 'static + Send + Sync> Bot<T> {
|
||||
state: Arc::new(state),
|
||||
prefixes: Arc::new(prefixes),
|
||||
intents: Intents::empty(),
|
||||
cached_resource_types: Vec::new(),
|
||||
commands: Vec::new(),
|
||||
components: Vec::new(),
|
||||
on_ready_handlers: Vec::new(),
|
||||
@@ -147,6 +150,14 @@ impl<T: 'static + Send + Sync> Bot<T> {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn cache_resource_type(mut self, resource_type: ResourceType) -> Self {
|
||||
if !self.cached_resource_types.contains(&resource_type) {
|
||||
self.cached_resource_types.push(resource_type);
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_ready(mut self, handler: impl OnReadyHandler<T> + 'static) -> Self {
|
||||
self.on_ready_handlers.push(Arc::new(handler));
|
||||
self
|
||||
@@ -219,6 +230,16 @@ impl<T: 'static + Send + Sync> Bot<T> {
|
||||
|
||||
let bot = Arc::new(self);
|
||||
|
||||
let mut resource_types = ResourceType::empty();
|
||||
|
||||
for resource_type in &bot.cached_resource_types {
|
||||
resource_types.insert(*resource_type);
|
||||
}
|
||||
|
||||
let cache = DefaultInMemoryCache::builder()
|
||||
.resource_types(resource_types)
|
||||
.build();
|
||||
|
||||
let runtime = Arc::new(Runtime {
|
||||
http,
|
||||
app,
|
||||
@@ -226,6 +247,7 @@ impl<T: 'static + Send + Sync> Bot<T> {
|
||||
state: bot.state.clone(),
|
||||
prefixes: bot.prefixes.clone(),
|
||||
commands: bot.commands.clone(),
|
||||
cache,
|
||||
});
|
||||
|
||||
let mut gateway = Shard::new(bot.shard, token.to_string(), bot.intents);
|
||||
@@ -236,6 +258,8 @@ impl<T: 'static + Send + Sync> Bot<T> {
|
||||
let task_bot = bot.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
task_rt.cache.update(&event);
|
||||
|
||||
match event {
|
||||
Event::Ready(event) => {
|
||||
for handler in &task_bot.on_ready_handlers {
|
||||
|
||||
Vendored
-3
@@ -1,3 +0,0 @@
|
||||
pub struct RedisCache {
|
||||
|
||||
}
|
||||
@@ -33,6 +33,9 @@ pub enum CommandError {
|
||||
|
||||
#[error(transparent)]
|
||||
DiscordDeserializationFailed(#[from] DeserializeBodyError),
|
||||
|
||||
#[error("The cache's state was inconsistent.")]
|
||||
CacheStateInconsistent,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
|
||||
@@ -158,7 +158,6 @@
|
||||
//! ```
|
||||
|
||||
pub mod bot;
|
||||
pub mod cache;
|
||||
pub mod commands;
|
||||
pub mod events;
|
||||
pub mod interactions;
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use twilight_cache_inmemory::InMemoryCache;
|
||||
use twilight_http::Client;
|
||||
use twilight_model::oauth::Application;
|
||||
|
||||
@@ -45,6 +46,8 @@ pub struct Runtime<T> {
|
||||
|
||||
/// The application state.
|
||||
pub state: Arc<T>,
|
||||
|
||||
pub cache: InMemoryCache,
|
||||
}
|
||||
|
||||
impl<T> Runtime<T> {
|
||||
|
||||
Reference in New Issue
Block a user