1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use super::super::CommandBorrowed;
use crate::{
client::Client,
error::Error as HttpError,
request::{Request, RequestBuilder, TryIntoRequest},
response::ResponseFuture,
routing::Route,
};
use twilight_model::{
application::command::{Command, CommandOption, CommandType},
id::{
marker::{ApplicationMarker, GuildMarker},
Id,
},
};
use twilight_validate::command::{
chat_input_name as validate_chat_input_name, description as validate_description,
options as validate_options, CommandValidationError,
};
#[must_use = "requests must be configured and executed"]
pub struct CreateGuildChatInputCommand<'a> {
application_id: Id<ApplicationMarker>,
default_permission: Option<bool>,
description: &'a str,
guild_id: Id<GuildMarker>,
http: &'a Client,
name: &'a str,
options: Option<&'a [CommandOption]>,
}
impl<'a> CreateGuildChatInputCommand<'a> {
pub(crate) fn new(
http: &'a Client,
application_id: Id<ApplicationMarker>,
guild_id: Id<GuildMarker>,
name: &'a str,
description: &'a str,
) -> Result<Self, CommandValidationError> {
validate_description(&description)?;
validate_chat_input_name(name)?;
Ok(Self {
application_id,
default_permission: None,
description,
guild_id,
http,
name,
options: None,
})
}
pub fn default_permission(mut self, default: bool) -> Self {
self.default_permission.replace(default);
self
}
pub fn command_options(
mut self,
options: &'a [CommandOption],
) -> Result<Self, CommandValidationError> {
validate_options(options)?;
self.options = Some(options);
Ok(self)
}
pub fn exec(self) -> ResponseFuture<Command> {
let http = self.http;
match self.try_into_request() {
Ok(request) => http.request(request),
Err(source) => ResponseFuture::error(source),
}
}
}
impl TryIntoRequest for CreateGuildChatInputCommand<'_> {
fn try_into_request(self) -> Result<Request, HttpError> {
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_permission: self.default_permission,
description: Some(self.description),
kind: CommandType::ChatInput,
name: self.name,
options: self.options,
})
.map(RequestBuilder::build)
}
}