fix: change some replies to ephemeral, updated rand, cleanup

This commit is contained in:
Ladislav Hano 2025-01-30 19:21:45 +01:00
parent 276e9f5d75
commit 9e66b56721
11 changed files with 22 additions and 33 deletions

View file

@ -2,7 +2,7 @@ use poise;
use serenity::all::User; use serenity::all::User;
use super::interaction::send_with_embed; use super::interaction::send_with_embed;
use crate::types::{Error, Context}; use crate::types::{Context, ContextExt, Error};
#[poise::command( #[poise::command(
slash_command, slash_command,
@ -16,6 +16,6 @@ pub async fn headpat(ctx: Context<'_>,
let title = "HEADPATS!"; let title = "HEADPATS!";
let desc = format!("{} headpats {}", ctx.author(), user); let desc = format!("{} headpats {}", ctx.author(), user);
send_with_embed(ctx, "headpat", &title, &desc).await?; send_with_embed(ctx, "headpat", &title, &desc).await?;
ctx.reply("Done!").await?; ctx.reply_ephemeral("Done!").await?;
Ok(()) Ok(())
} }

View file

@ -2,7 +2,7 @@ use poise;
use serenity::all::User; use serenity::all::User;
use super::interaction::send_with_embed; use super::interaction::send_with_embed;
use crate::types::{Error, Context}; use crate::types::{Context, ContextExt, Error};
#[poise::command( #[poise::command(
slash_command, slash_command,
@ -16,6 +16,6 @@ pub async fn hug(ctx: Context<'_>,
let title = "HUGS!"; let title = "HUGS!";
let desc = format!("{} hugs {}", ctx.author(), user); let desc = format!("{} hugs {}", ctx.author(), user);
send_with_embed(ctx, "hug", &title, &desc).await?; send_with_embed(ctx, "hug", &title, &desc).await?;
ctx.reply("Done!").await?; ctx.reply_ephemeral("Done!").await?;
Ok(()) Ok(())
} }

View file

