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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//! Command permission checkers.
//!
//! Not all commands are meant to be run by all users, nor in all contexts. For example, moderation
//! commands must only be run by moderators. Dyncord supports permission checking before a command
//! is run, which simplifies the task of checking if the user is allowed to run the command.
//!
//! # Quick Start
//!
//! > Note: This example uses prefixed commands, but the same code is valid for other command
//! > types. Permission checks are command type-indifferent.
//!
//! Let's simulate we're making a gardening bot. Our bot will be simple, it'll have a `water`
//! command to water our plants. Plants must not be watered at night, though, since it attracts
//! pests. Let's start by creating our command.
//!
//! ```
//! async fn handle_water(ctx: PrefixedContext) {
//! ctx.send("Watering plants...").await.ok();
//! }
//!
//! let bot = Bot::new(())
//! .intents(Intents::GUILD_MESSAGES)
//! .intents(Intents::MESSAGE_CONTENT)
//! .with_prefix("!")
//! .command(Command::prefixed("water", handle_water));
//!
//! bot.run("token").await.unwrap();
//! ```
//!
//! Perfect. Now when we run `!water`, our plants get watered.
//!
//! All permission checkers follow the same signature:
//!
//! ```
//! async fn function_name(ctx: PermissionContext) -> Result<(), Error>;
//! ```
//!
//! You can set `Error` to any error type you want. Handling is simple: `Ok(())` means the user is
//! allowed to run the command, `Err(Anything)` means the user is not allowed to run the command.
//!
//! Let's create our permission checker and a custom error for when it's not daytime.
//!
//! ```
//! #[derive(Debug, thiserror::Error)]
//! #[error("It's not daytime! Water your plants tomorrow.")]
//! struct NotDaytime;
//!
//! async fn is_daytime(ctx: PermissionContext) -> Result<(), NotDaytime> {
//! Ok(())
//! }
//! ```
//!
//! We'll use [`chrono`] to check whether it's daytime.
//!
//! ```
//! async fn is_daytime(ctx: PermissionContext) -> Result<(), NotDaytime> {
//! let now = Local::now();
//!
//! if now.hour() < 8 || now.hour() >= 20 {
//! return Err(NotDaytime);
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! Now, let's add our permission checker to our command.
//!
//! ```
//! let bot = Bot::new(())
//! .intents(Intents::GUILD_MESSAGES)
//! .intents(Intents::MESSAGE_CONTENT)
//! .with_prefix("!")
//! .command(Command::prefixed("water", handle_water).check(is_daytime)); // This line.
//! ```
//!
//! Great! Try running your command. You'll see that if it's not between 8 AM and 8 PM the bot
//! won't respond. Good.
//!
//! Now, it's not really user-friendly if we just go silent when it's not daytime. We have to let
//! the user know why the command is not being run. Let's make an error handler for that.
//!
//! > Note: If you're not familiar with error handling, you may want to read
//! > [the error-handling documentation](crate::errors).
//!
//! ```
//! async fn on_error(ctx: ErrorContext, error: DyncordError) {
//! // We check if the inner error is our NotDaytime error type.
//! if error.downcast::<NotDaytime>().is_some() {
//! ctx.send("Don't water plants at night! Come back once there's daylight.").await.ok();
//! }
//! }
//! ```
//!
//! We're just missing to add our error handler to our bot.
//!
//! ```
//! let bot = Bot::new(())
//! .intents(Intents::GUILD_MESSAGES)
//! .intents(Intents::MESSAGE_CONTENT)
//! .with_prefix("!")
//! .command(Command::prefixed("water", handle_water).check(is_daytime))
//! .on_error(on_error); // Add this line.
//! ```
//!
//! Re-run your bot and try running your command. If it's daytime, the command will run normally.
//! However, if it's past 8 PM and before 8 AM, you'll instead see the error message. Well done!
//!
//! # Multiple Error Handlers
//!
//! It's common to want to make multiple checks before running a command. With dyncord, you can add
//! multiple checks to a command and they'll be checked for in order before the command is run. If
//! any of those checks fails, the command is not run and that error is passed to the corresponding
//! error handler.
//!
//! For example:
//!
//! ```
//! Command::prefixed("hello", handle_hello)
//! .check(my_check_1)
//! .check(my_check_2)
//! .check(my_check_3);
//! ```
//!
//! They'll run serially in order before the command is run.
use Error;
use Arc;
use Event;
use crateHandle;
use crateStateBound;
use crateDynFuture;
use crateUser;
/// The context in which the permission is being checked.
/// An error returned by a permission checker.
pub type PermissionError = ;
/// The result type all permission checksers must return.
pub type PermissionResult = ;
/// Normalizes permission checker results into [`PermissionResult`].
/// A trait implemented by all permission checker functions.