Skip to content

Commit ff8a254

Browse files
committed
rebase to serenity@next
1 parent 1926b14 commit ff8a254

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
@@ -6,7 +6,6 @@ pub(crate) const ACCENT_COLOUR: Colour = Colour(0x8957e5);
66
pub(crate) const OK_COLOUR: Colour = Colour(0x2ecc71);
77
pub(crate) const ERROR_COLOUR: Colour = Colour(0xe74c3c);
88

9-
use arrayvec::ArrayString;
109
use to_arraystring::ToArrayString;
1110

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

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

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

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

10397
let colour = Colour::TEAL;
10498

@@ -135,10 +129,11 @@ pub async fn paginate_lists(
135129
let msg = ctx.send(reply).await?;
136130

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

156151
press
157152
.create_response(
158-
ctx.serenity_context(),
153+
ctx.http(),
159154
CreateInteractionResponse::UpdateMessage(
160155
CreateInteractionResponseMessage::new().embed(
161156
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
@@ -163,15 +163,15 @@ pub async fn edit_embed(
163163
embedb = embedb.title(title);
164164
}
165165
} else if let Some(t) = &embed.title {
166-
embedb = embedb.title(t);
166+
embedb = embedb.title(t.as_str());
167167
}
168168

169169
if let Some(description) = description {
170170
if description != "_" {
171171
embedb = embedb.description(description);
172172
}
173173
} else if let Some(d) = &embed.description {
174-
embedb = embedb.description(d);
174+
embedb = embedb.description(d.as_str());
175175
}
176176

177177
if let Some(color) = color {
@@ -200,7 +200,7 @@ pub async fn edit_embed(
200200
embedb = embedb.url(url);
201201
}
202202
} else if let Some(u) = &embed.url {
203-
embedb = embedb.url(u);
203+
embedb = embedb.url(u.as_str());
204204
}
205205

206206
if let Some(image) = image {
@@ -293,7 +293,9 @@ pub async fn add_repo(
293293
}
294294

295295
{
296-
let mut rwlock_guard = { ctx.data().state.write().unwrap() };
296+
let data = ctx.data();
297+
let mut rwlock_guard = data.state.write().unwrap();
298+
297299
let details = RepositoryDetails {
298300
owner: owner.clone(),
299301
name: repository.clone(),
@@ -302,6 +304,7 @@ pub async fn add_repo(
302304
rwlock_guard
303305
.issue_prefixes
304306
.insert(key.clone().to_lowercase(), details);
307+
305308
println!(
306309
"Successfully added repository {} for **{}/{}**",
307310
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::str::FromStr;
34
use std::{path::Path, sync::OnceLock};
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)