Skip to content

Commit

Permalink
Update deps, fix lints, fix server_mode premium modes
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Feb 15, 2025
1 parent 70a9bc5 commit 7ebedab
Show file tree
Hide file tree
Showing 21 changed files with 652 additions and 526 deletions.
964 changes: 554 additions & 410 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ regex = "1"
anyhow = "1"
serde = "1.0.209"
tracing = "0.1"
sysinfo = "0.31.3"
sysinfo = "0.33"
aformat = "0.1.3"
itertools = "0.13"
itertools = "0.14"
arrayvec = "0.7.6"
parking_lot = "0.12"
mini-moka = { version = "0.10.3", features = ["sync"] }
Expand All @@ -89,7 +89,7 @@ default-features = false

[workspace.dependencies.serenity]
git = "https://github.com/serenity-rs/serenity"
features = ["typesize", "temp_cache"]
features = ["typesize", "temp_cache", "transport_compression_zstd"]
default-features = false
branch = "next"

Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ async fn main_(start_time: std::time::SystemTime) -> Result<()> {
let reqwest = reqwest::Client::new();
let auth_key = config.main.tts_service_auth_key.as_deref();

let mut http_builder = serenity::HttpBuilder::new(config.main.token.as_deref().unwrap());
let token = config.main.token.clone();
let mut http_builder = serenity::HttpBuilder::new(token.clone());
if let Some(proxy) = &config.main.proxy_url {
println!("Connecting via proxy");
http_builder = http_builder
Expand Down Expand Up @@ -165,7 +166,7 @@ async fn main_(start_time: std::time::SystemTime) -> Result<()> {
..poise::FrameworkOptions::default()
};

let mut client = serenity::ClientBuilder::new_with_http(http, tts_events::get_intents())
let mut client = serenity::ClientBuilder::new_with_http(token, http, tts_events::get_intents())
.voice_manager::<songbird::Songbird>(data.songbird.clone())
.framework(poise::Framework::new(framework_options))
.data(data as _)
Expand Down
6 changes: 3 additions & 3 deletions tts_commands/src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub async fn autocomplete<'a>(

if result.try_push(choice).is_err() {
return ControlFlow::Break(());
};
}
}
} else {
flatten_commands(result, &command.subcommands, searching)?;
Expand Down Expand Up @@ -157,7 +157,7 @@ pub async fn command_func(ctx: Context<'_>, command: Option<&str>) -> CommandRes
Ok(())
}
);
};
}

if command_obj.owners_only && !framework_options.owners.contains(&ctx.author().id) {
ctx.say("This command is only available to the bot owner!")
Expand Down Expand Up @@ -203,7 +203,7 @@ pub async fn command_func(ctx: Context<'_>, command: Option<&str>) -> CommandRes
let description = p.description.as_deref().unwrap_or("no description");
writeln!(msg, "`{name}`: {description}").unwrap();
});
};
}

msg
}
Expand Down
4 changes: 2 additions & 2 deletions tts_commands/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ You may have:
pub async fn command_check(ctx: Context<'_>) -> Result<bool> {
if ctx.author().bot() {
return Ok(false);
};
}

let data = ctx.data();
let user_row = data.userinfo_db.get(ctx.author().id.into()).await?;
Expand Down Expand Up @@ -176,7 +176,7 @@ pub async fn command_check(ctx: Context<'_>) -> Result<bool> {

if member_roles.contains(&required_role) || ctx.author_permissions()?.administrator() {
return Ok(true);
};
}

let msg = aformat!(
"You do not have the required role to use this bot, ask a server administrator for {}.",
Expand Down
8 changes: 4 additions & 4 deletions tts_commands/src/main_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub async fn join(ctx: Context<'_>) -> CommandResult {
if author_vc == bot_channel_id {
ctx.say("I am already in your voice channel!").await?;
return Ok(());
};
}

let msg = aformat!("I am already in <#{bot_channel_id}>!");
ctx.say(msg.as_str()).await?;
Expand All @@ -174,7 +174,7 @@ pub async fn join(ctx: Context<'_>) -> CommandResult {
data.last_to_xsaid_tracker.remove(&guild_id);
data.songbird.remove(guild_id).await?;
}
};
}

let display_name = {
let join_vc_lock = JoinVCToken::acquire(&data, guild_id);
Expand All @@ -190,7 +190,7 @@ pub async fn join(ctx: Context<'_>) -> CommandResult {
} else {
Err(err.into())
};
};
}

