Skip to content

Commit da2f52c

Browse files
committed
Truncate embed fields in guild_info
1 parent c29d0a9 commit da2f52c

3 files changed

Lines changed: 31 additions & 20 deletions

File tree

tts_commands/src/owner.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use self::serenity::{
1515
use poise::{serenity_prelude as serenity, CreateReply};
1616

1717
use tts_core::{
18-
common::dm_generic,
18+
common::{dm_generic, safe_truncate},
1919
database,
2020
database_models::Compact,
2121
structs::{Command, CommandResult, Context, PrefixContext, TTSModeChoice},
@@ -342,7 +342,7 @@ fn process_cache_info(
342342
Some(fields)
343343
}
344344

345-
#[poise::command(prefix_command, owners_only)]
345+
#[poise::command(prefix_command, owners_only, hide_in_help)]
346346
pub async fn cache_info(ctx: Context<'_>, kind: Option<String>) -> CommandResult {
347347
ctx.defer().await?;
348348

@@ -394,8 +394,12 @@ fn format_channels<'a>(channels: impl Iterator<Item = &'a serenity::GuildChannel
394394
let mut out = String::new();
395395
for channel in channels {
396396
writeln!(out, "`{}`: {}", channel.id, channel.base.name).unwrap();
397+
if out.len() >= 1024 {
398+
break;
399+
}
397400
}
398401

402+
safe_truncate(&mut out, 1024);
399403
out
400404
}
401405

@@ -406,7 +410,7 @@ fn get_runner_channel(
406410
ctx.runners.get(&shard_id).map(|entry| entry.1.clone())
407411
}
408412

409-
#[poise::command(prefix_command, owners_only)]
413+
#[poise::command(prefix_command, owners_only, hide_in_help)]
410414
pub async fn guild_info(ctx: Context<'_>, guild_id: Option<serenity::GuildId>) -> CommandResult {
411415
let cache = ctx.cache();
412416
let Some(guild_id) = guild_id.or(ctx.guild_id()) else {
@@ -423,7 +427,7 @@ pub async fn guild_info(ctx: Context<'_>, guild_id: Option<serenity::GuildId>) -
423427
.footer(CreateEmbedFooter::new(&*footer))
424428
.title(&*title);
425429

426-
let permissions_formatted;
430+
let mut permissions_formatted;
427431
let mut guild_cached = false;
428432
if let Some(guild) = cache.guild(guild_id) {
429433
guild_cached = true;
@@ -433,6 +437,7 @@ pub async fn guild_info(ctx: Context<'_>, guild_id: Option<serenity::GuildId>) -
433437
"Administrator"
434438
} else {
435439
permissions_formatted = permissions.to_string();
440+
safe_truncate(&mut permissions_formatted, 256);
436441
&permissions_formatted
437442
};
438443

tts_core/src/common.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,18 @@ pub async fn confirm_dialog(
438438

439439
confirm_dialog_wait(ctx.serenity_context(), message.id, ctx.author().id).await
440440
}
441+
442+
/// Avoid char boundary panics with utf8 chars
443+
pub fn safe_truncate(string: &mut String, mut new_len: usize) {
444+
if string.len() <= new_len {
445+
return;
446+
}
447+
448+
new_len -= 3;
449+
while !string.is_char_boundary(new_len) {
450+
new_len -= 1;
451+
}
452+
453+
string.truncate(new_len);
454+
string.push_str("...");
455+
}

tts_core/src/errors.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use self::serenity::{
1212
use poise::serenity_prelude as serenity;
1313

1414
use crate::{
15-
common::push_permission_names,
15+
common::{push_permission_names, safe_truncate},
1616
constants,
1717
opt_ext::OptionTryUnwrap,
1818
structs::{Context, Data},
@@ -42,19 +42,6 @@ fn hash(data: &[u8]) -> Vec<u8> {
4242
Vec::from(&*hasher.finalize())
4343
}
4444

45-
fn truncate_error(error: &Error) -> String {
46-
let mut long_err = error.to_string();
47-
48-
// Avoid char boundary panics with utf8 chars
49-
let mut new_len = 256;
50-
while !long_err.is_char_boundary(new_len) {
51-
new_len -= 1;
52-
}
53-
54-
long_err.truncate(new_len);
55-
long_err
56-
}
57-
5845
pub async fn handle_unexpected<'a>(
5946
ctx: &serenity::Context,
6047
event: &'a str,
@@ -112,8 +99,12 @@ pub async fn handle_unexpected<'a>(
11299
];
113100

114101
let mut embed = serenity::CreateEmbed::default()
115-
.title(truncate_error(&error))
116-
.colour(constants::RED);
102+
.colour(constants::RED)
103+
.title({
104+
let mut error_str = error.to_string();
105+
safe_truncate(&mut error_str, 256);
106+
error_str
107+
});
117108

118109
for (title, mut value, inline) in before_fields
119110
.into_iter()

0 commit comments

Comments
 (0)