Skip to content

Commit

Permalink
Add experimental new message formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed May 5, 2024
1 parent 0f8b9cc commit cbffd3c
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 36 deletions.
50 changes: 37 additions & 13 deletions tts_commands/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,27 +522,30 @@ pub async fn bot_ban(ctx: Context<'_>, user: serenity::UserId, value: bool) -> C
Ok(())
}

fn replace_bool(ctx: Context<'_>, original: &str, value: bool) -> String {
ctx.gettext(original).replace(
"{}",
if value {
ctx.gettext("Enabled")
} else {
ctx.gettext("Disabled")
},
)
}

async fn generic_bool_command(
ctx: Context<'_>,
key: &'static str,
value: Option<bool>,
resp: &'static str,
) -> CommandResult {
let value = require!(bool_button(ctx, value).await?, Ok(()));
let resp = ctx.gettext(resp).replace(
"{}",
if value {
ctx.gettext("Enabled")
} else {
ctx.gettext("Disabled")
},
);

ctx.data()
.guilds_db
.set_one(ctx.guild_id().unwrap().into(), key, &value)
.await?;
ctx.say(resp).await?;
let guilds_db = &ctx.data().guilds_db;
let guild_id = ctx.guild_id().unwrap();

guilds_db.set_one(guild_id.into(), key, &value).await?;
ctx.say(replace_bool(ctx, resp, value)).await?;

Ok(())
}
Expand Down Expand Up @@ -633,6 +636,26 @@ create_bool_command!(
check = "crate::premium_command_check",
);

/// Enables the experimental new message formatting
#[poise::command(
prefix_command,
slash_command,
required_bot_permissions = "SEND_MESSAGES"
)]
async fn use_new_formatting(
ctx: Context<'_>,
#[description = "Whether to use the experimental new message formatting"] value: bool,
) -> CommandResult {
let id = ctx.author().id.into();
let userinfo = &ctx.data().userinfo_db;

userinfo.set_one(id, "use_new_formatting", value).await?;

let resp = ctx.gettext("Experimental new message formatting is now: {}");
ctx.say(replace_bool(ctx, resp, value)).await?;
Ok(())
}

/// Changes the required role to use the bot.
#[poise::command(
guild_only,
Expand Down Expand Up @@ -1458,6 +1481,7 @@ pub fn commands() -> [Command; 5] {
command_prefix(),
block(),
bot_ban(),
use_new_formatting(),
],
..set()
},
Expand Down
117 changes: 95 additions & 22 deletions tts_core/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::borrow::Cow;
use std::fmt::Write;
use std::num::NonZeroU8;

use itertools::Itertools;
Expand Down Expand Up @@ -293,12 +292,6 @@ pub async fn run_checks(
}
}

let mut removed_chars_content = content.clone();
removed_chars_content.retain(|c| !" ?.)'!\":".contains(c));
if removed_chars_content.is_empty() {
return Ok(None);
}

Ok(Some((content, to_autojoin)))
}

Expand All @@ -316,6 +309,7 @@ pub fn clean_msg(
xsaid: bool,
repeated_limit: Option<NonZeroU8>,
nickname: Option<&str>,
use_new_formatting: bool,

regex_cache: &RegexCache,
last_to_xsaid_tracker: &LastToXsaidTracker,
Expand Down Expand Up @@ -362,7 +356,40 @@ pub fn clean_msg(
state.should_announce_name(&guild, user.id)
});

if announce_name {
let attached_file_format = attachments_to_format(attachments);
let said_name = announce_name.then(|| {
nickname
.or(member_nick)
.or(user.global_name.as_deref())
.unwrap_or(&user.name)
});

if use_new_formatting {
format_message(&mut content, said_name, contained_url, attached_file_format);
} else {
format_message_legacy(&mut content, said_name, contained_url, attached_file_format);
}

if xsaid {
last_to_xsaid_tracker.insert(guild_id, LastXsaidInfo::new(user.id));
}

if let Some(repeated_limit) = repeated_limit {
content = remove_repeated_chars(&content, repeated_limit.get());
}

content
}

