feat: bot now plays songs, started to work on queue system

This commit is contained in:
moonleay 2024-03-08 01:18:30 +01:00
parent edc22a91f2
commit a16d8a6b60
Signed by: moonleay
GPG key ID: 82667543CCD715FB
12 changed files with 289 additions and 130 deletions

View file

@ -22,7 +22,7 @@ impl Embed {
.title(title)
.description(message)
.footer(CreateEmbedFooter::new(format!(
"> {} | {}",
"> {} - {}",
current_time, username
)))
}

View file

@ -1,21 +1,7 @@
use serenity::all::{CacheHttp, ChannelId, Context, Guild, GuildId, GuildRef, PartialGuild, UserId};
use serenity::Error;
/// Get a guild by id
pub fn get_guild_cached(ctx: &Context, guild_id: &GuildId) -> Option<Guild> {
let guild = match ctx.cache.guild(guild_id) {
Some(guild) => guild,
None => {
println!("Cannot get guild with id {:?}!", guild_id);
return None
}
};
println!("Got guild: {:?}", guild.name);
Some(guild.clone())
}
use std::collections::HashMap;
use serenity::all::{ChannelId, Context, Guild, GuildId, PartialGuild, UserId, VoiceState};
/// Request a guild by id, get it from cache
pub fn request_guild(ctx: &Context, guild_id: &GuildId) -> Guild {
let guild = match guild_id.to_guild_cached(&ctx.cache) {
Some(guild) => guild.clone(),
@ -39,26 +25,7 @@ pub async fn request_partial_guild(ctx: &Context, guild_id: &GuildId) -> Partial
guild
}
/// Get the current channel id of the bot
pub fn get_vc_id_cached(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> Option<ChannelId> {
let guild = match get_guild_cached(&ctx, guild_id){
Some(guild) => guild,
None => {
println!("Cannot get guild while getting channel id!");
return None
}
};
let channel_id = guild
.voice_states
.get(user_id)
.and_then(|voice_state| voice_state.channel_id)?;
println!("Got vc with id: {:?}", channel_id);
Some(channel_id)
}
/// Get the voice channel id of a user
pub async fn get_vc_id(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> Option<ChannelId> {
let guild = request_guild(&ctx, guild_id);
guild
@ -69,27 +36,40 @@ 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
pub fn is_self_connected_to_vc_cached(ctx: &Context, guild_id: &GuildId) -> bool {
let channel_id = get_self_vc_id_cached(ctx, guild_id);
!channel_id.is_none()
}
pub async fn is_self_connected_to_vc(ctx: &Context, guild_id: &GuildId) -> bool {
let channel_id = get_self_vc_id(ctx, guild_id);
!channel_id.await.is_none()
}
/// Get the current channel id of the bot
pub fn get_self_vc_id_cached(ctx: &Context, guild_id: &GuildId) -> Option<ChannelId> {
let channel_id = get_vc_id_cached(ctx, guild_id, &ctx.cache.current_user().id)?;
/// 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 {
let channel_id = get_vc_id(ctx, guild_id, user_id).await;
Some(channel_id)
!channel_id.is_none()
}
/// Get the voice channel id of the bot
pub async fn get_self_vc_id(ctx: &Context, guild_id: &GuildId) -> Option<ChannelId> {
let user_id = ctx.cache.current_user().id;
get_vc_id(ctx, guild_id, &user_id).await
}
/// Get all voice states of a guild
pub async fn get_voice_states(ctx: &Context, guild_id: &GuildId) -> HashMap<UserId, VoiceState>{
let guild = request_guild(ctx, guild_id);
let voice_states = guild.voice_states.clone();
voice_states
}
/// Get the amount of members in a voice channel
pub async fn get_amount_of_members_in_vc(ctx: &Context, guild_id: &GuildId, channel_id: &ChannelId,) -> usize {
let voice_states = get_voice_states(ctx, guild_id).await;
let amount = voice_states
.iter()
.filter(|(_, voice_state)| voice_state.channel_id == Some(*channel_id))
.count();
amount
}