Skip to content

Commit

Permalink
Update for small-fixed-array v2
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Jan 16, 2024
1 parent a313686 commit cb5cafa
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 58 deletions.
65 changes: 32 additions & 33 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const_format = "0.2"
strum_macros = "0.25"
arrayvec = "0.7.4"
bitflags = "2.4.1"
paste = "1.0.14"
typesize = { version = "0.1.2", features = ["arrayvec"] }
bool_to_bitflags = { version = "0.1", features = ["typesize"] }

Expand Down
13 changes: 9 additions & 4 deletions src/commands/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@

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

use self::serenity::{builder::*, ChannelId, ComponentInteractionDataKind, Mentionable};
use anyhow::bail;
use database::Compact;

use self::serenity::{
builder::*, small_fixed_array::FixedString, ChannelId, ComponentInteractionDataKind,
Mentionable,
};
use poise::serenity_prelude as serenity;

use crate::{
constants::{OPTION_SEPERATORS, PREMIUM_NEUTRAL_COLOUR},
database,
database::{self, Compact},
funcs::{confirm_dialog, random_footer},
opt_ext::OptionGettext,
require, require_guild,
Expand Down Expand Up @@ -265,7 +268,9 @@ impl<'a> MenuPaginator<'a> {
.map(|emoji| {
CreateButton::new(emoji)
.style(serenity::ButtonStyle::Primary)
.emoji(serenity::ReactionType::Unicode(String::from(emoji).into()))
.emoji(serenity::ReactionType::Unicode(
FixedString::from_str_trunc(emoji),
))
.disabled(
disabled
|| (["⏮️", "◀"].contains(&emoji) && self.index == 0)
Expand Down
9 changes: 5 additions & 4 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use anyhow::{Error, Result};
use sha2::Digest;
use tracing::error;

use poise::serenity_prelude as serenity;

use self::serenity::{
CreateActionRow, CreateButton, CreateInteractionResponse, FullEvent as Event,
small_fixed_array::TruncatingInto, CreateActionRow, CreateButton, CreateInteractionResponse,
FullEvent as Event,
};
use poise::serenity_prelude as serenity;

use crate::{
constants, require,
structs::{Data, FrameworkContext},
Expand Down Expand Up @@ -89,7 +90,7 @@ async fn fetch_update_occurrences(
let mut embed = message.embeds.into_vec().remove(0);

embed.footer.as_mut().try_unwrap()?.text =
format!("This error has occurred {occurrences} times!").into();
format!("This error has occurred {occurrences} times!").trunc_into();

let builder = serenity::EditWebhookMessage::default().embeds(vec![embed.into()]);
error_webhook.edit_message(ctx, message_id, builder).await?;
Expand Down
13 changes: 6 additions & 7 deletions src/events/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,15 @@ async fn process_mention_msg(
message: &serenity::Message,
data: &Data,
) -> Result<()> {
let bot_user = ctx.cache.current_user().id;
if ![
format!("<@{bot_user}>").into(),
format!("<@!{bot_user}>").into(),
]
.contains(&message.content)
{
let Some(bot_mention_regex) = data.regex_cache.bot_mention.get() else {
return Ok(());
};

if !bot_mention_regex.is_match(&message.content) {
return Ok(());
};

let bot_user = ctx.cache.current_user().id;
let guild_id = require!(message.guild_id, Ok(()));
let channel = message.channel(ctx).await?.guild().unwrap();
let permissions = channel.permissions_for_user(ctx, bot_user)?;
Expand Down
21 changes: 13 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use std::{
collections::BTreeMap,
num::NonZeroU16,
str::FromStr,
sync::{atomic::AtomicBool, Arc},
sync::{atomic::AtomicBool, Arc, OnceLock},
};

use anyhow::Ok;
Expand Down Expand Up @@ -259,13 +259,10 @@ async fn _main(start_time: std::time::SystemTime) -> Result<()> {
let analytics = Arc::new(analytics::Handler::new(pool.clone()));
tokio::spawn(analytics.clone().start());

let startup_builder = ExecuteWebhook::default().content("**TTS Bot is starting up**");
let startup_message = webhooks
.logs
.execute(
&http,
true,
ExecuteWebhook::default().content("**TTS Bot is starting up**"),
)
.execute(&http, true, startup_builder)
.await?
.unwrap()
.id;
Expand Down Expand Up @@ -294,6 +291,7 @@ async fn _main(start_time: std::time::SystemTime) -> Result<()> {
],
id_in_brackets: regex::Regex::new(r"\((\d+)\)")?,
emoji: regex::Regex::new(r"<(a?):([^<>]+):\d+>")?,
bot_mention: OnceLock::new(),
};

let data = Data(Arc::new(DataInner {
Expand Down Expand Up @@ -431,8 +429,15 @@ async fn _main(start_time: std::time::SystemTime) -> Result<()> {

let mut client = serenity::Client::builder(&token, intents)
.voice_manager::<songbird::Songbird>(songbird)
.framework(poise::Framework::new(framework_options, |_, _, _| {
Box::pin(async { Ok(data) })
.framework(poise::Framework::new(framework_options, |_, ready, _| {
Box::pin(async {
data.regex_cache
.bot_mention
.set(regex::Regex::new(&format!("<@!{}>", ready.user.id))?)
.unwrap();

Ok(data)
})
}))
.await?;

Expand Down
3 changes: 2 additions & 1 deletion src/structs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
borrow::Cow,
collections::{BTreeMap, HashMap},
sync::Arc,
sync::{Arc, OnceLock},
};

pub use anyhow::{Error, Result};
Expand Down Expand Up @@ -108,6 +108,7 @@ pub struct PatreonInfo {

pub struct RegexCache {
pub replacements: [(regex::Regex, &'static str); 3],
pub bot_mention: OnceLock<regex::Regex>,
pub id_in_brackets: regex::Regex,
pub emoji: regex::Regex,
}
Expand Down

0 comments on commit cb5cafa

Please sign in to comment.