Skip to content

Commit ddafdc9

Browse files
committed
rebase to serenity@next
1 parent e218a01 commit ddafdc9

File tree

11 files changed

+797
-454
lines changed

11 files changed

+797
-454
lines changed

Cargo.lock

Lines changed: 539 additions & 227 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ serde = { version = "1.0", features = ["derive"] }
1212
serde_json = { version = "1.0" }
1313

1414
# Discord API
15-
poise = "0.6"
16-
serenity = "0.12"
15+
serenity = { git = "https://github.com/serenity-rs/serenity", branch = "next" }
16+
poise = { git = "https://github.com/serenity-rs/poise", branch = "serenity-next" }
1717
tokio = { version = "1.29.1", features = ["macros", "signal", "rt-multi-thread"] }
1818

1919
# Misc
20-
regex = "1.10.2"
21-
octocrab = "0.19.0"
22-
reqwest = "0.11.22"
20+
regex = "1.10"
2321
hex = "0.4.3"
24-
to-arraystring = "0.1.3"
22+
octocrab = "0.38"
23+
reqwest = "0.12"
24+
to-arraystring = "0.2"
2525
arrayvec = "0.7.4"
26+
futures = "0.3.30"
27+
aformat = "0.1.3"

src/commands/mod.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ pub(crate) const ACCENT_COLOUR: Colour = Colour(0x8957e5);
55
pub(crate) const OK_COLOUR: Colour = Colour(0x2ecc71);
66
pub(crate) const ERROR_COLOUR: Colour = Colour(0xe74c3c);
77

8-
use arrayvec::ArrayString;
98
use to_arraystring::ToArrayString;
109

1110
use crate::{Context, Error};
@@ -24,7 +23,7 @@ pub async fn register(ctx: Context<'_>) -> Result<(), Error> {
2423
Ok(())
2524
}
2625

27-
pub async fn respond_embed(ctx: &Context<'_>, embed: CreateEmbed, ephemeral: bool) {
26+
pub async fn respond_embed(ctx: &Context<'_>, embed: CreateEmbed<'_>, ephemeral: bool) {
2827
let builder = poise::CreateReply::default()
2928
.embed(embed)
3029
.ephemeral(ephemeral);
@@ -64,7 +63,7 @@ pub async fn interaction_err(ctx: &serenity::Context, press: &ComponentInteracti
6463
)
6564
.ephemeral(true),
6665
);
67-
let _ = press.create_response(ctx, builder).await;
66+
let _ = press.create_response(&ctx.http, builder).await;
6867
}
6968

