Skip to content

Commit

Permalink
chore: migrate to upstream poise for serenity-rs/poise#275
Browse files Browse the repository at this point in the history
  • Loading branch information
matteopolak committed May 26, 2024
1 parent 96414c5 commit 60afe33
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 159 deletions.
257 changes: 169 additions & 88 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ git = "https://github.com/matteopolak/plotters.git"
branch = "next-release-devel"

[workspace.dependencies.poise]
git = "https://github.com/matteopolak/poise"
git = "https://github.com/serenity-rs/poise"
branch = "serenity-next"

[workspace.dependencies.redis]
Expand Down
4 changes: 3 additions & 1 deletion crates/api/src/canvas/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ pub trait Diff {

#[allow(clippy::module_name_repetitions)]
pub trait DiffLog {
/// Appends the difference between two game modes as a new field.
/// If no difference is found, the embed is returned as is as an Err.
fn diff_log<'e>(
data_lhs: &crate::player::data::Data,
data_rhs: &crate::player::data::Data,
ctx: &translate::context::Context<'_>,
embed: CreateEmbed<'e>,
) -> CreateEmbed<'e>;
) -> Result<CreateEmbed<'e>, CreateEmbed<'e>>;
}

macro_rules! impl_to_formatted_for_number {
Expand Down
22 changes: 18 additions & 4 deletions crates/api/src/player/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,27 @@ impl DiffLog for Data {
data_rhs: &Data,
ctx: &context::Context<'_>,
embed: CreateEmbed<'e>,
) -> CreateEmbed<'e> {
) -> Result<CreateEmbed<'e>, CreateEmbed<'e>> {
let mut is_modified = false;

execute_for_games!(
[let embed = ],
[::diff_log(data_lhs, data_rhs, ctx, embed);]
[let embed = match],
[
::diff_log(data_lhs, data_rhs, ctx, embed) {
Ok(embed) => {
is_modified = true;
embed
}
Err(embed) => embed
};
]
);

embed
if is_modified {
Ok(embed)
} else {
Err(embed)
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/derive/src/game/impls/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ pub(crate) fn impl_mode(
data_rhs: &#api::player::data::Data,
ctx: &#translate::context::Context<'_>,
mut embed: #poise::serenity_prelude::CreateEmbed<'e>,
) -> #poise::serenity_prelude::CreateEmbed<'e> {
) -> Result<#poise::serenity_prelude::CreateEmbed<'e>, #poise::serenity_prelude::CreateEmbed<'e>> {
let mut log = String::new();
let game_lhs = &data_lhs.stats.#path_to_game;
let game_rhs = &data_rhs.stats.#path_to_game;
Expand All @@ -605,9 +605,9 @@ pub(crate) fn impl_mode(
title.push(' ');
title.push_str(#translate::tr(ctx, Self::tr()).as_ref());

embed.field(title, log, true)
Ok(embed.field(title, log, true))
} else {
embed
Err(embed)
}
}
}
Expand Down
33 changes: 26 additions & 7 deletions crates/derive/src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ impl ToTokens for GameInputReceiver {
data_rhs: &#api::player::data::Data,
ctx: &#translate::context::Context<'_>,
mut embed: #poise::serenity_prelude::CreateEmbed<'e>,
) -> #poise::serenity_prelude::CreateEmbed<'e> {
) -> Result<#poise::serenity_prelude::CreateEmbed<'e>, #poise::serenity_prelude::CreateEmbed<'e>> {
let mut log = String::new();
let game_lhs = &data_lhs.stats.#path_to_game;
let game_rhs = &data_rhs.stats.#path_to_game;
Expand All @@ -1278,9 +1278,9 @@ impl ToTokens for GameInputReceiver {
title.push(' ');
title.push_str(#translate::tr(ctx, Overall::tr()).as_ref());

embed.field(title, log, true)
Ok(embed.field(title, log, true))
} else {
embed
Err(embed)
}
}
}
Expand Down Expand Up @@ -1792,7 +1792,13 @@ impl ToTokens for GameInputReceiver {
let ty = mode.ty();

quote! {
let embed = <#ty as #api::canvas::diff::DiffLog>::diff_log(data_lhs, data_rhs, ctx, embed);
let embed = match <#ty as #api::canvas::diff::DiffLog>::diff_log(data_lhs, data_rhs, ctx, embed) {
Err(embed) => embed,
Ok(embed) => {
is_modified = true;
embed
},
};
}
});

