fix: fixed import errors

feat: removed unneeded functions, added logging to user_util
This commit is contained in:
moonleay 2024-03-04 23:39:36 +01:00
parent 57e9bca8f0
commit 4d6e665a3b
Signed by: moonleay
GPG key ID: 82667543CCD715FB
3 changed files with 43 additions and 56 deletions

View file

@ -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::builder::{CreateCommand, CreateCommandOption, CreateEmbed};
use serenity::model::application::CommandOptionType; use serenity::model::application::CommandOptionType;
use crate::util::embed::Embed; 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 { pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
let username = command.user.name.as_str(); 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."); return Embed::create(username, "GuildId not found", "Could not find guild id.");
} }
}; };
println!("Guild ID: {:?}", guild_id);
let (guild_id, channel_id) = { let connect_to = get_vc_id(ctx, &guild_id, &command.user.id).expect("Cannot get 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 manager = &songbird::get(ctx) let manager = &songbird::get(ctx)
.await .await
@ -86,12 +61,12 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
} }
// Connect to VC // Connect to VC
manager manager
.join(guild_id, connect_to) .join(*guild_id, connect_to)
.await .await
.expect("Cannot connect>..."); .expect("Cannot connect>...");
} }
Embed::create(username, "Searching...", format!("Looking for {}", query)) Embed::create(username, "Searching...", format!("Looking for {:?}", query))
} }
pub fn register() -> CreateCommand { pub fn register() -> CreateCommand {

View file

@ -9,7 +9,7 @@ use serenity::gateway::ActivityData;
use serenity::model::application::{Command, Interaction}; use serenity::model::application::{Command, Interaction};
use serenity::model::gateway::Ready; use serenity::model::gateway::Ready;
use serenity::prelude::*; use serenity::prelude::*;
use util::{config, error::Embed}; use util::{config, embed::Embed};
// This trait adds the `register_songbird` and `register_songbird_with` methods // This trait adds the `register_songbird` and `register_songbird_with` methods
// to the client builder below, making it easy to install this voice client. // to the client builder below, making it easy to install this voice client.

View file

@ -1,38 +1,50 @@
use serenity::all::{ChannelId, Context, Guild, GuildId}; use serenity::all::{ChannelId, Context, Guild, GuildId, UserId};
/// Get a guild by id /// Get a guild by id
pub fn get_guild(ctx: &Context, guild_id: &GuildId) -> Option<Guild> { pub fn get_guild(ctx: &Context, guild_id: &GuildId) -> Option<Guild> {
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()) Some(guild.clone())
} }
/// Get the current channel id of the bot /// Get the current channel id of the bot
pub fn get_channel_id(ctx: &Context, guild_id: &GuildId) -> Option<ChannelId> { pub fn get_vc_id(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> Option<ChannelId> {
let guild = get_guild(ctx, guild_id)?; 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 let channel_id = guild
.voice_states .voice_states
.get(&ctx.cache.current_user().id) .get(user_id)
.and_then(|voice_state| voice_state.channel_id); .and_then(|voice_state| voice_state.channel_id)?;
Some(channel_id.unwrap()) println!("Got vc with id: {:?}", channel_id);
}
Some(channel_id)
pub fn is_self_connected_to_vc(ctx: &Context, guild_id: &GuildId) -> bool { }
let channel_id = get_channel_id(ctx, guild_id);
/// Check if the bot is connected to a voice channel
// TODO: There has to be a way to improve this. This is bad code and it should be optimized. pub fn is_self_connected_to_vc(ctx: &Context, guild_id: &GuildId) -> bool {
if channel_id.is_none() { let channel_id = get_self_vc_id(ctx, guild_id);
return false;
} !channel_id.is_none()
}
true
} /// Get the current channel id of the bot
// 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<ChannelId> {
let channel_id = get_vc_id(ctx, guild_id, &ctx.cache.current_user().id)?;
pub fn get_self_vc_id(ctx: &Context, guild_id: &GuildId) -> Option<ChannelId> {
let channel_id = get_channel_id(ctx, guild_id)?;
Some(channel_id) Some(channel_id)
} }