Skip to content

Commit

Permalink
Use ToArrayString instead of ToString for ints
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Mar 8, 2024
1 parent 04c4950 commit c9947c2
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 60 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ anyhow = "1"
tracing = "0.1"
sysinfo = "0.30"
itertools = "0.12"
arrayvec = "0.7.4"
parking_lot = "0.12"
typesize = { version = "0.1.2", features = ["arrayvec"] }
to-arraystring = { git = "https://github.com/GnomedDev/to-arraystring" }

[workspace.dependencies.sqlx]
version = "0.7"
Expand Down
2 changes: 2 additions & 0 deletions tts_commands/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ tracing = { workspace = true }
serenity = { workspace = true }
typesize = { workspace = true }
songbird = { workspace = true }
arrayvec = { workspace = true }
to-arraystring = { workspace = true }

tts_core = { path = "../tts_core" }
15 changes: 8 additions & 7 deletions tts_commands/src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::fmt::Write;
use std::{borrow::Cow, fmt::Write};

use indexmap::IndexMap;

Expand Down Expand Up @@ -233,12 +233,13 @@ pub async fn command_func(ctx: Context<'_>, command: Option<&str>) -> CommandRes
.icon_url(ctx.author().face()),
)
.footer(serenity::CreateEmbedFooter::new(match mode {
HelpCommandMode::Group(c) => ctx
.gettext("Use `/help {command_name} [command]` for more info on a command")
.replace("{command_name}", &c.qualified_name),
HelpCommandMode::Command(_) | HelpCommandMode::Root => ctx
.gettext("Use `/help [command]` for more info on a command")
.to_string(),
HelpCommandMode::Group(c) => Cow::Owned(
ctx.gettext("Use `/help {command_name} [command]` for more info on a command")
.replace("{command_name}", &c.qualified_name),
),
HelpCommandMode::Command(_) | HelpCommandMode::Root => {
Cow::Borrowed(ctx.gettext("Use `/help [command]` for more info on a command"))
}
}));

ctx.send(poise::CreateReply::default().embed(embed)).await?;
Expand Down
5 changes: 3 additions & 2 deletions tts_commands/src/main_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use songbird::error::JoinError;

use poise::serenity_prelude::{self as serenity, builder::*, colours::branding::YELLOW};

