chore: improve checks

This commit is contained in:
Miguel da Mota 2024-03-10 17:46:35 +01:00
parent fc32017a10
commit 29392dc72d
7 changed files with 20 additions and 29 deletions

View file

@ -3,9 +3,7 @@ use serenity::builder::{CreateCommand, CreateEmbed};
use crate::util::embed::Embed; use crate::util::embed::Embed;
pub async fn run(_ctx: &Context, command: &CommandInteraction) -> CreateEmbed { pub async fn run(_ctx: &Context, _command: &CommandInteraction) -> CreateEmbed {
let username = command.user.name.as_str();
Embed::create( Embed::create(
"", "",
"Botendo v7", "Botendo v7",

View file

@ -6,7 +6,7 @@ use serenity::model::application::CommandOptionType;
use crate::util::embed::Embed; use crate::util::embed::Embed;
pub async unsafe fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed { pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
let username = command.user.name.as_str(); let username = command.user.name.as_str();
let options = &command.data.options; let options = &command.data.options;

View file

@ -3,7 +3,7 @@ use crate::util::embed::Embed;
use serenity::all::{CommandInteraction, Context}; use serenity::all::{CommandInteraction, Context};
use serenity::builder::{CreateCommand, CreateEmbed}; use serenity::builder::{CreateCommand, CreateEmbed};
pub async unsafe fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed { pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
let username = command.user.name.as_str(); let username = command.user.name.as_str();
let guild_id = match &command.guild_id { let guild_id = match &command.guild_id {

View file

@ -63,22 +63,20 @@ struct Handler;
#[async_trait] #[async_trait]
impl EventHandler for Handler { impl EventHandler for Handler {
async fn interaction_create(&self, ctx: Context, interaction: Interaction) { async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
unsafe { if let Interaction::Command(command) = interaction {
if let Interaction::Command(command) = interaction { let _ = &command.defer(&ctx.http()).await.expect("Cannot defer");
let _ = &command.defer(&ctx.http()).await.expect("Cannot defer");
let content = Some(match command.data.name.as_str() { let content = Some(match command.data.name.as_str() {
"info" => commands::info::run(&ctx, &command).await, "info" => commands::info::run(&ctx, &command).await,
"play" => commands::play::run(&ctx, &command).await, "play" => commands::play::run(&ctx, &command).await,
"stop" => commands::stop::run(&ctx, &command).await, "stop" => commands::stop::run(&ctx, &command).await,
_ => respond_with_error(&ctx, &command).await, _ => respond_with_error(&ctx, &command).await,
}); });
if let Some(embed) = content { if let Some(embed) = content {
let followup = CreateInteractionResponseFollowup::new().embed(embed); let followup = CreateInteractionResponseFollowup::new().embed(embed);
if let Err(why) = command.create_followup(&ctx.http, followup).await { if let Err(why) = command.create_followup(&ctx.http, followup).await {
println!("Cannot followup to slash command: {why}") println!("Cannot followup to slash command: {why}")
}
} }
} }
} }

View file

@ -224,9 +224,7 @@ pub async fn stop(ctx: &Context, guild_id: &GuildId) -> Result<bool, JoinError>
.expect("Cannot get Songbird") .expect("Cannot get Songbird")
.clone(); .clone();
let has_handler = manager.get(*guild_id).is_some(); if manager.get(*guild_id).is_some() {
if has_handler {
manager.remove(*guild_id).await?; manager.remove(*guild_id).await?;
return Ok(true); // Handler removed return Ok(true); // Handler removed
} }

View file

@ -12,6 +12,7 @@ pub fn request_guild(ctx: &Context, guild_id: &GuildId) -> Guild {
} }
/// Request a guild by id, get it from Discord, not from cache, this is a partial guild /// Request a guild by id, get it from Discord, not from cache, this is a partial guild
#[allow(dead_code)]
pub async fn request_partial_guild(ctx: &Context, guild_id: &GuildId) -> PartialGuild { pub async fn request_partial_guild(ctx: &Context, guild_id: &GuildId) -> PartialGuild {
match ctx.http.get_guild(*guild_id).await { match ctx.http.get_guild(*guild_id).await {
Ok(guild) => guild, Ok(guild) => guild,
@ -32,16 +33,12 @@ pub async fn get_vc_id(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> O
/// Check if the bot is connected to a voice channel /// Check if the bot is connected to a voice channel
pub async fn is_self_connected_to_vc(ctx: &Context, guild_id: &GuildId) -> bool { pub async fn is_self_connected_to_vc(ctx: &Context, guild_id: &GuildId) -> bool {
let channel_id = get_self_vc_id(ctx, guild_id); get_self_vc_id(ctx, guild_id).await.is_some()
channel_id.await.is_some()
} }
/// Check if a user is connected to a voice channel /// Check if a user is connected to a voice channel
pub async fn is_user_connected_to_vc(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> bool { pub async fn is_user_connected_to_vc(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> bool {
let channel_id = get_vc_id(ctx, guild_id, user_id).await; get_vc_id(ctx, guild_id, user_id).await.is_some()
channel_id.is_some()
} }
/// Get the voice channel id of the bot /// Get the voice channel id of the bot