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
|
use std::sync::Arc;
use futures::future::join_all;
use tokio::sync::RwLock;
use tokio::sync::Mutex;
use twilight_http::client::Client as TwiClient;
use twilight_http::error::Error as TwiError;
use twilight_http::request::channel::reaction::RequestReactionType;
use twilight_model::channel::message::{AllowedMentions, MentionType, MessageType};
use twilight_model::http::attachment::Attachment;
use super::*;
pub struct Client {
client: Arc<Mutex<TwiClient>>,
bot_conf: Arc<RwLock<BotConfig>>,
}
impl Client {
pub fn new(discord_token: &String, bot_conf: &Arc<RwLock<BotConfig>>) -> Self {
Self {
client: Arc::new(Mutex::new(TwiClient::new(discord_token.clone()))),
bot_conf: bot_conf.clone(),
}
}
pub async fn refetch_message(&self, message_id: MessageId, channel_id: ChannelId) {
let client = self.client.lock().await;
let bot_conf = self.bot_conf.read().await;
let message_channel = bot_conf.message_handler.as_ref().expect("No message handler");
let message = client
.message(channel_id, message_id)
.await
.expect("Could not load message")
.model()
.await
.expect("Could not deserialize message");
let timestamp = if message.edited_timestamp.is_some() {
message.edited_timestamp.unwrap()
} else {
message.timestamp
};
message_channel
.send((timestamp, Message::Complete(message)))
.await;
}
pub async fn delete_message(&self, channel_id: ChannelId, message_id: MessageId) -> Result<(), TwiError> {
let client = self.client.lock().await;
let delete_result = client.delete_message(channel_id, message_id).await;
let member_id = self.bot_conf.read().await.member_id;
drop(client);
match delete_result {
Err(err) => {
match &err.kind() {
twilight_http::error::ErrorType::Response { body: _, error, status: _ } => match error {
twilight_http::api_error::ApiError::General(err) => {
// Code for "Missing Permissions": https://discord.com/developers/docs/topics/opcodes-and-status-codes#json-json-error-codes
if err.code == 50013 {
println!("ERROR: Client {} doesn't have permissions to delete message", member_id);
let _ = self.react_message(channel_id, message_id, &RequestReactionType::Unicode { name: "🔐" }).await;
}
},
_ => (),
},
_ => (),
};
Err(err)
},
_ => Ok(()),
}
}
pub async fn react_message(&self, channel_id: ChannelId, message_id: MessageId, react: &'_ RequestReactionType<'_>) -> Result<(), TwiError> {
let _ = self.client.lock().await.create_reaction(
channel_id,
message_id,
react
).await;
return Ok(())
}
pub async fn duplicate_message(&self, message: &TwiMessage, content: &str) -> Result<TwiMessage, MessageDuplicateError> {
let client = self.client.lock().await;
let mut create_message = client.create_message(message.channel_id).content(content)?;
let mut allowed_mentions = AllowedMentions {
parse: Vec::new(),
replied_user: false,
roles: message.mention_roles.clone(),
users: message.mentions.iter().map(|user| user.id).collect(),
};
if message.mention_everyone {
allowed_mentions.parse.push(MentionType::Everyone);
}
if message.kind == MessageType::Reply {
if let Some(ref_message) = message.referenced_message.as_ref() {
create_message = create_message.reply(ref_message.id);
let pings_referenced_author = message
.mentions
.iter()
.any(|user| user.id == ref_message.author.id);
if pings_referenced_author {
allowed_mentions.replied_user = true;
} else {
allowed_mentions.replied_user = false;
}
} else {
panic!("Cannot proxy message: Was reply but no referenced message");
}
}
let attachments = join_all(message.attachments.iter().map(|attachment| async {
let filename = attachment.filename.clone();
let description_opt = attachment.description.clone();
let bytes = reqwest::get(attachment.proxy_url.clone())
.await?
.bytes()
.await?;
let mut new_attachment =
Attachment::from_bytes(filename, bytes.try_into().unwrap(), attachment.id.into());
if let Some(description) = description_opt {
new_attachment.description(description);
}
Ok(new_attachment)
}))
.await
.iter()
.filter_map(
|result: &Result<Attachment, MessageDuplicateError>| match result {
Ok(attachment) => Some(attachment.clone()),
Err(_) => None,
},
)
.collect::<Vec<_>>();
if attachments.len() > 0 {
create_message = create_message.attachments(attachments.as_slice())?;
}
if let Some(flags) = message.flags {
create_message = create_message.flags(flags);
}
create_message = create_message.allowed_mentions(Some(&allowed_mentions));
let new_message = create_message.await?.model().await?;
Ok(new_message)
}
}
#[derive(Debug)]
pub enum MessageDuplicateError {
MessageValidation(twilight_validate::message::MessageValidationError),
AttachmentRequest(reqwest::Error),
MessageCreate(twilight_http::error::Error),
ResponseDeserialization(twilight_http::response::DeserializeBodyError),
}
impl From<twilight_validate::message::MessageValidationError> for MessageDuplicateError {
fn from(value: twilight_validate::message::MessageValidationError) -> Self {
MessageDuplicateError::MessageValidation(value)
}
}
impl From<reqwest::Error> for MessageDuplicateError {
fn from(value: reqwest::Error) -> Self {
MessageDuplicateError::AttachmentRequest(value)
}
}
impl From<twilight_http::error::Error> for MessageDuplicateError {
fn from(value: twilight_http::error::Error) -> Self {
MessageDuplicateError::MessageCreate(value)
}
}
impl From<twilight_http::response::DeserializeBodyError> for MessageDuplicateError {
fn from(value: twilight_http::response::DeserializeBodyError) -> Self {
MessageDuplicateError::ResponseDeserialization(value)
}
}
|