From 9e66b567216e610e062a6d1b9e7de627e6d8c9a7 Mon Sep 17 00:00:00 2001 From: Ladislav Hano Date: Thu, 30 Jan 2025 19:21:45 +0100 Subject: [PATCH] fix: change some replies to ephemeral, updated rand, cleanup --- src/commands/user_interactions/headpat.rs | 4 ++-- src/commands/user_interactions/hug.rs | 4 ++-- src/commands/user_interactions/interaction.rs | 6 ++---- src/commands/voice.rs | 5 ----- src/commands/voice/player_common.rs | 2 +- src/commands/voice/radio/radio_utils.rs | 10 +++++----- src/message_handler.rs | 10 +++++----- src/other/notice.rs | 2 +- src/utils/debug.rs | 5 +++-- src/utils/gifs.rs | 2 +- src/utils/utilities.rs | 5 ----- 11 files changed, 22 insertions(+), 33 deletions(-) diff --git a/src/commands/user_interactions/headpat.rs b/src/commands/user_interactions/headpat.rs index dd67445..70e0601 100644 --- a/src/commands/user_interactions/headpat.rs +++ b/src/commands/user_interactions/headpat.rs @@ -2,7 +2,7 @@ use poise; use serenity::all::User; use super::interaction::send_with_embed; -use crate::types::{Error, Context}; +use crate::types::{Context, ContextExt, Error}; #[poise::command( slash_command, @@ -16,6 +16,6 @@ pub async fn headpat(ctx: Context<'_>, let title = "HEADPATS!"; let desc = format!("{} headpats {}", ctx.author(), user); send_with_embed(ctx, "headpat", &title, &desc).await?; - ctx.reply("Done!").await?; + ctx.reply_ephemeral("Done!").await?; Ok(()) } \ No newline at end of file diff --git a/src/commands/user_interactions/hug.rs b/src/commands/user_interactions/hug.rs index 3f7e63a..92b56a2 100644 --- a/src/commands/user_interactions/hug.rs +++ b/src/commands/user_interactions/hug.rs @@ -2,7 +2,7 @@ use poise; use serenity::all::User; use super::interaction::send_with_embed; -use crate::types::{Error, Context}; +use crate::types::{Context, ContextExt, Error}; #[poise::command( slash_command, @@ -16,6 +16,6 @@ pub async fn hug(ctx: Context<'_>, let title = "HUGS!"; let desc = format!("{} hugs {}", ctx.author(), user); send_with_embed(ctx, "hug", &title, &desc).await?; - ctx.reply("Done!").await?; + ctx.reply_ephemeral("Done!").await?; Ok(()) } \ No newline at end of file diff --git a/src/commands/user_interactions/interaction.rs b/src/commands/user_interactions/interaction.rs index d53002e..8cb1461 100644 --- a/src/commands/user_interactions/interaction.rs +++ b/src/commands/user_interactions/interaction.rs @@ -2,7 +2,7 @@ use anyhow::anyhow; use serenity::all::{Colour, CreateEmbed, CreateMessage}; use tenorv2::tenor_builder::Tenor; -use crate::{types::Context, utils::{gifs::get_random_tenor_gif, utilities}}; +use crate::{types::Context, utils::gifs::get_random_tenor_gif}; /// Sends embed with random tenor gif from searched query /// title and desc are used in the embed @@ -24,9 +24,7 @@ pub(super) async fn send_with_embed(ctx: Context<'_>, query: &str, title: &str, return Err(anyhow!("Guild id not available in context")); } - utilities::get_system_channel( - ctx.guild_id().unwrap(), ctx.http() - ).await? + ctx.channel_id() .send_message( ctx.http(), CreateMessage::new().add_embed(embed) diff --git a/src/commands/voice.rs b/src/commands/voice.rs index a86fe29..f1a91e9 100644 --- a/src/commands/voice.rs +++ b/src/commands/voice.rs @@ -5,8 +5,3 @@ pub mod player_common; pub mod radio; pub mod general_player; pub mod voice_types; - -// ! not working -// pub mod yt; -// TODO implement -// pub mod spotify; diff --git a/src/commands/voice/player_common.rs b/src/commands/voice/player_common.rs index 90babb8..1a6d5dd 100644 --- a/src/commands/voice/player_common.rs +++ b/src/commands/voice/player_common.rs @@ -37,7 +37,7 @@ pub async fn disconnect( let has_handler = manager.get(guild_id).is_some(); if ! has_handler { - ctx.reply("I am not connected to a channel!").await?; + ctx.reply_ephemeral("I am not connected to a channel!").await?; return Ok(()) } diff --git a/src/commands/voice/radio/radio_utils.rs b/src/commands/voice/radio/radio_utils.rs index 84d4d8f..08e5a79 100644 --- a/src/commands/voice/radio/radio_utils.rs +++ b/src/commands/voice/radio/radio_utils.rs @@ -5,13 +5,13 @@ use radiobrowser::{ApiStation, StationSearchBuilder}; use regex::Regex; use serenity::all::{CreateActionRow, CreateButton, CreateEmbed, CreateEmbedFooter, CreateInteractionResponse, CreateInteractionResponseMessage}; -use crate::{commands::voice_types::NumberOfEntries, types::Context}; +use crate::{commands::voice_types::NumberOfEntries, types::{Context, ContextExt, Error}}; pub async fn paginate_search_stations( ctx: &Context<'_>, search_builder: &StationSearchBuilder, limit: NumberOfEntries -) -> Result<(), serenity::Error> { +) -> Result<(), Error> { // Define some unique identifiers for the navigation buttons let ctx_id = ctx.id(); let prev_button_id = format!("{}prev", ctx_id); @@ -20,7 +20,7 @@ pub async fn paginate_search_stations( let search_builder = search_builder; let Ok(stations) = search_builder.clone().send().await else { - ctx.reply("Something went wrong, try searching again").await?; + ctx.reply_ephemeral("Something went wrong, try searching again").await?; return Ok(()) }; @@ -66,7 +66,7 @@ pub async fn paginate_search_stations( } let Ok(mut stations) = search_builder.clone().offset(offset.to_string()).send().await else { - ctx.reply("Something went wrong, try searching again").await?; + ctx.reply_ephemeral("Something went wrong, try searching again").await?; return Ok(()) }; @@ -75,7 +75,7 @@ pub async fn paginate_search_stations( page = 0; let Ok(new_stations) = search_builder.clone().offset(offset.to_string()).send().await else { - ctx.reply("Something went wrong, try searching again").await?; + ctx.reply_ephemeral("Something went wrong, try searching again").await?; return Ok(()) }; stations = new_stations; diff --git a/src/message_handler.rs b/src/message_handler.rs index 8033036..c14d392 100644 --- a/src/message_handler.rs +++ b/src/message_handler.rs @@ -1,4 +1,5 @@ use rand::random; +use rand::seq::IndexedRandom; use serenity::http::CacheHttp; use serenity::{client::Context, http::Http}; use serenity::model::channel::Message; @@ -80,8 +81,8 @@ async fn response(http: Arc, msg: Message) -> bool { "Hm?" ]; - let num = random::() % RESPONSES.len(); - match msg.reply(http.clone(), RESPONSES[num]).await { + let response = RESPONSES.choose(&mut rand::rng()).unwrap_or(&"?").to_string(); + match msg.reply(http.clone(), response).await { Ok(_) => { return true } Err(e) => { let _ = send_error(http, e.to_string()).await; @@ -93,9 +94,8 @@ async fn response(http: Arc, msg: Message) -> bool { async fn henlo(http: Arc, msg: Message) -> bool { const EMOJIS: [&str; 7] = ["🥰", "🐄", "🐮", "❤️", "👋", "🤠", "😊"]; - let num = random::() % EMOJIS.len(); - let response = format!("Henlooo {} {}", msg.author.name, EMOJIS[num]); - + let emoji = EMOJIS.choose(&mut rand::rng()).unwrap_or(&"🐮"); + let response = format!("Henlooo {} {}", msg.author.name, emoji); match msg.reply(http.clone(), response).await { Ok(_) => { return true } diff --git a/src/other/notice.rs b/src/other/notice.rs index 9d75231..7f03855 100644 --- a/src/other/notice.rs +++ b/src/other/notice.rs @@ -58,7 +58,7 @@ async fn celebrate_birthday(guild_id: GuildId, user_id: UserId, nick: &str, http .locale("sk".to_string()) .search("vsetko najlepsie").await?; - let index = rand::random::() % LIMIT as usize; + let index = (rand::random::() % LIMIT) as usize; let gif_url = match tenor::get_gif_url(MediaFilter::gif, tenor_response) { Ok(urls) => Some(urls), Err(e) => { diff --git a/src/utils/debug.rs b/src/utils/debug.rs index 1df4c69..e8c1d6b 100644 --- a/src/utils/debug.rs +++ b/src/utils/debug.rs @@ -26,6 +26,7 @@ pub async fn hello(http: Arc) -> anyhow::Result<()> { use serenity::all::ChannelId; use anyhow::Context; use std::env; + use rand::seq::IndexedRandom; let messages = [ "AAAAAAAAAAAAAAAAAAAA", @@ -38,11 +39,11 @@ pub async fn hello(http: Arc) -> anyhow::Result<()> { "Whom'st have summoned the ancient one?", ]; - let num = rand::random::() % messages.len(); + let message = messages.choose(&mut rand::rng()).unwrap_or(&"Chello").to_string(); let channel_id: String = env::var("DEBUG_CHANNEL_ID").context("DEBUG_CHANNEL_ID not found in env file")?; let channel = ChannelId::new(channel_id.parse::().unwrap()); - if let Err(why) = channel.say(http, messages[num]).await { + if let Err(why) = channel.say(http, message).await { print!("Error sending message: {:?}", why); }; diff --git a/src/utils/gifs.rs b/src/utils/gifs.rs index f128ea9..f7c76b0 100644 --- a/src/utils/gifs.rs +++ b/src/utils/gifs.rs @@ -1,7 +1,7 @@ use tenorv2::{tenor, tenor_types::{MediaFilter, TenorError}, JsonValue}; pub async fn get_random_tenor_gif(tenor_response: JsonValue, limit: u8) -> Result { - let index = rand::random::() % limit as usize; + let index = (rand::random::() % limit) as usize; match tenor::get_gif_url(MediaFilter::gif, tenor_response) { Ok(urls) => Ok(urls[index].clone()), Err(e) => Err(e) diff --git a/src/utils/utilities.rs b/src/utils/utilities.rs index f24e427..1af175f 100644 --- a/src/utils/utilities.rs +++ b/src/utils/utilities.rs @@ -2,11 +2,6 @@ use std::{fs, hash::{DefaultHasher, Hash, Hasher}, io, path::Path, sync::Arc, ve use serenity::{all::{ChannelId, ChannelType, CreateMessage, GuildId, GuildRef, Message}, http::Http}; -use poise::CreateReply; -use serenity::async_trait; - -use crate::types::Context; - pub async fn get_system_channel(guild_id: GuildId, http: &Http) -> anyhow::Result { use anyhow::Context;