pub fn format_message_legacy(
content: &mut String,
said_name: Option<&str>,
contained_url: bool,
attached_file_format: Option<&str>,
) {
use std::fmt::Write;

if let Some(said_name) = said_name {
if contained_url {
let suffix = if content.is_empty() {
"a link."
Expand All @@ -373,12 +400,7 @@ pub fn clean_msg(
write!(content, " {suffix}",).unwrap();
}

let said_name = nickname
.or(member_nick)
.or(user.global_name.as_deref())
.unwrap_or(&user.name);

content = match attachments_to_format(attachments) {
*content = match attached_file_format {
Some(file_format) if content.is_empty() => format!("{said_name} sent {file_format}"),
Some(file_format) => format!("{said_name} sent {file_format} and said {content}"),
None => format!("{said_name} said: {content}"),
Expand All @@ -392,16 +414,67 @@ pub fn clean_msg(

write!(content, "{suffix}",).unwrap();
}
}

if xsaid {
last_to_xsaid_tracker.insert(guild_id, LastXsaidInfo::new(user.id));
}

if let Some(repeated_limit) = repeated_limit {
content = remove_repeated_chars(&content, repeated_limit.get());
pub fn format_message(
content: &mut String,
said_name: Option<&str>,
contained_url: bool,
attached_file_format: Option<&str>,
) {
match (
said_name,
content.trim(),
contained_url,
attached_file_format,
) {
(Some(said_name), "", true, Some(format)) => {
*content = format!("{said_name} sent a link and attached {format}");
}
(Some(said_name), "", true, None) => {
*content = format!("{said_name} sent a link");
}
(Some(said_name), "", false, Some(format)) => {
*content = format!("{said_name} sent {format}");
}
// Fallback, this shouldn't occur
(Some(said_name), "", false, None) => {
*content = format!("{said_name} sent a message");
}
(Some(said_name), msg, true, Some(format)) => {
*content = format!("{said_name} sent a link, attached {format}, and said {msg}");
}
(Some(said_name), msg, true, None) => {
*content = format!("{said_name} sent a link and said {msg}");
}
(Some(said_name), msg, false, Some(format)) => {
*content = format!("{said_name} sent {format} and said {msg}");
}
(Some(said_name), msg, false, None) => {
*content = format!("{said_name} said: {msg}");
}
(None, "", true, Some(format)) => {
*content = format!("A link and {format}");
}
(None, "", true, None) => {
"A link".clone_into(content);
}
(None, "", false, Some(format)) => {
format.clone_into(content);
}
// Again, fallback, there is nothing to say
(None, "", false, None) => {}
(None, msg, true, Some(format)) => {
*content = format!("{msg} with {format} and a link");
}
(None, msg, true, None) => {
*content = format!("{msg} with a link");
}
(None, msg, false, Some(format)) => {
*content = format!("{msg} with {format}");
}
(None, _msg, false, None) => {}
}

content
}

pub fn confirm_dialog_components<'a>(
Expand Down
3 changes: 3 additions & 0 deletions tts_core/src/database_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub struct UserRowRaw {
pub dm_blocked: bool,
pub dm_welcomed: bool,
pub bot_banned: bool,
pub use_new_formatting: bool,
pub voice_mode: Option<TTSMode>,
pub premium_voice_mode: Option<TTSMode>,
}
Expand All @@ -122,6 +123,7 @@ pub struct UserRow {
pub dm_blocked: bool,
pub dm_welcomed: bool,
pub bot_banned: bool,
pub use_new_formatting: bool,
pub voice_mode: Option<TTSMode>,
pub premium_voice_mode: Option<TTSMode>,
}
Expand All @@ -137,6 +139,7 @@ impl Compact for UserRowRaw {
.set_dm_blocked(self.dm_blocked)
.set_dm_welcomed(self.dm_welcomed)
.set_bot_banned(self.bot_banned)
.set_use_new_formatting(self.use_new_formatting)
}
}

Expand Down
8 changes: 8 additions & 0 deletions tts_events/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,21 @@ async fn process_tts_msg(
guild_row.xsaid(),
guild_row.repeated_chars,
nickname_row.name.as_deref(),
user_row.use_new_formatting(),
&data.regex_cache,
&data.last_to_xsaid_tracker,
);

(voice, mode)
};

// Final check, make sure we aren't sending an empty message or just symbols.
let mut removed_chars_content = content.clone();
removed_chars_content.retain(|c| !" ?.)'!\":".contains(c));
if removed_chars_content.is_empty() {
return Ok(());
}

let speaking_rate = data.speaking_rate(message.author.id, mode).await?;
let url = prepare_url(
data.config.tts_service.clone(),
Expand Down
3 changes: 2 additions & 1 deletion tts_migrations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ async fn _run(
ALTER TABLE userinfo
ADD COLUMN IF NOT EXISTS voice_mode TTSMode,
ADD COLUMN IF NOT EXISTS premium_voice_mode TTSMode,
ADD COLUMN IF NOT EXISTS bot_banned bool DEFAULT False;
ADD COLUMN IF NOT EXISTS bot_banned bool DEFAULT False,
ADD COLUMN IF NOT EXISTS use_new_formatting bool DEFAULT False;
ALTER TABLE guilds
ADD COLUMN IF NOT EXISTS audience_ignore bool DEFAULT True,
ADD COLUMN IF NOT EXISTS voice_mode TTSMode DEFAULT 'gtts',
Expand Down

0 comments on commit cbffd3c

Please sign in to comment.