match ctx {
Context::Application(poise::ApplicationContext { interaction, .. }) => {
Expand Down Expand Up @@ -312,7 +312,7 @@ pub async fn clear(ctx: Context<'_>) -> CommandResult {
}
} else {
ctx.say("**Error**: I am not in a voice channel!").await?;
};
}

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions tts_commands/src/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ pub async fn botstats(ctx: Context<'_>) -> CommandResult {
let ram_usage = {
let mut system_info = data.system_info.lock();
system_info.refresh_specifics(
sysinfo::RefreshKind::new()
.with_processes(sysinfo::ProcessRefreshKind::new().with_memory()),
sysinfo::RefreshKind::nothing()
.with_processes(sysinfo::ProcessRefreshKind::nothing().with_memory()),
);

let pid = sysinfo::get_current_pid().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion tts_commands/src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ fn process_cache_info(
is_collection: false,
value: format!("Size: `{size}b`"),
});
};
}
}

fields.sort_by_key(|field| field.size);
Expand Down
111 changes: 47 additions & 64 deletions tts_commands/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,23 +291,20 @@ enum Target {
User,
}

async fn change_mode<'a, CacheKey, RowT>(
ctx: &'a Context<'a>,
general_db: &'a database::Handler<CacheKey, RowT>,
identifier: CacheKey,
async fn can_change_mode(
ctx: &Context<'_>,
mode: Option<TTSMode>,
target: Target,
guild_is_premium: bool,
) -> Result<Option<Cow<'a, str>>, Error>
where
CacheKey: database::CacheKeyTrait + Default + Send + Sync + Copy,
RowT: for<'r> sqlx::FromRow<'r, sqlx::postgres::PgRow> + Compact + Send + Sync + Unpin,
{
) -> Result<bool> {
let data = ctx.data();
if let Some(mode) = mode
&& mode.is_premium()
&& !guild_is_premium
{
let Some(mode) = mode else { return Ok(true) };

if data.config.gtts_disabled.load(Ordering::Relaxed) && mode == TTSMode::gTTS {
ctx.send_error(GTTS_DISABLED_ERROR).await?;
return Ok(false);
}

if mode.is_premium() && !guild_is_premium {
ctx.send(poise::CreateReply::default().embed(CreateEmbed::default()
.title("TTS Bot Premium")
.colour(PREMIUM_NEUTRAL_COLOUR)
Expand All @@ -319,25 +316,10 @@ where
The `{mode}` TTS Mode is only for TTS Bot Premium subscribers, please check out the `/premium` command!
").as_str())
)).await?;
Ok(None)
} else {
let key = if guild_is_premium {
"premium_voice_mode"
} else {
"voice_mode"
};

general_db.set_one(identifier, key, &mode).await?;
Ok(Some(match mode {
Some(mode) => Cow::Owned(match target {
Target::Guild => format!("Changed the server TTS Mode to: {mode}"),
Target::User => format!("Changed your TTS Mode to: {mode}"),
}),
None => Cow::Borrowed(match target {
Target::Guild => "Reset the server mode",
Target::User => "Reset your mode",
}),
}))
Ok(false)
} else {
Ok(true)
}
}

Expand Down Expand Up @@ -398,7 +380,7 @@ fn format_languages<'a>(mut iter: impl Iterator<Item = &'a FixedString<u8>>) ->
buf.push_str(elt);
buf.push('`');
}
};
}