@ -2,7 +2,7 @@ use anyhow::anyhow;
use serenity::all::{Colour, CreateEmbed, CreateMessage}; use serenity::all::{Colour, CreateEmbed, CreateMessage};
use tenorv2::tenor_builder::Tenor; 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 /// Sends embed with random tenor gif from searched query
/// title and desc are used in the embed /// 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")); return Err(anyhow!("Guild id not available in context"));
} }
utilities::get_system_channel( ctx.channel_id()
ctx.guild_id().unwrap(), ctx.http()
).await?
.send_message( .send_message(
ctx.http(), ctx.http(),
CreateMessage::new().add_embed(embed) CreateMessage::new().add_embed(embed)

View file

@ -5,8 +5,3 @@ pub mod player_common;
pub mod radio; pub mod radio;
pub mod general_player; pub mod general_player;
pub mod voice_types; pub mod voice_types;
// ! not working
// pub mod yt;
// TODO implement
// pub mod spotify;

View file

@ -37,7 +37,7 @@ pub async fn disconnect(
let has_handler = manager.get(guild_id).is_some(); let has_handler = manager.get(guild_id).is_some();
if ! has_handler { 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(()) return Ok(())
} }

View file

@ -5,13 +5,13 @@ use radiobrowser::{ApiStation, StationSearchBuilder};
use regex::Regex; use regex::Regex;
use serenity::all::{CreateActionRow, CreateButton, CreateEmbed, CreateEmbedFooter, CreateInteractionResponse, CreateInteractionResponseMessage}; 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( pub async fn paginate_search_stations(
ctx: &Context<'_>, ctx: &Context<'_>,
search_builder: &StationSearchBuilder, search_builder: &StationSearchBuilder,
limit: NumberOfEntries limit: NumberOfEntries
) -> Result<(), serenity::Error> { ) -> Result<(), Error> {
// Define some unique identifiers for the navigation buttons // Define some unique identifiers for the navigation buttons
let ctx_id = ctx.id(); let ctx_id = ctx.id();
let prev_button_id = format!("{}prev", 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 search_builder = search_builder;
let Ok(stations) = search_builder.clone().send().await else { 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(()) 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 { 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(()) return Ok(())
}; };
@ -75,7 +75,7 @@ pub async fn paginate_search_stations(
page = 0; page = 0;
let Ok(new_stations) = search_builder.clone().offset(offset.to_string()).send().await else { 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(()) return Ok(())
}; };
stations = new_stations; stations = new_stations;

View file

@ -1,4 +1,5 @@
use rand::random; use rand::random;
use rand::seq::IndexedRandom;
use serenity::http::CacheHttp; use serenity::http::CacheHttp;
use serenity::{client::Context, http::Http}; use serenity::{client::Context, http::Http};
use serenity::model::channel::Message; use serenity::model::channel::Message;
@ -80,8 +81,8 @@ async fn response(http: Arc<Http>, msg: Message) -> bool {
"Hm?" "Hm?"
]; ];
let num = random::<usize>() % RESPONSES.len(); let response = RESPONSES.choose(&mut rand::rng()).unwrap_or(&"?").to_string();
match msg.reply(http.clone(), RESPONSES[num]).await { match msg.reply(http.clone(), response).await {
Ok(_) => { return true } Ok(_) => { return true }
Err(e) => { Err(e) => {
let _ = send_error(http, e.to_string()).await; let _ = send_error(http, e.to_string()).await;
@ -93,9 +94,8 @@ async fn response(http: Arc<Http>, msg: Message) -> bool {
async fn henlo(http: Arc<Http>, msg: Message) -> bool { async fn henlo(http: Arc<Http>, msg: Message) -> bool {
const EMOJIS: [&str; 7] = ["🥰", "🐄", "🐮", "❤️", "👋", "🤠", "😊"]; const EMOJIS: [&str; 7] = ["🥰", "🐄", "🐮", "❤️", "👋", "🤠", "😊"];
let num = random::<usize>() % EMOJIS.len(); let emoji = EMOJIS.choose(&mut rand::rng()).unwrap_or(&"🐮");
let response = format!("Henlooo {} {}", msg.author.name, EMOJIS[num]); let response = format!("Henlooo {} {}", msg.author.name, emoji);
match msg.reply(http.clone(), response).await { match msg.reply(http.clone(), response).await {
Ok(_) => { return true } Ok(_) => { return true }

View file

@ -58,7 +58,7 @@ async fn celebrate_birthday(guild_id: GuildId, user_id: UserId, nick: &str, http
.locale("sk".to_string()) .locale("sk".to_string())
.search("vsetko najlepsie").await?; .search("vsetko najlepsie").await?;
let index = rand::random::<usize>() % LIMIT as usize; let index = (rand::random::<u8>() % LIMIT) as usize;
let gif_url = match tenor::get_gif_url(MediaFilter::gif, tenor_response) { let gif_url = match tenor::get_gif_url(MediaFilter::gif, tenor_response) {
Ok(urls) => Some(urls), Ok(urls) => Some(urls),
Err(e) => { Err(e) => {

View file

@ -26,6 +26,7 @@ pub async fn hello(http: Arc<Http>) -> anyhow::Result<()> {
use serenity::all::ChannelId; use serenity::all::ChannelId;
use anyhow::Context; use anyhow::Context;
use std::env; use std::env;
use rand::seq::IndexedRandom;
let messages = [ let messages = [
"AAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAA",
@ -38,11 +39,11 @@ pub async fn hello(http: Arc<Http>) -> anyhow::Result<()> {
"Whom'st have summoned the ancient one?", "Whom'st have summoned the ancient one?",
]; ];
let num = rand::random::<usize>() % 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_id: String = env::var("DEBUG_CHANNEL_ID").context("DEBUG_CHANNEL_ID not found in env file")?;
let channel = ChannelId::new(channel_id.parse::<u64>().unwrap()); let channel = ChannelId::new(channel_id.parse::<u64>().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); print!("Error sending message: {:?}", why);
}; };

View file

@ -1,7 +1,7 @@
use tenorv2::{tenor, tenor_types::{MediaFilter, TenorError}, JsonValue}; use tenorv2::{tenor, tenor_types::{MediaFilter, TenorError}, JsonValue};
pub async fn get_random_tenor_gif(tenor_response: JsonValue, limit: u8) -> Result<String, TenorError> { pub async fn get_random_tenor_gif(tenor_response: JsonValue, limit: u8) -> Result<String, TenorError> {
let index = rand::random::<usize>() % limit as usize; let index = (rand::random::<u8>() % limit) as usize;
match tenor::get_gif_url(MediaFilter::gif, tenor_response) { match tenor::get_gif_url(MediaFilter::gif, tenor_response) {
Ok(urls) => Ok(urls[index].clone()), Ok(urls) => Ok(urls[index].clone()),
Err(e) => Err(e) Err(e) => Err(e)

View file

@ -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 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<ChannelId> { pub async fn get_system_channel(guild_id: GuildId, http: &Http) -> anyhow::Result<ChannelId> {
use anyhow::Context; use anyhow::Context;