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
use serde::de::Error as DeError;
use serde::{Deserialize, Deserializer};
use serde_json::json;
use super::prelude::*;
use crate::builder::CreateAutocompleteResponse;
use crate::http::Http;
use crate::internal::prelude::{JsonMap, StdResult, Value};
use crate::model::id::{ApplicationId, ChannelId, GuildId, InteractionId};
use crate::model::interactions::{
application_command::ApplicationCommandInteractionData,
InteractionType,
};
use crate::model::prelude::User;
use crate::utils;
#[derive(Clone, Debug, Serialize)]
#[non_exhaustive]
pub struct AutocompleteInteraction {
pub id: InteractionId,
pub application_id: ApplicationId,
#[serde(rename = "type")]
pub kind: InteractionType,
pub data: ApplicationCommandInteractionData,
#[serde(skip_serializing_if = "Option::is_none")]
pub guild_id: Option<GuildId>,
pub channel_id: ChannelId,
#[serde(skip_serializing_if = "Option::is_none")]
pub member: Option<Member>,
pub user: User,
pub token: String,
pub version: u8,
pub guild_locale: Option<String>,
pub locale: String,
}
impl AutocompleteInteraction {
pub async fn create_autocomplete_response<F>(&self, http: impl AsRef<Http>, f: F) -> Result<()>
where
F: FnOnce(&mut CreateAutocompleteResponse) -> &mut CreateAutocompleteResponse,
{
let mut response = CreateAutocompleteResponse::default();
f(&mut response);
let data = utils::hashmap_to_json_map(response.0);
let map = json!({
"type": 8,
"data": data,
});
http.as_ref().create_interaction_response(self.id.0, &self.token, &map).await
}
}
impl<'de> Deserialize<'de> for AutocompleteInteraction {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> StdResult<Self, D::Error> {
let mut map = JsonMap::deserialize(deserializer)?;
let id = map.get("guild_id").and_then(|x| x.as_str()).and_then(|x| x.parse::<u64>().ok());
if let Some(guild_id) = id {
if let Some(member) = map.get_mut("member").and_then(|x| x.as_object_mut()) {
member.insert("guild_id".to_string(), Value::Number(Number::from(guild_id)));
}
if let Some(data) = map.get_mut("data") {
if let Some(resolved) = data.get_mut("resolved") {
if let Some(roles) = resolved.get_mut("roles") {
if let Some(values) = roles.as_object_mut() {
for value in values.values_mut() {
value.as_object_mut().expect("couldn't deserialize").insert(
"guild_id".to_string(),
Value::String(guild_id.to_string()),
);
}
}
}
if let Some(channels) = resolved.get_mut("channels") {
if let Some(values) = channels.as_object_mut() {
for value in values.values_mut() {
value
.as_object_mut()
.expect("couldn't deserialize application command")
.insert(
"guild_id".to_string(),
Value::String(guild_id.to_string()),
);
}
}
}
}
}
}
let id = map
.remove("id")
.ok_or_else(|| DeError::custom("expected id"))
.and_then(InteractionId::deserialize)
.map_err(DeError::custom)?;
let application_id = map
.remove("application_id")
.ok_or_else(|| DeError::custom("expected application id"))
.and_then(ApplicationId::deserialize)
.map_err(DeError::custom)?;
let kind = map
.remove("type")
.ok_or_else(|| DeError::custom("expected type"))
.and_then(InteractionType::deserialize)
.map_err(DeError::custom)?;
let data = map
.remove("data")
.ok_or_else(|| DeError::custom("expected data"))
.and_then(ApplicationCommandInteractionData::deserialize)
.map_err(DeError::custom)?;
let guild_id = match map.contains_key("guild_id") {
true => Some(
map.remove("guild_id")
.ok_or_else(|| DeError::custom("expected guild_id"))
.and_then(GuildId::deserialize)
.map_err(DeError::custom)?,
),
false => None,
};
let channel_id = map
.remove("channel_id")
.ok_or_else(|| DeError::custom("expected channel_id"))
.and_then(ChannelId::deserialize)
.map_err(DeError::custom)?;
let member = match map.contains_key("member") {
true => Some(
map.remove("member")
.ok_or_else(|| DeError::custom("expected member"))
.and_then(Member::deserialize)
.map_err(DeError::custom)?,
),
false => None,
};
let user = match map.contains_key("user") {
true => map
.remove("user")
.ok_or_else(|| DeError::custom("expected user"))
.and_then(User::deserialize)
.map_err(DeError::custom)?,
false => member.as_ref().expect("expected user or member").user.clone(),
};
let token = map
.remove("token")
.ok_or_else(|| DeError::custom("expected token"))
.and_then(String::deserialize)
.map_err(DeError::custom)?;
let version = map
.remove("version")
.ok_or_else(|| DeError::custom("expected version"))
.and_then(u8::deserialize)
.map_err(DeError::custom)?;
let guild_locale = match map.contains_key("guild_locale") {
true => Some(
map.remove("guild_locale")
.ok_or_else(|| DeError::custom("expected guild_locale"))
.and_then(String::deserialize)
.map_err(DeError::custom)?,
),
false => None,
};
let locale = map
.remove("locale")
.ok_or_else(|| DeError::custom("expected locale"))
.and_then(String::deserialize)
.map_err(DeError::custom)?;
Ok(Self {
id,
application_id,
kind,
data,
guild_id,
channel_id,
member,
user,
token,
version,
guild_locale,
locale,
})
}
}