From 4d6e665a3b31ac6479a8e10b4ec4124523cc669a Mon Sep 17 00:00:00 2001 From: moonleay Date: Mon, 4 Mar 2024 23:39:36 +0100 Subject: [PATCH] fix: fixed import errors feat: removed unneeded functions, added logging to user_util --- src/commands/play.rs | 39 ++++++----------------------- src/main.rs | 2 +- src/util/user_util.rs | 58 ++++++++++++++++++++++++++----------------- 3 files changed, 43 insertions(+), 56 deletions(-) diff --git a/src/commands/play.rs b/src/commands/play.rs index 3791885..fffaf21 100644 --- a/src/commands/play.rs +++ b/src/commands/play.rs @@ -1,9 +1,9 @@ -use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, Context}; +use serenity::all::{CommandDataOptionValue, CommandInteraction, Context}; use serenity::builder::{CreateCommand, CreateCommandOption, CreateEmbed}; use serenity::model::application::CommandOptionType; use crate::util::embed::Embed; -use crate::util::user_util::{self, get_guild}; +use crate::util::user_util::{self, get_vc_id}; pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed { let username = command.user.name.as_str(); @@ -27,35 +27,10 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed { return Embed::create(username, "GuildId not found", "Could not find guild id."); } }; + + println!("Guild ID: {:?}", guild_id); - let (guild_id, channel_id) = { - let guild = get_guild(ctx, guild_id); - - match guild { - Some(guild) => { - let channel_id = guild - .voice_states - .get(&command.user.id) - .and_then(|voice_state| voice_state.channel_id); - - (guild.id, channel_id) - } - None => { - return Embed::create(username, "Guild not found", "Could not find guild."); - } - } - }; - - let connect_to = match channel_id { - Some(channel) => channel, - None => { - return Embed::create( - username, - "You are not in a VC.", - "Join one to start playing music.", - ); - } - }; + let connect_to = get_vc_id(ctx, &guild_id, &command.user.id).expect("Cannot get channel id"); let manager = &songbird::get(ctx) .await @@ -86,12 +61,12 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed { } // Connect to VC manager - .join(guild_id, connect_to) + .join(*guild_id, connect_to) .await .expect("Cannot connect>..."); } - Embed::create(username, "Searching...", format!("Looking for {}", query)) + Embed::create(username, "Searching...", format!("Looking for {:?}", query)) } pub fn register() -> CreateCommand { diff --git a/src/main.rs b/src/main.rs index cbc9c4f..5c533d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use serenity::gateway::ActivityData; use serenity::model::application::{Command, Interaction}; use serenity::model::gateway::Ready; use serenity::prelude::*; -use util::{config, error::Embed}; +use util::{config, embed::Embed}; // This trait adds the `register_songbird` and `register_songbird_with` methods // to the client builder below, making it easy to install this voice client. diff --git a/src/util/user_util.rs b/src/util/user_util.rs index ad88d02..46a91b0 100644 --- a/src/util/user_util.rs +++ b/src/util/user_util.rs @@ -1,38 +1,50 @@ -use serenity::all::{ChannelId, Context, Guild, GuildId}; +use serenity::all::{ChannelId, Context, Guild, GuildId, UserId}; /// Get a guild by id pub fn get_guild(ctx: &Context, guild_id: &GuildId) -> Option { - let guild = ctx.cache.guild(guild_id)?; + let guild = match ctx.cache.guild(guild_id) { + Some(guild) => guild, + None => { + println!("Cannot get guild!"); + return None + } + }; // TODO This is always None for some reason + + println!("Got guild: {:?}", guild.name); Some(guild.clone()) } /// Get the current channel id of the bot -pub fn get_channel_id(ctx: &Context, guild_id: &GuildId) -> Option { - let guild = get_guild(ctx, guild_id)?; +pub fn get_vc_id(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> Option { + let guild = match get_guild(&ctx, guild_id){ + Some(guild) => guild, + None => { + println!("Cannot get guild while getting channel id!"); + return None + } + }; let channel_id = guild .voice_states - .get(&ctx.cache.current_user().id) - .and_then(|voice_state| voice_state.channel_id); + .get(user_id) + .and_then(|voice_state| voice_state.channel_id)?; - Some(channel_id.unwrap()) -} - -pub fn is_self_connected_to_vc(ctx: &Context, guild_id: &GuildId) -> bool { - let channel_id = get_channel_id(ctx, guild_id); - - // TODO: There has to be a way to improve this. This is bad code and it should be optimized. - if channel_id.is_none() { - return false; - } - - true -} -// This whole file is jank. I have to rewrite this once I know Rust better - -pub fn get_self_vc_id(ctx: &Context, guild_id: &GuildId) -> Option { - let channel_id = get_channel_id(ctx, guild_id)?; + println!("Got vc with id: {:?}", channel_id); + + Some(channel_id) +} + +/// Check if the bot is connected to a voice channel +pub fn is_self_connected_to_vc(ctx: &Context, guild_id: &GuildId) -> bool { + let channel_id = get_self_vc_id(ctx, guild_id); + + !channel_id.is_none() +} + +/// Get the current channel id of the bot +pub fn get_self_vc_id(ctx: &Context, guild_id: &GuildId) -> Option { + let channel_id = get_vc_id(ctx, guild_id, &ctx.cache.current_user().id)?; Some(channel_id) }