Skip to content

Commit

Permalink
Add an option for a bot to work only in certain channels (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
iddm authored and arqunis committed May 24, 2018
1 parent af7f176 commit 457a17e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/framework/standard/configuration.rs
Expand Up @@ -2,7 +2,7 @@ use client::Context;
use http;
use model::{
channel::Message,
id::{GuildId, UserId}
id::{ChannelId, GuildId, UserId}
};
use std::{
collections::HashSet,
Expand Down Expand Up @@ -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>>,
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions src/framework/standard/mod.rs
Expand Up @@ -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
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 457a17e

Please sign in to comment.