Compare commits

..

2 Commits

Author SHA1 Message Date
jude
48b50f783d Add more counters 2023-10-22 09:55:36 +01:00
jude
605bc37db6 Add response to /random 2023-10-22 00:07:04 +01:00
6 changed files with 27 additions and 8 deletions

2
Cargo.lock generated
View File

@ -2215,7 +2215,7 @@ dependencies = [
[[package]] [[package]]
name = "soundfx-rs" name = "soundfx-rs"
version = "1.5.11" version = "1.5.12"
dependencies = [ dependencies = [
"axum", "axum",
"dashmap", "dashmap",

View File

@ -2,7 +2,7 @@
name = "soundfx-rs" name = "soundfx-rs"
description = "Discord bot for custom sound effects and soundboards" description = "Discord bot for custom sound effects and soundboards"
license = "AGPL-3.0-only" license = "AGPL-3.0-only"
version = "1.5.11" version = "1.5.12"
authors = ["jellywx <judesouthworth@pm.me>"] authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018" edition = "2018"

View File

@ -1,6 +1,8 @@
use poise::serenity_prelude::{Attachment, GuildId, RoleId}; use poise::serenity_prelude::{Attachment, GuildId, RoleId};
use tokio::fs::File; use tokio::fs::File;
#[cfg(feature = "metrics")]
use crate::metrics::{DELETE_COUNTER, UPLOAD_COUNTER};
use crate::{ use crate::{
cmds::autocomplete_sound, cmds::autocomplete_sound,
consts::{MAX_SOUNDS, PATREON_GUILD, PATREON_ROLE}, consts::{MAX_SOUNDS, PATREON_GUILD, PATREON_ROLE},
@ -21,6 +23,9 @@ pub async fn upload_new_sound(
#[description = "Name to upload sound to"] name: String, #[description = "Name to upload sound to"] name: String,
#[description = "Sound file (max. 2MB)"] file: Attachment, #[description = "Sound file (max. 2MB)"] file: Attachment,
) -> Result<(), Error> { ) -> Result<(), Error> {
#[cfg(feature = "metrics")]
UPLOAD_COUNTER.inc();
ctx.defer().await?; ctx.defer().await?;
fn is_numeric(s: &String) -> bool { fn is_numeric(s: &String) -> bool {
@ -110,6 +115,9 @@ pub async fn delete_sound(
#[autocomplete = "autocomplete_sound"] #[autocomplete = "autocomplete_sound"]
name: String, name: String,
) -> Result<(), Error> { ) -> Result<(), Error> {
#[cfg(feature = "metrics")]
DELETE_COUNTER.inc();
let pool = ctx.data().database.clone(); let pool = ctx.data().database.clone();
let uid = ctx.author().id.0; let uid = ctx.author().id.0;

View File

@ -25,13 +25,13 @@ pub async fn play(
#[channel_types("Voice")] #[channel_types("Voice")]
channel: Option<GuildChannel>, channel: Option<GuildChannel>,
) -> Result<(), Error> { ) -> Result<(), Error> {
#[cfg(feature = "metrics")]
PLAY_COUNTER.inc();
ctx.defer().await?; ctx.defer().await?;
let guild = ctx.guild().unwrap(); let guild = ctx.guild().unwrap();
#[cfg(feature = "metrics")]
PLAY_COUNTER.inc();
ctx.say( ctx.say(
play_from_query( play_from_query(
&ctx.serenity_context(), &ctx.serenity_context(),
@ -78,9 +78,15 @@ pub async fn play_random(
join_channel(ctx.serenity_context(), guild.clone(), channel).await; join_channel(ctx.serenity_context(), guild.clone(), channel).await;
let sounds = ctx.data().guild_sounds(guild.id, None).await?; let sounds = ctx.data().guild_sounds(guild.id, None).await?;
if sounds.len() == 0 {
ctx.say("No sounds in this server!").await?;
return Ok(());
}
let ts = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); let ts = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
println!("{}", ts.subsec_micros());
// This is far cheaper and easier than using an RNG. No reason to use a full RNG here // This is far cheaper and easier than using an RNG. No reason to use a full RNG here
// anyway. // anyway.
match sounds.get(ts.subsec_micros() as usize % sounds.len()) { match sounds.get(ts.subsec_micros() as usize % sounds.len()) {
@ -97,6 +103,9 @@ pub async fn play_random(
) )
.await .await
.unwrap(); .unwrap();
ctx.say(format!("Playing {} (ID {})", sound.name, sound.id))
.await?;
} }
None => { None => {

View File

@ -30,7 +30,6 @@ type Database = MySql;
pub struct Data { pub struct Data {
database: Pool<Database>, database: Pool<Database>,
http: reqwest::Client,
guild_data_cache: DashMap<GuildId, Arc<RwLock<GuildData>>>, guild_data_cache: DashMap<GuildId, Arc<RwLock<GuildData>>>,
join_sound_cache: DashMap<UserId, DashMap<Option<GuildId>, Option<u32>>>, join_sound_cache: DashMap<UserId, DashMap<Option<GuildId>, Option<u32>>>,
} }
@ -159,7 +158,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.unwrap(); .unwrap();
Ok(Data { Ok(Data {
http: reqwest::Client::new(),
database, database,
guild_data_cache: Default::default(), guild_data_cache: Default::default(),
join_sound_cache: Default::default(), join_sound_cache: Default::default(),

View File

@ -8,7 +8,11 @@ use prometheus::{register_int_counter, IntCounter, Registry};
lazy_static! { lazy_static! {
static ref REGISTRY: Registry = Registry::new(); static ref REGISTRY: Registry = Registry::new();
pub static ref PLAY_COUNTER: IntCounter = pub static ref PLAY_COUNTER: IntCounter =
register_int_counter!("play", "Number of calls to /play").unwrap(); register_int_counter!("play_cmd", "Number of calls to /play").unwrap();
pub static ref UPLOAD_COUNTER: IntCounter =
register_int_counter!("upload_cmd", "Number of calls to /upload").unwrap();
pub static ref DELETE_COUNTER: IntCounter =
register_int_counter!("delete_cmd", "Number of calls to /delete").unwrap();
} }
pub fn init_metrics() { pub fn init_metrics() {