use super::super::CommandBorrowed;
use crate::{
client::Client,
error::Error,
request::{Request, RequestBuilder, TryIntoRequest},
response::{Response, ResponseFuture},
routing::Route,
};
use std::{collections::HashMap, future::IntoFuture};
use twilight_model::{
application::command::{Command, CommandType},
guild::Permissions,
id::{
marker::{ApplicationMarker, GuildMarker},
Id,
},
};
use twilight_validate::command::{name as validate_name, CommandValidationError};
#[must_use = "requests must be configured and executed"]
pub struct CreateGuildUserCommand<'a> {
application_id: Id<ApplicationMarker>,
default_member_permissions: Option<Permissions>,
guild_id: Id<GuildMarker>,
http: &'a Client,
name: &'a str,
name_localizations: Option<&'a HashMap<String, String>>,
nsfw: Option<bool>,
}
impl<'a> CreateGuildUserCommand<'a> {
pub(crate) fn new(
http: &'a Client,
application_id: Id<ApplicationMarker>,
guild_id: Id<GuildMarker>,
name: &'a str,
) -> Result<Self, CommandValidationError> {
validate_name(name)?;
Ok(Self {
application_id,
default_member_permissions: None,
guild_id,
http,
name,
name_localizations: None,
nsfw: None,
})
}
pub const fn default_member_permissions(mut self, default: Permissions) -> Self {
self.default_member_permissions = Some(default);
self
}
pub fn name_localizations(
mut self,
localizations: &'a HashMap<String, String>,
) -> Result<Self, CommandValidationError> {
for name in localizations.values() {
validate_name(name)?;
}
self.name_localizations = Some(localizations);
Ok(self)
}
pub const fn nsfw(mut self, nsfw: bool) -> Self {
self.nsfw = Some(nsfw);
self
}
#[deprecated(since = "0.14.0", note = "use `.await` or `into_future` instead")]
pub fn exec(self) -> ResponseFuture<Command> {
self.into_future()
}
}
impl IntoFuture for CreateGuildUserCommand<'_> {
type Output = Result<Response<Command>, Error>;
type IntoFuture = ResponseFuture<Command>;
fn into_future(self) -> Self::IntoFuture {
let http = self.http;
match self.try_into_request() {
Ok(request) => http.request(request),
Err(source) => ResponseFuture::error(source),
}
}
}
impl TryIntoRequest for CreateGuildUserCommand<'_> {
fn try_into_request(self) -> Result<Request, Error> {
Request::builder(&Route::CreateGuildCommand {
application_id: self.application_id.get(),
guild_id: self.guild_id.get(),
})
.json(&CommandBorrowed {
application_id: Some(self.application_id),
default_member_permissions: self.default_member_permissions,
dm_permission: None,
description: None,
description_localizations: None,
kind: CommandType::User,
name: self.name,
name_localizations: self.name_localizations,
nsfw: self.nsfw,
options: None,
})
.map(RequestBuilder::build)
}
}