use to_arraystring::ToArrayString as _;
use tts_core::{
common::random_footer,
database_models::GuildRow,
Expand Down Expand Up @@ -49,7 +50,7 @@ async fn channel_check(
"You ran this command in the wrong channel, please move to <#{channel_id}>.",
);

Cow::Owned(msg.replace("{channel_id}", &setup_id.to_string()))
Cow::Owned(msg.replace("{channel_id}", &setup_id.get().to_arraystring()))
} else {
Cow::Borrowed(ctx.gettext("Your setup channel has been deleted, please run /setup!"))
}
Expand Down Expand Up @@ -169,7 +170,7 @@ pub async fn join(ctx: Context<'_>) -> CommandResult {

ctx.say(
ctx.gettext("I am already in <#{channel_id}>!")
.replace("{channel_id}", &bot_channel_id.to_string()),
.replace("{channel_id}", &bot_channel_id.get().to_arraystring()),
)
.await?;
return Ok(());
Expand Down
20 changes: 11 additions & 9 deletions tts_commands/src/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use poise::{
CreateReply,
};

use to_arraystring::ToArrayString;
use tts_core::{
common::{fetch_audio, prepare_url},
constants::OPTION_SEPERATORS,
Expand Down Expand Up @@ -60,7 +61,7 @@ pub async fn uptime(ctx: Context<'_>) -> CommandResult {
ctx.say(
ctx.gettext("{user_mention} has been up since: <t:{timestamp}:R>")
.replace("{user_mention}", &current_user_mention)
.replace("{timestamp}", &timestamp.to_string()),
.replace("{timestamp}", &timestamp.to_arraystring()),
)
.await?;

Expand Down Expand Up @@ -148,7 +149,7 @@ async fn _tts(ctx: Context<'_>, author: &serenity::User, message: &str) -> Comma
&voice,
mode,
&speaking_rate,
&u64::MAX.to_string(),
&u64::MAX.to_arraystring(),
translation_lang,
);

Expand Down Expand Up @@ -223,11 +224,12 @@ pub async fn botstats(ctx: Context<'_>) -> CommandResult {
let guilds: Vec<_> = guild_ids.iter().filter_map(|id| cache.guild(*id)).collect();

(
guilds.len(),
guilds.len().to_arraystring(),
guilds
.iter()
.filter(|g| g.voice_states.contains_key(&bot_user_id))
.count(),
.count()
.to_arraystring(),
guilds
.into_iter()
.map(|g| g.member_count)
Expand Down Expand Up @@ -319,10 +321,10 @@ and can be used by {total_members} people!",
)
.replace("{sep1}", sep1)
.replace("{sep2}", sep2)
.replace("{total_guild_count}", &total_guild_count.to_string())
.replace("{total_voice_clients}", &total_voice_clients.to_string())
.replace("{total_guild_count}", &total_guild_count)
.replace("{total_voice_clients}", &total_voice_clients)
.replace("{total_members}", &total_members)
.replace("{shard_count}", &shard_count.to_string())
.replace("{shard_count}", &shard_count.get().to_arraystring())
.replace("{ram_usage}", &format!("{ram_usage:.1}"))
.replace("{scheduler_stats}", &scheduler_stats),
);
Expand Down Expand Up @@ -351,7 +353,7 @@ pub async fn channel(ctx: Context<'_>) -> CommandResult {
} else {
let msg = ctx
.gettext("The current setup channel is: <#{channel}>")
.replace("{channel}", &channel.to_string());
.replace("{channel}", &channel.get().to_arraystring());

Cow::Owned(msg)
}
Expand Down Expand Up @@ -394,7 +396,7 @@ pub async fn ping(ctx: Context<'_>) -> CommandResult {

let msg = ctx
.gettext("Current Latency: {}ms")
.replace("{}", &ping_before.elapsed()?.as_millis().to_string());
.replace("{}", &ping_before.elapsed()?.as_millis().to_arraystring());

ping_msg
.edit(ctx, CreateReply::default().content(msg))
Expand Down
5 changes: 3 additions & 2 deletions tts_commands/src/premium.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use poise::{
CreateReply,
};

use to_arraystring::ToArrayString;
use tts_core::{
common::remove_premium,
constants::PREMIUM_NEUTRAL_COLOUR,
Expand Down Expand Up @@ -62,8 +63,8 @@ pub async fn premium_activate(ctx: Context<'_>) -> CommandResult {
if linked_guilds as u8 >= tier.entitled_servers {
Some(Cow::Owned(ctx
.gettext("Hey, you already have {server_count} servers linked, you are only subscribed to the {entitled_servers} tier!")
.replace("{entitled_servers}", &tier.entitled_servers.to_string())
.replace("{server_count}", &linked_guilds.to_string())
.replace("{entitled_servers}", &tier.entitled_servers.to_arraystring())
.replace("{server_count}", &linked_guilds.to_arraystring())
))
} else {
None
Expand Down
37 changes: 21 additions & 16 deletions tts_commands/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ mod voice_paginator;

use std::{borrow::Cow, collections::HashMap, fmt::Write, num::NonZeroU8};

use arrayvec::ArrayString;
use poise::serenity_prelude as serenity;
use serenity::{
builder::*,
small_fixed_array::{FixedString, TruncatingInto},
Mentionable,
};
use to_arraystring::ToArrayString;

use tts_core::{
common::{confirm_dialog, random_footer},
Expand Down Expand Up @@ -131,14 +133,17 @@ pub async fn settings(ctx: Context<'_>) -> CommandResult {
let user_voice_row = data.user_voice_db.get((author_id.into(), mode)).await?;
let (default, kind) = mode
.speaking_rate_info()
.map_or((1.0, "x"), |info| (info.default, info.kind));
.map_or(("1.0", "x"), |info| (info.default, info.kind));

(
Cow::Owned(user_voice_row.speaking_rate.unwrap_or(default).to_string()),
user_voice_row
.speaking_rate
.map(f32::to_arraystring)
.unwrap_or(ArrayString::from(default)?),
kind,
)
} else {
(Cow::Borrowed("1.0"), "x")
(ArrayString::from("1.0").unwrap(), "x")
};

let neutral_colour = ctx.neutral_colour().await;
Expand All @@ -161,7 +166,7 @@ pub async fn settings(ctx: Context<'_>) -> CommandResult {
.replace("{sep1}", sep1)
.replace("{prefix}", prefix)
.replace("{channel_mention}", &channel_mention)
.replace("{autojoin}", &guild_row.auto_join().to_string())
.replace("{autojoin}", &guild_row.auto_join().to_arraystring())
.replace("{role_mention}", required_role.as_deref().unwrap_or(none_str)),
false)
.field("**TTS Settings**", &ctx.gettext("
Expand All @@ -178,22 +183,22 @@ pub async fn settings(ctx: Context<'_>) -> CommandResult {
{sep2} Max Repeated Characters: `{repeated_chars}`
")
.replace("{sep2}", sep2)
.replace("{xsaid}", &guild_row.xsaid().to_string())
.replace("{bot_ignore}", &guild_row.bot_ignore().to_string())
.replace("{audience_ignore}", &guild_row.audience_ignore().to_string())
.replace("{require_voice}", &guild_row.require_voice().to_string())
.replace("{xsaid}", &guild_row.xsaid().to_arraystring())
.replace("{bot_ignore}", &guild_row.bot_ignore().to_arraystring())
.replace("{audience_ignore}", &guild_row.audience_ignore().to_arraystring())
.replace("{require_voice}", &guild_row.require_voice().to_arraystring())
.replace("{required_prefix}", guild_row.required_prefix.as_deref().unwrap_or(none_str))
.replace("{guild_mode}", guild_mode.into())
.replace("{default_voice}", &default_voice)
.replace("{msg_length}", &guild_row.msg_length.to_string())
.replace("{repeated_chars}", &guild_row.repeated_chars.map_or(0, NonZeroU8::get).to_string()),
.replace("{msg_length}", &guild_row.msg_length.to_arraystring())
.replace("{repeated_chars}", &guild_row.repeated_chars.map_or(0, NonZeroU8::get).to_arraystring()),
false)
.field(ctx.gettext("**Translation Settings (Premium Only)**"), &ctx.gettext("
{sep4} Translation: `{to_translate}`
{sep4} Translation Language: `{target_lang}`
")
.replace("{sep4}", sep4)
.replace("{to_translate}", &guild_row.to_translate().to_string())
.replace("{to_translate}", &guild_row.to_translate().to_arraystring())
.replace("{target_lang}", target_lang),
false)
.field("**User Specific**", &ctx.gettext("
Expand Down Expand Up @@ -957,7 +962,7 @@ pub async fn repeated_characters(
.await?;
Cow::Owned(
ctx.gettext("Max repeated characters is now: {}")
.replace("{}", &chars.to_string()),
.replace("{}", &chars.to_arraystring()),
)
}
};
Expand Down Expand Up @@ -1000,7 +1005,7 @@ pub async fn msg_length(
.await?;
Cow::Owned(
ctx.gettext("Max message length is now: {} seconds")
.replace("{}", &seconds.to_string()),
.replace("{}", &seconds.to_arraystring()),
)
}
};
Expand Down Expand Up @@ -1051,10 +1056,10 @@ pub async fn speaking_rate(
let to_send = {
if speaking_rate > max {
ctx.gettext("**Error**: Cannot set the speaking rate multiplier above {max}{kind}")
.replace("{max}", &max.to_string())
.replace("{max}", &max.to_arraystring())
} else if speaking_rate < min {
ctx.gettext("**Error**: Cannot set the speaking rate multiplier below {min}{kind}")
.replace("{min}", &min.to_string())
.replace("{min}", &min.to_arraystring())
} else {
data.userinfo_db.create_row(author.id.get() as i64).await?;
data.user_voice_db
Expand All @@ -1065,7 +1070,7 @@ pub async fn speaking_rate(
)
.await?;
ctx.gettext("Your speaking rate is now: {speaking_rate}{kind}")
.replace("{speaking_rate}", &speaking_rate.to_string())
.replace("{speaking_rate}", &speaking_rate.to_arraystring())
}
}
.replace("{kind}", kind);
Expand Down
Loading

0 comments on commit c9947c2

Please sign in to comment.