Skip to content

Commit

Permalink
feat: split compact message
Browse files Browse the repository at this point in the history
  • Loading branch information
matteopolak committed May 26, 2024
1 parent eda71e6 commit 6de5a08
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 63 deletions.
11 changes: 1 addition & 10 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
[build]
rustflags = ["-C", "target-cpu=haswell"]
rustdocflags = ["-C", "target-cpu=native"]

[target.x86_64-unknown-linux-gnu]

[target.wasm32-unknown-unknown]
rustflags = ["-C", "target-feature=+simd128"]

[target.wasm32-wasi]
rustflags = ["-C", "target-feature=+simd128"]
rustflags = ["-C", "target-cpu=native"]

[net]
git-fetch-with-cli = true
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#![allow(clippy::wildcard_imports)]
#![allow(clippy::get_first)]
#![feature(let_chains)]
#![feature(exclusive_range_pattern)]
#![feature(assert_matches)]
#![feature(associated_type_defaults)]

Expand Down
2 changes: 0 additions & 2 deletions crates/extra/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(exclusive_range_pattern)]

pub mod inverse_bool;
pub mod meters;
pub mod percent;
Expand Down
1 change: 0 additions & 1 deletion crates/minecraft/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![warn(clippy::pedantic)]
#![feature(exclusive_range_pattern)]
#![feature(assert_matches)]
#![feature(const_precise_live_drops)]
#![feature(const_mut_refs)]
Expand Down
2 changes: 1 addition & 1 deletion crates/statpixel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "statpixel"
version = "0.40.0"
version = "0.41.0"
edition = "2021"
license = "GPL-3.0"
authors = ["Matthew Polak <[email protected]>"]
Expand Down
47 changes: 31 additions & 16 deletions crates/statpixel/src/commands/at/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use minecraft::{
text::{parse, Text},
Colour,
};
use poise::serenity_prelude::CreateAttachment;
use poise::serenity_prelude::{CacheHttp, CreateAttachment, CreateMessage};
use skia_safe::textlayout::TextAlign;
use translate::{context, tr, tr_fmt, Error};
use uuid::Uuid;
Expand Down Expand Up @@ -119,15 +119,16 @@ pub async fn command<G: api::canvas::prelude::Game>(
at: format!("<t:{}:f>", created_at.timestamp()),
);

let attachments = G::condensed(ctx, family, data_lhs, suffix.as_deref(), background)
.into_iter()
.map(|mut surface| {
CreateAttachment::bytes(
Cow::Owned(canvas::to_png(&mut surface)),
crate::IMAGE_NAME,
)
})
.collect::<Vec<_>>();
let mut attachments =
G::condensed(ctx, family, data_lhs, suffix.as_deref(), background)
.into_iter()
.map(|mut surface| {
CreateAttachment::bytes(
Cow::Owned(canvas::to_png(&mut surface)),
crate::IMAGE_NAME,
)
})
.collect::<Vec<_>>();

