feat: added getting data from discord
This commit is contained in:
parent
4d6e665a3b
commit
cfd051be3f
3 changed files with 63 additions and 15 deletions
|
@ -30,18 +30,18 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
|
||||||
|
|
||||||
println!("Guild ID: {:?}", guild_id);
|
println!("Guild ID: {:?}", guild_id);
|
||||||
|
|
||||||
let connect_to = get_vc_id(ctx, &guild_id, &command.user.id).expect("Cannot get channel id");
|
let connect_to = get_vc_id(ctx, &guild_id, &command.user.id).await.expect("Cannot get channel id");
|
||||||
|
|
||||||
let manager = &songbird::get(ctx)
|
let manager = &songbird::get(ctx)
|
||||||
.await
|
.await
|
||||||
.expect("Cannot get Songbird.")
|
.expect("Cannot get Songbird.")
|
||||||
.clone();
|
.clone();
|
||||||
|
|
||||||
if !user_util::is_self_connected_to_vc(ctx, &guild_id) {
|
if !user_util::is_self_connected_to_vc(ctx, &guild_id).await {
|
||||||
// self is connected to vc, check if user is in same vc
|
// self is connected to vc, check if user is in same vc
|
||||||
let self_channel = user_util::get_self_vc_id(ctx, &guild_id);
|
let self_channel = user_util::get_self_vc_id(ctx, &guild_id).await;
|
||||||
|
|
||||||
if self_channel.is_none() {
|
if self_channel.is_err() {
|
||||||
return Embed::create(
|
return Embed::create(
|
||||||
username,
|
username,
|
||||||
"I am not in a VC.",
|
"I am not in a VC.",
|
||||||
|
|
|
@ -14,7 +14,7 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if !user_util::is_self_connected_to_vc(ctx, guild_id) {
|
if !user_util::is_self_connected_to_vc_cached(ctx, guild_id) {
|
||||||
// Bot is not connectd to vc; no need to dc
|
// Bot is not connectd to vc; no need to dc
|
||||||
return Embed::create(
|
return Embed::create(
|
||||||
username,
|
username,
|
||||||
|
|
|
@ -1,23 +1,48 @@
|
||||||
use serenity::all::{ChannelId, Context, Guild, GuildId, UserId};
|
use futures::future::BoxFuture;
|
||||||
|
use serenity::all::{ChannelId, Context, Guild, GuildId, PartialGuild, UserId};
|
||||||
|
use serenity::Error;
|
||||||
|
|
||||||
/// 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_cached(ctx: &Context, guild_id: &GuildId) -> Option<Guild> {
|
||||||
let guild = match ctx.cache.guild(guild_id) {
|
let guild = match ctx.cache.guild(guild_id) {
|
||||||
Some(guild) => guild,
|
Some(guild) => guild,
|
||||||
None => {
|
None => {
|
||||||
println!("Cannot get guild!");
|
println!("Cannot get guild with id {:?}!", guild_id);
|
||||||
return None
|
return None
|
||||||
}
|
}
|
||||||
}; // TODO This is always None for some reason
|
};
|
||||||
|
|
||||||
println!("Got guild: {:?}", guild.name);
|
println!("Got guild: {:?}", guild.name);
|
||||||
|
|
||||||
Some(guild.clone())
|
Some(guild.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn request_guild(ctx: &Context, guild_id: &GuildId) -> Result<Guild, Error> {
|
||||||
|
let guild = match ctx.http.get_guild(*guild_id).await {
|
||||||
|
Ok(guild) => guild,
|
||||||
|
Err(error) => {
|
||||||
|
return Err(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(guild.id.to_guild_cached(ctx).unwrap().clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Request a guild by id, get it from Discord, not from cache, this is a partial guild
|
||||||
|
pub async fn request_partial_guild(ctx: &Context, guild_id: &GuildId) -> PartialGuild {
|
||||||
|
let guild = match ctx.http.get_guild(*guild_id).await {
|
||||||
|
Ok(guild) => guild,
|
||||||
|
Err(error) => {
|
||||||
|
panic!("error whilest getting guild from Discord {}", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
guild
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the current channel id of the bot
|
/// Get the current channel id of the bot
|
||||||
pub fn get_vc_id(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> Option<ChannelId> {
|
pub fn get_vc_id_cached(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> Option<ChannelId> {
|
||||||
let guild = match get_guild(&ctx, guild_id){
|
let guild = match get_guild_cached(&ctx, guild_id){
|
||||||
Some(guild) => guild,
|
Some(guild) => guild,
|
||||||
None => {
|
None => {
|
||||||
println!("Cannot get guild while getting channel id!");
|
println!("Cannot get guild while getting channel id!");
|
||||||
|
@ -35,16 +60,39 @@ pub fn get_vc_id(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> Option<
|
||||||
Some(channel_id)
|
Some(channel_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_vc_id(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> Result<ChannelId, Error> {
|
||||||
|
let guild = request_guild(&ctx, guild_id).await?;
|
||||||
|
|
||||||
|
let channel_id = guild
|
||||||
|
.voice_states
|
||||||
|
.get(user_id)
|
||||||
|
.and_then(|voice_state| voice_state.channel_id)
|
||||||
|
.expect("Cannot get channel id");
|
||||||
|
|
||||||
|
Ok(channel_id)
|
||||||
|
}
|
||||||
|
|
||||||
/// Check if the bot is connected to a voice channel
|
/// Check if the bot is connected to a voice channel
|
||||||
pub fn is_self_connected_to_vc(ctx: &Context, guild_id: &GuildId) -> bool {
|
pub fn is_self_connected_to_vc_cached(ctx: &Context, guild_id: &GuildId) -> bool {
|
||||||
let channel_id = get_self_vc_id(ctx, guild_id);
|
let channel_id = get_self_vc_id_cached(ctx, guild_id);
|
||||||
|
|
||||||
!channel_id.is_none()
|
!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_err()
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the current channel id of the bot
|
/// Get the current channel id of the bot
|
||||||
pub fn get_self_vc_id(ctx: &Context, guild_id: &GuildId) -> Option<ChannelId> {
|
pub fn get_self_vc_id_cached(ctx: &Context, guild_id: &GuildId) -> Option<ChannelId> {
|
||||||
let channel_id = get_vc_id(ctx, guild_id, &ctx.cache.current_user().id)?;
|
let channel_id = get_vc_id_cached(ctx, guild_id, &ctx.cache.current_user().id)?;
|
||||||
|
|
||||||
Some(channel_id)
|
Some(channel_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_self_vc_id(ctx: &Context, guild_id: &GuildId) -> Result<ChannelId, Error> {
|
||||||
|
let channel_id = get_vc_id(ctx, guild_id, &ctx.cache.current_user().id).await;
|
||||||
|
channel_id // This throws errors in main.rs for some reason
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue