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, Eq, PartialEq)]
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,
}
}
}
#[derive(Clone)]
pub struct Configuration {
pub(crate) allow_dm: bool,
pub(crate) with_whitespace: WithWhiteSpace,
pub(crate) by_space: bool,
pub(crate) blocked_guilds: HashSet<GuildId>,
pub(crate) blocked_users: HashSet<UserId>,
pub(crate) allowed_channels: HashSet<ChannelId>,
pub(crate) disabled_commands: HashSet<String>,
pub(crate) dynamic_prefixes: Vec<DynamicPrefixHook>,
pub(crate) ignore_bots: bool,
pub(crate) ignore_webhooks: bool,
pub(crate) on_mention: Option<String>,
pub(crate) owners: HashSet<UserId>,
pub(crate) prefixes: Vec<String>,
pub(crate) no_dm_prefix: bool,
pub(crate) delimiters: Vec<Delimiter>,
pub(crate) case_insensitive: bool,
}
impl Configuration {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn allow_dm(mut self, allow_dm: bool) -> Self {
self.allow_dm = allow_dm;
self
}
#[must_use]
pub fn with_whitespace(mut self, with: impl Into<WithWhiteSpace>) -> Self {
self.with_whitespace = with.into();
self
}
#[must_use]
pub fn by_space(mut self, b: bool) -> Self {
self.by_space = b;
self
}
#[must_use]
pub fn allowed_channels(mut self, channels: HashSet<ChannelId>) -> Self {
self.allowed_channels = channels;
self
}
#[must_use]
pub fn blocked_guilds(mut self, guilds: HashSet<GuildId>) -> Self {
self.blocked_guilds = guilds;
self
}
#[must_use]
pub fn blocked_users(mut self, users: HashSet<UserId>) -> Self {
self.blocked_users = users;
self
}
#[inline]
#[must_use]
pub fn disabled_commands(mut self, commands: HashSet<String>) -> Self {
self.disabled_commands = commands;
self
}
#[inline]
#[must_use]
pub fn dynamic_prefix(mut self, dynamic_prefix: DynamicPrefixHook) -> Self {
self.dynamic_prefixes.push(dynamic_prefix);
self
}
#[must_use]
pub fn ignore_bots(mut self, ignore_bots: bool) -> Self {
self.ignore_bots = ignore_bots;
self
}
#[must_use]
pub fn ignore_webhooks(mut self, ignore_webhooks: bool) -> Self {
self.ignore_webhooks = ignore_webhooks;
self
}
#[must_use]
pub fn on_mention(mut self, id_to_mention: Option<UserId>) -> Self {
self.on_mention = id_to_mention.map(|id| id.to_string());
self
}
#[must_use]
pub fn owners(mut self, user_ids: HashSet<UserId>) -> Self {
self.owners = user_ids;
self
}
#[must_use]
pub fn prefix(mut self, prefix: impl Into<String>) -> Self {
let p = prefix.into();
self.prefixes = if p.is_empty() { vec![] } else { vec![p] };
self
}
#[inline]
#[must_use]
pub fn prefixes(mut self, prefixes: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.prefixes = prefixes.into_iter().map(Into::into).filter(|p| !p.is_empty()).collect();
self
}
#[inline]
#[must_use]
pub fn no_dm_prefix(mut self, b: bool) -> Self {
self.no_dm_prefix = b;
self
}
#[must_use]
pub fn delimiter(mut self, delimiter: impl Into<Delimiter>) -> Self {
self.delimiters.clear();
self.delimiters.push(delimiter.into());
self
}
#[must_use]
pub fn delimiters(
mut self,
delimiters: impl IntoIterator<Item = impl Into<Delimiter>>,
) -> Self {
self.delimiters.clear();
self.delimiters.extend(delimiters.into_iter().map(Into::into));
self
}
#[must_use]
pub fn case_insensitivity(mut self, cs: bool) -> 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("~")],
}
}
}