buf
}
Expand Down Expand Up @@ -722,30 +704,28 @@ async fn required_prefix(
)]
pub async fn server_mode(
ctx: Context<'_>,
#[description = "The TTS Mode to change to"] mode: TTSModeChoice,
#[description = "The TTS Mode to change to"] mode: Option<TTSModeChoice>,
) -> CommandResult {
let data = ctx.data();
let guild_id = ctx.guild_id().unwrap();

let mode = TTSMode::from(mode);
if data.config.gtts_disabled.load(Ordering::Relaxed) && mode == TTSMode::gTTS {
ctx.send_error(GTTS_DISABLED_ERROR).await?;
let mode = mode.map(TTSMode::from);
let guild_is_premium = data.is_premium_simple(ctx.http(), guild_id).await?;
if !can_change_mode(&ctx, mode, guild_is_premium).await? {
return Ok(());
}

let to_send = change_mode(
&ctx,
&data.guilds_db,
guild_id.into(),
Some(mode),
Target::Guild,
false,
)
.await?;
data.guilds_db
.set_one(guild_id.into(), "voice_mode", mode)
.await?;

if let Some(to_send) = to_send {
ctx.say(to_send).await?;
let response = if let Some(mode) = mode {
&aformat!("Set your server's voice mode to: {mode}")
} else {
"Reset your server's voice mode"
};

ctx.say(response).await?;
Ok(())
}

Expand Down Expand Up @@ -822,7 +802,7 @@ pub async fn translation_lang(
let mut to_say = format!("The target translation language is now: `{target_lang}`");
if !data.guilds_db.get(guild_id).await?.to_translate() {
to_say.push_str("\nYou may want to enable translation with `/set translation on`");
};
}

Cow::Owned(to_say)
} else {
Expand Down Expand Up @@ -1060,32 +1040,35 @@ pub async fn mode(
>,
) -> CommandResult {
let data = ctx.data();
let author_id = ctx.author().id.into();
let guild_id = ctx.guild_id().unwrap();

let mode = mode.map(TTSMode::from);
if data.config.gtts_disabled.load(Ordering::Relaxed) && mode == Some(TTSMode::gTTS) {
ctx.send_error(GTTS_DISABLED_ERROR).await?;
let guild_is_premium = data.is_premium_simple(ctx.http(), guild_id).await?;
if !can_change_mode(&ctx, mode, guild_is_premium).await? {
return Ok(());
}

let to_send = change_mode(
&ctx,
&data.userinfo_db,
author_id,
mode,
Target::User,
data.is_premium_simple(ctx.http(), guild_id).await?,
)
.await?;
let key = if guild_is_premium {
"premium_voice_mode"
} else {
"voice_mode"
};

data.userinfo_db
.set_one(ctx.author().id.into(), key, mode)
.await?;

if let Some(to_send) = to_send {
ctx.say(to_send).await?;
let response = if let Some(mode) = mode {
&aformat!("Set your voice mode to: {mode}")
} else {
"Reset your voice mode"
};

ctx.say(response).await?;
Ok(())
}

/// Changes the voice your messages are read in, full list in `-voices`
/// Changes the voice your messages are read in, full list in `/voices`
#[poise::command(
guild_only,
category = "Settings",
Expand Down
7 changes: 4 additions & 3 deletions tts_commands/src/settings/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use anyhow::bail;

use poise::serenity_prelude as serenity;
use serenity::{
builder::*, small_fixed_array::FixedString, ComponentInteractionDataKind, Permissions,
builder::*, small_fixed_array::FixedString, CollectComponentInteractions,
ComponentInteractionDataKind, Permissions,
};

use tts_core::{
Expand Down Expand Up @@ -94,7 +95,7 @@ async fn show_channel_select_menu(
} else if text_channels.len() >= (25 * 5) {
ctx.say("**Error**: This server has too many text channels to show in a menu! Please run `/setup #channel`").await?;
return Ok(None);
};
}

text_channels.sort_by(|c1, c2| Ord::cmp(&c1.position, &c2.position));

Expand All @@ -106,7 +107,7 @@ async fn show_channel_select_menu(
let reply_message = reply.message().await?;
let interaction = reply_message
.id
.await_component_interaction(ctx.serenity_context().shard.clone())
.collect_component_interactions(ctx.serenity_context().shard.clone())
.timeout(std::time::Duration::from_secs(60 * 5))
.author_id(ctx.author().id)
.await;
Expand Down
4 changes: 2 additions & 2 deletions tts_commands/src/settings/voice_paginator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;

use poise::serenity_prelude as serenity;
use serenity::{builder::*, small_fixed_array::FixedString};
use serenity::{builder::*, small_fixed_array::FixedString, CollectComponentInteractions};

use tts_core::structs::{Context, TTSMode};

Expand Down Expand Up @@ -96,7 +96,7 @@ impl<'a> MenuPaginator<'a> {

loop {
let builder = message_id
.await_component_interaction(serenity_context.shard.clone())
.collect_component_interactions(serenity_context.shard.clone())
.timeout(std::time::Duration::from_secs(60 * 5))
.author_id(self.ctx.author().id);

Expand Down
4 changes: 2 additions & 2 deletions tts_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ edition = "2021"
rust-version = "1.83"

[dependencies]
rand = "0.8"
rand = "0.9"
sha2 = "0.10"
linkify = "0.10"
bitflags = "2.4.1"
strum_macros = "0.26"
strum_macros = "0.27"
chrono = { version = "0.4.38", default-features = false }
bool_to_bitflags = { version = "0.1", features = ["typesize"] }

Expand Down
Loading

0 comments on commit 7ebedab

Please sign in to comment.