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
use crate::{
client::Client,
error::Error as HttpError,
request::{
application::{InteractionError, InteractionErrorType},
validate, Pending, Request,
},
routing::Route,
};
use twilight_model::{
application::command::{Command, CommandOption},
id::ApplicationId,
};
pub struct CreateGlobalCommand<'a> {
command: Command,
application_id: ApplicationId,
fut: Option<Pending<'a, ()>>,
http: &'a Client,
optional_option_added: bool,
}
impl<'a> CreateGlobalCommand<'a> {
pub(crate) fn new(
http: &'a Client,
application_id: ApplicationId,
name: impl Into<String>,
description: impl Into<String>,
) -> Result<Self, InteractionError> {
let name = name.into();
let description = description.into();
if !validate::command_name(&name) {
return Err(InteractionError {
kind: InteractionErrorType::CommandNameValidationFailed { name },
});
}
if !validate::command_description(&description) {
return Err(InteractionError {
kind: InteractionErrorType::CommandDescriptionValidationFailed { description },
});
}
Ok(Self {
command: Command {
application_id: Some(application_id),
guild_id: None,
name,
default_permission: None,
description,
id: None,
options: vec![],
},
application_id,
fut: None,
http,
optional_option_added: false,
})
}
pub fn add_command_option(mut self, option: CommandOption) -> Result<Self, InteractionError> {
if !self.optional_option_added && !option.is_required() {
self.optional_option_added = true
}
if option.is_required() && self.optional_option_added {
return Err(InteractionError {
kind: InteractionErrorType::CommandOptionsRequiredFirst { option },
});
}
self.command.options.push(option);
Ok(self)
}
pub fn default_permission(mut self, default: bool) -> Self {
self.command.default_permission.replace(default);
self
}
fn start(&mut self) -> Result<(), HttpError> {
let request = Request::builder(Route::CreateGlobalCommand {
application_id: self.application_id.0,
})
.json(&self.command)?;
self.fut
.replace(Box::pin(self.http.verify(request.build())));
Ok(())
}
}
poll_req!(CreateGlobalCommand<'_>, ());