Expand Down Expand Up @@ -1993,10 +1999,23 @@ impl ToTokens for GameInputReceiver {
data_rhs: &#api::player::data::Data,
ctx: &#translate::context::Context<'_>,
embed: #poise::serenity_prelude::CreateEmbed<'e>,
) -> #poise::serenity_prelude::CreateEmbed<'e> {
let embed = <#overall_ident as #api::canvas::diff::DiffLog>::diff_log(data_lhs, data_rhs, ctx, embed);
) -> Result<#poise::serenity_prelude::CreateEmbed<'e>, #poise::serenity_prelude::CreateEmbed<'e>> {
let mut is_modified = false;
let embed = match <#overall_ident as #api::canvas::diff::DiffLog>::diff_log(data_lhs, data_rhs, ctx, embed) {
Err(embed) => embed,
Ok(embed) => {
is_modified = true;
embed
},
};

#(#diff_log)*
embed

if is_modified {
Ok(embed)
} else {
Err(embed)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/statpixel/src/snapshot/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ pub async fn begin(
CreateEmbed::default(),
);

if !embed.fields.is_empty() {
if let Ok(embed) = embed {
let player = Player::new(uuid, None);
let message = CreateMessage::default().embed(
embed
Expand Down
70 changes: 16 additions & 54 deletions crates/translate/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,22 +371,14 @@ impl<'c> Context<'c> {
interaction: &serenity::ModalInteraction,
data: poise::CreateReply<'_>,
) -> Result<(), serenity::Error> {
let mut edit = serenity::CreateInteractionResponseMessage::new().embeds(data.embeds);

if let Some(content) = data.content {
edit = edit.content(content);
}

if let Some(components) = data.components {
edit = edit.components(components);
}

edit = edit.files(data.attachments);
let message = data.to_slash_initial_response(
serenity::CreateInteractionResponseMessage::new().add_files([]),
);

interaction
.create_response(
ctx.http(),
serenity::CreateInteractionResponse::UpdateMessage(edit),
serenity::CreateInteractionResponse::UpdateMessage(message),
)
.await?;

Expand All @@ -399,22 +391,14 @@ impl<'c> Context<'c> {
interaction: &serenity::ModalInteraction,
data: poise::CreateReply<'_>,
) -> Result<(), serenity::Error> {
let mut edit = serenity::CreateInteractionResponseMessage::new().embeds(data.embeds);

if let Some(content) = data.content {
edit = edit.content(content);
}

if let Some(components) = data.components {
edit = edit.components(components);
}

edit = edit.files(data.attachments);
let message = data.to_slash_initial_response(
serenity::CreateInteractionResponseMessage::new().add_files([]),
);

interaction
.create_response(
ctx.http(),
serenity::CreateInteractionResponse::Message(edit),
serenity::CreateInteractionResponse::Message(message),
)
.await?;

Expand All @@ -429,42 +413,20 @@ impl<'c> Context<'c> {
data: poise::CreateReply<'_>,
) -> Result<(), serenity::Error> {
if deferred.load(Ordering::SeqCst) {
let mut edit = serenity::EditInteractionResponse::new().embeds(data.embeds);

if let Some(content) = data.content {
edit = edit.content(content);
}
let message = data.to_slash_initial_response_edit(
serenity::EditInteractionResponse::new().clear_attachments(),
);

if let Some(components) = data.components {
edit = edit.components(components);
}

if !data.attachments.is_empty() {
edit = edit.clear_attachments();
}

for attachment in data.attachments {
edit = edit.new_attachment(attachment);
}

interaction.edit_response(ctx.http(), edit).await?;
interaction.edit_response(ctx.http(), message).await?;
} else {
let mut edit = serenity::CreateInteractionResponseMessage::new().embeds(data.embeds);

if let Some(content) = data.content {
edit = edit.content(content);
}

if let Some(components) = data.components {
edit = edit.components(components);
}

edit = edit.files(data.attachments);
let message = data.to_slash_initial_response(
serenity::CreateInteractionResponseMessage::new().files([]),
);

interaction
.create_response(
ctx.http(),
serenity::CreateInteractionResponse::UpdateMessage(edit),
serenity::CreateInteractionResponse::UpdateMessage(message),
)
.await?;
}
Expand Down

0 comments on commit 60afe33

Please sign in to comment.