7069
enum Kind {
@@ -91,13 +90,8 @@ pub async fn paginate_lists(
9190
) -> Result<(), Error> {
9291
let ctx_id = ctx.id().to_arraystring();
9392

94-
let mut prev_button_id = ArrayString::<24>::new();
95-
prev_button_id.push_str(&ctx_id);
96-
prev_button_id.push_str("prev");
97-
98-
let mut next_button_id = ArrayString::<24>::new();
99-
next_button_id.push_str(&ctx_id);
100-
next_button_id.push_str("next");
93+
let prev_button_id = format!("{ctx_id}prev");
94+
let next_button_id = format!("{ctx_id}next");
10195

10296
let colour = Colour::TEAL;
10397

@@ -134,10 +128,11 @@ pub async fn paginate_lists(
134128
let msg = ctx.send(reply).await?;
135129

136130
if pages.len() > 1 {
137-
while let Some(press) = ComponentInteractionCollector::new(ctx)
138-
.filter(move |press| press.data.custom_id.starts_with(&ctx_id.to_string()))
139-
.timeout(std::time::Duration::from_secs(180))
140-
.await
131+
while let Some(press) =
132+
ComponentInteractionCollector::new(ctx.serenity_context().shard.clone())
133+
.filter(move |press| press.data.custom_id.starts_with(ctx_id.as_str()))
134+
.timeout(std::time::Duration::from_secs(180))
135+
.await
141136
{
142137
match Kind::from_id(&press.data.custom_id, &ctx_id) {
143138
Some(Kind::Next) => {
@@ -154,7 +149,7 @@ pub async fn paginate_lists(
154149

155150
press
156151
.create_response(
157-
ctx.serenity_context(),
152+
ctx.http(),
158153
CreateInteractionResponse::UpdateMessage(
159154
CreateInteractionResponseMessage::new().embed(
160155
serenity::CreateEmbed::new()

src/commands/snippets.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,29 @@ pub async fn create_snippet(
6666
#[description = "The snippet's title"] title: String,
6767
#[description = "The snippet's content"] content: String,
6868
) -> Result<(), Error> {
69+
let snippet = Snippet {
70+
id: id.clone(),
71+
title,
72+
content: content.replace(r"\n", "\n"),
73+
};
74+
75+
let mut embed = snippet.embed();
76+
embed = embed.colour(super::OK_COLOUR);
77+
6978
let embed = {
70-
let mut rwlock_guard = ctx.data().state.write().unwrap();
79+
let data = ctx.data();
80+
let mut rwlock_guard = data.state.write().unwrap();
7181

7282
if let Some(position) = rwlock_guard.snippets.iter().position(|s| s.id.eq(&id)) {
7383
rwlock_guard.snippets.remove(position);
7484
}
7585

76-
let snippet = Snippet {
77-
id,
78-
title,
79-
content: content.replace(r"\n", "\n"),
80-
};
81-
8286
println!("New snippet created '{}: {}'", snippet.id, snippet.title);
8387

84-
let mut embed = snippet.embed();
85-
86-
embed = embed.colour(super::OK_COLOUR);
87-
88-
rwlock_guard.snippets.push(snippet);
88+
rwlock_guard.snippets.push(snippet.clone());
8989
rwlock_guard.write();
9090

91-
embed
91+
embed.clone()
9292
};
9393

9494
respond_embed(&ctx, embed, false).await;
@@ -117,7 +117,8 @@ pub async fn edit_snippet(
117117
}
118118

119119
{
120-
let mut rwlock_guard = ctx.data().state.write().unwrap();
120+
let data = ctx.data();
121+
let mut rwlock_guard = data.state.write().unwrap();
121122
rwlock_guard.snippets.push(snippet.clone());
122123
println!("Snippet edited '{}: {}'", snippet.title, snippet.content);
123124
rwlock_guard.write();
@@ -206,8 +207,10 @@ pub async fn export_snippet(
206207
) -> Result<(), Error> {
207208
match get_snippet_lazy(&ctx, &id) {
208209
Some(snippet) => {
209-
let attachment =
210-
CreateAttachment::bytes(snippet.content.replace('\n', r"\n"), "snippet.txt");
210+
let attachment = CreateAttachment::bytes(
211+
snippet.content.replace('\n', r"\n").into_bytes(),
212+
"snippet.txt",
213+
);
211214
let message = poise::CreateReply::default()
212215
.attachment(attachment)
213216
.embed(snippet.embed());
@@ -224,7 +227,7 @@ pub async fn export_snippet(
224227
}
225228

226229
impl Embeddable for Snippet {
227-
fn embed(&self) -> CreateEmbed {
230+
fn embed(&self) -> CreateEmbed<'_> {
228231
CreateEmbed::default()
229232
.title(&self.title)
230233
.description(&self.content)
@@ -297,10 +300,11 @@ async fn remove_snippet_confirm(ctx: &Context<'_>, snippet: &Snippet) -> Result<
297300

298301
ctx.send(builder).await?;
299302

300-
while let Some(press) = serenity::ComponentInteractionCollector::new(ctx)
301-
.filter(move |press| press.data.custom_id.starts_with(&ctx_id.to_string()))
302-
.timeout(std::time::Duration::from_secs(60))
303-
.await
303+
while let Some(press) =
304+
serenity::ComponentInteractionCollector::new(ctx.serenity_context().shard.clone())
305+
.filter(move |press| press.data.custom_id.starts_with(&ctx_id.to_string()))
306+
.timeout(std::time::Duration::from_secs(60))
307+
.await
304308
{
305309
if press.data.custom_id == delete_id {
306310
handle_delete(ctx, snippet, press).await?;
@@ -320,7 +324,7 @@ async fn handle_delete(
320324
rm_snippet(ctx, snippet);
321325
interaction
322326
.create_response(
323-
ctx,
327+
ctx.http(),
324328
CreateInteractionResponse::UpdateMessage(
325329
CreateInteractionResponseMessage::new()
326330
.content("Deleted!")
@@ -343,7 +347,7 @@ async fn handle_cancel(
343347
) -> Result<(), Error> {
344348
interaction
345349
.create_response(
346-
ctx,
350+
ctx.http(),
347351
CreateInteractionResponse::UpdateMessage(
348352
CreateInteractionResponseMessage::new()
349353
.content("Aborted.")

src/commands/utils.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,15 @@ pub async fn edit_embed(
161161
embedb = embedb.title(title);
162162
}
163163
} else if let Some(t) = &embed.title {
164-
embedb = embedb.title(t);
164+
embedb = embedb.title(t.as_str());
165165
}
166166

167167
if let Some(description) = description {
168168
if description != "_" {
169169
embedb = embedb.description(description);
170170
}
171171
} else if let Some(d) = &embed.description {
172-
embedb = embedb.description(d);
172+
embedb = embedb.description(d.as_str());
173173
}
174174

175175
if let Some(color) = color {
@@ -198,7 +198,7 @@ pub async fn edit_embed(
198198
embedb = embedb.url(url);
199199
}
200200
} else if let Some(u) = &embed.url {
201-
embedb = embedb.url(u);
201+
embedb = embedb.url(u.as_str());
202202
}
203203

204204
if let Some(image) = image {
@@ -291,7 +291,9 @@ pub async fn add_repo(
291291
}
292292

293293
{
294-
let mut rwlock_guard = { ctx.data().state.write().unwrap() };
294+
let data = ctx.data();
295+
let mut rwlock_guard = data.state.write().unwrap();
296+
295297
let details = RepositoryDetails {
296298
owner: owner.clone(),
297299
name: repository.clone(),
@@ -300,6 +302,7 @@ pub async fn add_repo(
300302
rwlock_guard
301303
.issue_prefixes
302304
.insert(key.clone().to_lowercase(), details);
305+
303306
println!(
304307
"Successfully added repository {} for **{}/{}**",
305308
key.to_lowercase(),

src/events/code.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,50 @@
1+
use futures::future::join_all;
12
use regex::{Match, Regex};
23
use std::path::Path;
34
use std::str::FromStr;
45

5-
use poise::serenity_prelude::{self as serenity, Colour, Context, CreateEmbed, Message};
6+
use poise::serenity_prelude::{self as serenity, Colour, CreateEmbed, Http, Message};
7+
use std::sync::Arc;
68

79
use crate::formatting::trim_indent;
810

11+
use crate::FrameworkContext;
12+
913
// A shade of purple.
1014
const ACCENT_COLOUR: Colour = Colour::new(0x8957e5);
1115

12-
pub async fn message(ctx: &Context, message: &Message) {
13-
if let Some(embeds) = get_embeds(ctx, message).await {
14-
let typing = message.channel_id.start_typing(&ctx.http);
16+
pub async fn message(framework: FrameworkContext<'_>, message: &Message) {
17+
let http = &framework.serenity_context.http.clone();
1518

16-
let content = serenity::CreateMessage::default()
17-
.embeds(embeds)
18-
.reference_message(message);
19-
let _ = message.channel_id.send_message(ctx, content).await;
19+
let Some(file_refs) = get_file_refs(http.clone(), message) else {
20+
return;
21+
};
2022

21-
typing.stop();
22-
}
23+
// This is just cursed. I have no other way to explain this but its the only way I can figure
24+
// out how to satisfy the lifetimes.
25+
let embeds = join_all(file_refs.iter().map(FileReference::create_embed))
26+
.await
27+
.into_iter()
28+
.flatten()
29+
.collect::<Vec<_>>();
30+
31+
let typing = message.channel_id.start_typing(http.clone());
32+
33+
let content = serenity::CreateMessage::default()
34+
.embeds(embeds)
35+
.reference_message(message);
36+
let _ = message.channel_id.send_message(http, content).await;
37+
38+
typing.stop();
2339
}
2440

25-
async fn get_embeds(ctx: &Context, message: &Message) -> Option<Vec<CreateEmbed>> {
26-
let typing = message.channel_id.start_typing(&ctx.http);
27-
let mut embeds: Vec<CreateEmbed> = vec![];
41+
fn get_file_refs(http: Arc<Http>, message: &Message) -> Option<Vec<FileReference<'_>>> {
42+
let typing = message.channel_id.start_typing(http);
43+
let mut embeds = vec![];
2844

2945
if let Some(refs) = FileReference::try_from_str(&message.content) {
3046
for file_ref in refs {
31-
if let Some(embed) = file_ref.create_embed().await {
32-
embeds.push(embed);
33-
}
47+
embeds.push(file_ref);
3448
}
3549
}
3650

0 commit comments

Comments
 (0)