let (_, id) = G::Mode::as_at(
ctx,
Expand All @@ -136,14 +137,28 @@ pub async fn command<G: api::canvas::prelude::Game>(
None,
);

let mut reply = poise::CreateReply::new().content(format!(
"{}\n{content}",
tr_fmt!(ctx, "identifier", identifier: api::id::encode(&id)),
));

reply.attachments = attachments;
let reply = poise::CreateReply::new()
.content(format!(
"{}\n{content}",
tr_fmt!(ctx, "identifier", identifier: api::id::encode(&id)),
))
.attachment(attachments.remove(0));

ctx.send(reply).await?;

let Some(channel_id) = ctx.channel_id() else {
return Ok(());
};

for attachment in attachments {
channel_id
.send_files(
ctx.discord().http(),
Some(attachment),
CreateMessage::default(),
)
.await?;
}
}
format::Display::Text => {
let (player, data_rhs) = commands::get_player_data(ctx, uuid, username).await?;
Expand Down
30 changes: 22 additions & 8 deletions crates/statpixel/src/commands/compare/run.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;

use api::canvas::{self, prelude::Mode};
use poise::serenity_prelude::CreateAttachment;
use poise::serenity_prelude::{CacheHttp, CreateAttachment, CreateMessage};
use translate::{context, tr_fmt, Error};
use uuid::Uuid;

Expand Down Expand Up @@ -100,7 +100,7 @@ pub async fn command<G: api::canvas::prelude::Game>(
to: data_lhs.username.as_str(),
);

let attachments = G::condensed_diff(
let mut attachments = G::condensed_diff(
ctx,
family,
&data_lhs,
Expand All @@ -116,14 +116,28 @@ pub async fn command<G: api::canvas::prelude::Game>(

let (_, id) = G::Mode::as_compare(ctx, player_lhs.uuid, player_rhs.uuid, None);

let mut reply = poise::CreateReply::new().content(format!(
"{}\n{content}",
tr_fmt!(ctx, "identifier", identifier: api::id::encode(&id)),
));

reply.attachments = attachments;
let reply = poise::CreateReply::new()
.content(format!(
"{}\n{content}",
tr_fmt!(ctx, "identifier", identifier: api::id::encode(&id)),
))
.attachment(attachments.remove(0));

ctx.send(reply).await?;

let Some(channel_id) = ctx.channel_id() else {
return Ok(());
};

for attachment in attachments {
channel_id
.send_files(
ctx.discord().http(),
Some(attachment),
CreateMessage::default(),
)
.await?;
}
}
format::Display::Text => {
let (player_rhs, data_rhs) = commands::get_player_data(
Expand Down
33 changes: 24 additions & 9 deletions crates/statpixel/src/commands/games/run.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;

use api::canvas::{self, prelude::Mode};
use poise::serenity_prelude::CreateAttachment;
use poise::serenity_prelude::{CacheHttp, CreateAttachment, CreateMessage};
use translate::{context, tr_fmt};
use uuid::Uuid;

Expand All @@ -22,7 +22,8 @@ pub async fn command<G: api::canvas::prelude::Game>(

player.increase_searches(ctx).await?;

let attachments = G::condensed(ctx, family, &data, suffix.as_deref(), background)
// guaranteed to have >= 1 element
let mut attachments = G::condensed(ctx, family, &data, suffix.as_deref(), background)
.into_iter()
.map(|mut surface| {
CreateAttachment::bytes(
Expand All @@ -33,15 +34,29 @@ pub async fn command<G: api::canvas::prelude::Game>(
.collect::<Vec<_>>();

let (_, id) = G::Mode::as_root(ctx, player.uuid, None);
let mut reply = poise::CreateReply::new().content(format!(
"{}\n{}",
tr_fmt!(ctx, "identifier", identifier: api::id::encode(&id)),
crate::tip::random(ctx),
));

reply.attachments = attachments;
let reply = poise::CreateReply::new()
.content(format!(
"{}\n{}",
tr_fmt!(ctx, "identifier", identifier: api::id::encode(&id)),
crate::tip::random(ctx),
))
.attachment(attachments.remove(0));

ctx.send(reply).await?;

let Some(channel_id) = ctx.channel_id() else {
return Ok(());
};

for attachment in attachments {
channel_id
.send_files(
ctx.discord().http(),
Some(attachment),
CreateMessage::default(),
)
.await?;
}
}
format::Display::Image | format::Display::Compact => {
let (player, data, session, skin, suffix) =
Expand Down
30 changes: 22 additions & 8 deletions crates/statpixel/src/commands/snapshot/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use minecraft::{
text::{parse, Text},
Colour,
};
use poise::serenity_prelude::CreateAttachment;
use poise::serenity_prelude::{CacheHttp, CreateAttachment, CreateMessage};
use skia_safe::textlayout::TextAlign;
use translate::{context, tr, tr_fmt, Error};
use uuid::Uuid;
Expand Down Expand Up @@ -116,7 +116,7 @@ pub async fn command<G: api::canvas::prelude::Game>(
to: format!("<t:{}:f>", Utc::now().timestamp()),
);

let attachments = G::condensed_diff(
let mut attachments = G::condensed_diff(
ctx,
family,
data_lhs,
Expand All @@ -137,14 +137,28 @@ pub async fn command<G: api::canvas::prelude::Game>(
None,
);

let mut reply = poise::CreateReply::new().content(format!(
"{}\n{content}",
tr_fmt!(ctx, "identifier", identifier: api::id::encode(&id)),
));

reply.attachments = attachments;
let reply = poise::CreateReply::new()
.content(format!(
"{}\n{content}",
tr_fmt!(ctx, "identifier", identifier: api::id::encode(&id)),
))
.attachment(attachments.remove(0));

ctx.send(reply).await?;

let Some(channel_id) = ctx.channel_id() else {
return Ok(());
};

for attachment in attachments {
channel_id
.send_files(
ctx.discord().http(),
Some(attachment),
CreateMessage::default(),
)
.await?;
}
}
format::Display::Text => {
let (player, data_rhs) = commands::get_player_data(ctx, uuid, username).await?;
Expand Down
24 changes: 19 additions & 5 deletions crates/statpixel/src/commands/snapshot/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;

use api::canvas;
use chrono::Utc;
use poise::serenity_prelude::CreateAttachment;
use poise::serenity_prelude::{CacheHttp, CreateAttachment, CreateMessage};
use translate::{context, tr_fmt, Error};
use uuid::Uuid;

Expand Down Expand Up @@ -77,7 +77,7 @@ pub async fn command<G: api::canvas::prelude::Game>(
to: format!("<t:{}:f>", Utc::now().timestamp()),
);

let attachments = G::condensed_diff(
let mut attachments = G::condensed_diff(
ctx,
family,
data_lhs,
Expand All @@ -91,11 +91,25 @@ pub async fn command<G: api::canvas::prelude::Game>(
})
.collect::<Vec<_>>();

let mut reply = poise::CreateReply::new().content(content);

reply.attachments = attachments;
let reply = poise::CreateReply::new()
.content(content)
.attachment(attachments.remove(0));

ctx.send(reply).await?;

let Some(channel_id) = ctx.channel_id() else {
return Ok(());
};

for attachment in attachments {
channel_id
.send_files(
ctx.discord().http(),
Some(attachment),
CreateMessage::default(),
)
.await?;
}
}
format::Display::Text => {
let (player, data_rhs) = commands::get_player_data(ctx, Some(uuid_rhs), None).await?;
Expand Down
1 change: 0 additions & 1 deletion crates/statpixel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// and overflows the requirement evaluator
#![allow(clippy::get_first)]
#![feature(let_chains)]
#![feature(exclusive_range_pattern)]
#![feature(iter_intersperse)]
#![feature(iter_array_chunks)]

Expand Down
10 changes: 10 additions & 0 deletions crates/translate/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,16 @@ impl<'c> Context<'c> {
}
}

pub fn channel_id(&self) -> Option<serenity::ChannelId> {
match &self.interaction {
#[cfg(feature = "error")]
ContextInteraction::Command(ctx) => Some(ctx.channel_id()),
ContextInteraction::Component { interaction, .. } => Some(interaction.channel_id),
ContextInteraction::Modal { interaction, .. } => Some(interaction.channel_id),
ContextInteraction::External(..) | ContextInteraction::Empty => None,
}
}

async fn send_modal(
&self,
ctx: &serenity::Context,
Expand Down

0 comments on commit 6de5a08

Please sign in to comment.