use std::collections::HashSet;
use futures::future::BoxFuture;
use super::Delimiter;
use crate::client::Context;
use crate::model::channel::Message;
use crate::model::id::{ChannelId, GuildId, UserId};
type DynamicPrefixHook =
for<'fut> fn(&'fut Context, &'fut Message) -> BoxFuture<'fut, Option<String>>;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct WithWhiteSpace {
pub prefixes: bool,
pub groups: bool,
pub commands: bool,
}
impl Default for WithWhiteSpace {
fn default() -> Self {
WithWhiteSpace {
prefixes: false,
groups: true,
commands: true,
}
}
}
impl From<bool> for WithWhiteSpace {
fn from(b: bool) -> Self {
WithWhiteSpace {
prefixes: b,
..Default::default()
}
}
}
impl From<(bool, bool)> for WithWhiteSpace {
fn from((prefixes, groups): (bool, bool)) -> Self {
WithWhiteSpace {
prefixes,
groups,
..Default::default()
}
}
}
impl From<(bool, bool, bool)> for WithWhiteSpace {
fn from((prefixes, groups, commands): (bool, bool, bool)) -> Self {
WithWhiteSpace {
prefixes,
groups,
commands,
}
}
}
pub struct Configuration {
#[doc(hidden)]
pub allow_dm: bool,
#[doc(hidden)]
pub with_whitespace: WithWhiteSpace,
#[doc(hidden)]
pub by_space: 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 disabled_commands: HashSet<String>,
#[doc(hidden)]
pub dynamic_prefixes: Vec<DynamicPrefixHook>,
#[doc(hidden)]
pub ignore_bots: bool,
#[doc(hidden)]
pub ignore_webhooks: bool,
#[doc(hidden)]
pub on_mention: Option<String>,
#[doc(hidden)]
pub owners: HashSet<UserId>,
#[doc(hidden)]
pub prefixes: Vec<String>,
#[doc(hidden)]
pub no_dm_prefix: bool,
#[doc(hidden)]
pub delimiters: Vec<Delimiter>,
#[doc(hidden)]
pub case_insensitive: bool,
}
impl Configuration {
pub fn allow_dm(&mut self, allow_dm: bool) -> &mut Self {
self.allow_dm = allow_dm;
self
}
pub fn with_whitespace<I: Into<WithWhiteSpace>>(&mut self, with: I) -> &mut Self {
self.with_whitespace = with.into();
self
}
pub fn by_space(&mut self, b: bool) -> &mut Self {
self.by_space = b;
self
}
pub fn allowed_channels(&mut self, channels: HashSet<ChannelId>) -> &mut Self {
self.allowed_channels = channels;
self
}
pub fn blocked_guilds(&mut self, guilds: HashSet<GuildId>) -> &mut Self {
self.blocked_guilds = guilds;
self
}
pub fn blocked_users(&mut self, users: HashSet<UserId>) -> &mut Self {
self.blocked_users = users;
self
}
#[inline]
pub fn disabled_commands(&mut self, commands: HashSet<String>) -> &mut Self {
self.disabled_commands = commands;
self
}
#[inline]
pub fn dynamic_prefix(&mut self, dynamic_prefix: DynamicPrefixHook) -> &mut Self {
self.dynamic_prefixes.push(dynamic_prefix);
self
}
pub fn ignore_bots(&mut self, ignore_bots: bool) -> &mut Self {
self.ignore_bots = ignore_bots;
self
}
pub fn ignore_webhooks(&mut self, ignore_webhooks: bool) -> &mut Self {
self.ignore_webhooks = ignore_webhooks;
self
}
pub fn on_mention(&mut self, id_to_mention: Option<UserId>) -> &mut Self {
self.on_mention = id_to_mention.map(|id| id.to_string());
self
}
pub fn owners(&mut self, user_ids: HashSet<UserId>) -> &mut Self {
self.owners = user_ids;
self
}
pub fn prefix(&mut self, prefix: impl ToString) -> &mut Self {
let p = prefix.to_string();
self.prefixes = if p.is_empty() { vec![] } else { vec![p] };
self
}
#[inline]
pub fn prefixes<T, It>(&mut self, prefixes: It) -> &mut Self
where
T: ToString,
It: IntoIterator<Item = T>,
{
self.prefixes =
prefixes.into_iter().map(|p| p.to_string()).filter(|p| !p.is_empty()).collect();
self
}
#[inline]
pub fn no_dm_prefix(&mut self, b: bool) -> &mut Self {
self.no_dm_prefix = b;
self
}
pub fn delimiter<I: Into<Delimiter>>(&mut self, delimiter: I) -> &mut Self {
self.delimiters.clear();
self.delimiters.push(delimiter.into());
self
}
pub fn delimiters<T, It>(&mut self, delimiters: It) -> &mut Self
where
T: Into<Delimiter>,
It: IntoIterator<Item = T>,
{
self.delimiters.clear();
self.delimiters.extend(delimiters.into_iter().map(Into::into));
self
}
pub fn case_insensitivity(&mut self, cs: bool) -> &mut Self {
self.case_insensitive = cs;
for prefix in &mut self.prefixes {
*prefix = prefix.to_lowercase();
}
self
}
}
impl Default for Configuration {
fn default() -> Configuration {
Configuration {
allow_dm: true,
with_whitespace: WithWhiteSpace::default(),
by_space: true,
blocked_guilds: HashSet::default(),
blocked_users: HashSet::default(),
allowed_channels: HashSet::default(),
case_insensitive: false,
delimiters: vec![Delimiter::Single(' ')],
disabled_commands: HashSet::default(),
dynamic_prefixes: Vec::new(),
ignore_bots: true,
ignore_webhooks: true,
no_dm_prefix: false,
on_mention: None,
owners: HashSet::default(),
prefixes: vec![String::from("~")],
}
}
}