aboutsummaryrefslogtreecommitdiff
path: root/src/framework
diff options
context:
space:
mode:
authorVictor Polevoy <[email protected]>2018-05-24 11:33:26 +0300
committerAlex M. M <[email protected]>2018-05-24 10:33:26 +0200
commit457a17e059395aab3d1a23bd1cfe6e01ea0b5a61 (patch)
treeccb419eaadba872284b16cfd1abe595596f3321b /src/framework
parentimpl From<{,&'a }CurrentUser> for User (diff)
downloadserenity-457a17e059395aab3d1a23bd1cfe6e01ea0b5a61.tar.xz
serenity-457a17e059395aab3d1a23bd1cfe6e01ea0b5a61.zip
Add an option for a bot to work only in certain channels (#318)
Diffstat (limited to 'src/framework')
-rw-r--r--src/framework/standard/configuration.rs28
-rw-r--r--src/framework/standard/mod.rs15
2 files changed, 42 insertions, 1 deletions
diff --git a/src/framework/standard/configuration.rs b/src/framework/standard/configuration.rs
index 2b7584d..01d3b01 100644
--- a/src/framework/standard/configuration.rs
+++ b/src/framework/standard/configuration.rs
@@ -2,7 +2,7 @@ use client::Context;
use http;
use model::{
channel::Message,
- id::{GuildId, UserId}
+ id::{ChannelId, GuildId, UserId}
};
use std::{
collections::HashSet,
@@ -44,6 +44,7 @@ pub struct Configuration {
#[doc(hidden)] pub allow_whitespace: bool,
#[doc(hidden)] pub blocked_guilds: HashSet<GuildId>,
#[doc(hidden)] pub blocked_users: HashSet<UserId>,
+ #[doc(hidden)] pub allowed_channels: HashSet<ChannelId>,
#[doc(hidden)] pub depth: usize,
#[doc(hidden)] pub disabled_commands: HashSet<String>,
#[doc(hidden)] pub dynamic_prefix: Option<Box<PrefixCheck>>,
@@ -118,6 +119,30 @@ impl Configuration {
self
}
+ /// HashSet of channels Ids where commands will be working.
+ ///
+ /// # Examples
+ ///
+ /// Create a HashSet in-place:
+ ///
+ /// ```rust,no_run
+ /// # use serenity::prelude::*;
+ /// # struct Handler;
+ /// #
+ /// # impl EventHandler for Handler {}
+ /// # let mut client = Client::new("token", Handler).unwrap();
+ /// use serenity::model::id::ChannelId;
+ /// use serenity::framework::StandardFramework;
+ ///
+ /// client.with_framework(StandardFramework::new().configure(|c| c
+ /// .allowed_channels(vec![ChannelId(7), ChannelId(77)].into_iter().collect())));
+ /// ```
+ pub fn allowed_channels(mut self, channels: HashSet<ChannelId>) -> Self {
+ self.allowed_channels = channels;
+
+ self
+ }
+
/// HashSet of user Ids whose commands will be ignored.
/// Guilds owned by user Ids will also be ignored.
///
@@ -454,6 +479,7 @@ impl Default for Configuration {
owners: HashSet::default(),
blocked_users: HashSet::default(),
blocked_guilds: HashSet::default(),
+ allowed_channels: HashSet::default(),
disabled_commands: HashSet::default(),
allow_dm: true,
ignore_webhooks: true,
diff --git a/src/framework/standard/mod.rs b/src/framework/standard/mod.rs
index e3e6395..f25f605 100644
--- a/src/framework/standard/mod.rs
+++ b/src/framework/standard/mod.rs
@@ -159,6 +159,8 @@ pub enum DispatchError {
BlockedUser,
/// When the guild or its owner is blocked in bot configuration.
BlockedGuild,
+ /// When the channel blocked in bot configuration.
+ BlockedChannel,
/// When the command requester lacks specific required permissions.
LackOfPermissions(Permissions),
/// When the command requester has exceeded a ratelimit bucket. The attached
@@ -196,6 +198,7 @@ impl fmt::Debug for DispatchError {
CommandDisabled(ref s) => f.debug_tuple("DispatchError::CommandDisabled").field(&s).finish(),
BlockedUser => write!(f, "DispatchError::BlockedUser"),
BlockedGuild => write!(f, "DispatchError::BlockedGuild"),
+ BlockedChannel => write!(f, "DispatchError::BlockedChannel"),
LackOfPermissions(ref perms) => f.debug_tuple("DispatchError::LackOfPermissions").field(&perms).finish(),
RateLimited(ref num) => f.debug_tuple("DispatchError::RateLimited").field(&num).finish(),
OnlyForDM => write!(f, "DispatchError::OnlyForDM"),
@@ -495,6 +498,14 @@ impl StandardFramework {
false
}
+ #[cfg(feature = "cache")]
+ fn is_blocked_channel(&self, message: &Message) -> bool {
+ !self.configuration.allowed_channels.is_empty()
+ && !self.configuration
+ .allowed_channels
+ .contains(&message.channel_id)
+ }
+
#[allow(too_many_arguments)]
#[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))]
fn should_fail(&mut self,
@@ -561,6 +572,10 @@ impl StandardFramework {
return Some(DispatchError::BlockedGuild);
}
+ if self.is_blocked_channel(message) {
+ return Some(DispatchError::BlockedChannel);
+ }
+
if !has_correct_permissions(command, message) {
return Some(DispatchError::LackOfPermissions(
command.required_permissions,