Cleanup commands

This commit is contained in:
2024-09-05 13:12:39 -04:00
parent 399eb8a29d
commit 3994c18c49
9 changed files with 182 additions and 278 deletions

View File

@@ -17,101 +17,68 @@ pub mod skip;
pub mod stop;
pub mod volume;
pub async fn get_songbird(ctx: &Context) -> Arc<Songbird> {
songbird::get(ctx)
.await
.expect("Songbird Voice client placed in at initialization")
}
/**
* Finds a voice channel that the user is currently in, and attempts to join it.
*/
pub async fn join_by_user(
pub async fn join_voice_channel(
cache: &Arc<Cache>,
manager: &Arc<Songbird>,
guild_id: &GuildId,
user: &User,
) -> SirenResult<ChannelId> {
let channel_id = match find_voice_channel(cache, guild_id, user) {
Ok(channel) => channel,
Err(err) => return Err(SirenError::new(500, err.to_string()))
};
join_voice_channel(manager, guild_id, &channel_id).await?;
Ok(channel_id)
}
/**
* Joins a voice channel.
*/
async fn join_voice_channel(
manager: &Arc<Songbird>,
guild_id: &GuildId,
channel_id: &ChannelId,
) -> SirenResult<()> {
let channel_id = find_voice_channel(cache, guild_id, user)?;
log::debug!("<{}> Joining channel {}", guild_id.get(), channel_id.get());
manager.join(guild_id.to_owned(), channel_id.to_owned()).await?;
Ok(())
Ok(channel_id)
}
/**
* Leaves a voice channel.
*/
pub async fn leave_voice_channel(
manager: Arc<Songbird>,
guild_id_option: &Option<GuildId>,
) -> Result<(), String> {
let guild_id = match guild_id_option {
Some(g) => g,
None => {
return Err(format!("{}", "No guild ID set"));
}
};
if manager.get(*guild_id).is_some() {
manager: &Arc<Songbird>,
guild_id: &GuildId,
) -> SirenResult<()> {
if manager.get(guild_id.to_owned()).is_some() {
log::debug!("<{}> Disconnecting from channel", guild_id.get());
if let Err(e) = manager.remove(*guild_id).await {
return Err(format!("{}", e));
}
manager.remove(*guild_id).await?;
}
Ok(())
}
/**
* Finds a voice channel that the user is currently in.
*/
fn find_voice_channel(
cache: &Arc<Cache>,
guild_id: &GuildId,
user: &User,
) -> Result<ChannelId, String> {
let guild = match guild_id.to_guild_cached(cache) {
Some(g) => g,
None => return Err(format!("Guild not found")),
};
match guild
.voice_states
.get(&user.id)
.and_then(|voice_state| voice_state.channel_id)
{
Some(channel) => Ok(channel),
None => return Err(format!("User is not in a voice channel")),
}
}
pub async fn create_response(
ctx: &Context,
command: &CommandInteraction,
content: String,
) -> Result<(), SerenityError> {
let data = CreateInteractionResponseMessage::new().content(content);
) {
let data = CreateInteractionResponseMessage::new().content(content.to_owned());
let builder = CreateInteractionResponse::Message(data);
command.create_response(&ctx.http, builder).await?;
Ok(())
match command.create_response(&ctx.http, builder).await {
Ok(_) => {}
Err(err) => {
log::error!("Failed to create response for {content}\n{err}");
}
};
}
pub async fn edit_response(
ctx: &Context,
command: &CommandInteraction,
content: String,
) -> Result<serenity::model::channel::Message, SerenityError> {
let builder = EditInteractionResponse::new().content(content);
command.edit_response(&ctx.http, builder).await
) {
let builder = EditInteractionResponse::new().content(content.to_owned());
match command.edit_response(&ctx.http, builder).await {
Ok(_) => {}
Err(err) => {
log::error!("Failed to create response for {content}\n{err}");
}
}
}
/**
@@ -129,8 +96,25 @@ fn is_valid_url(url: &str) -> (bool, bool) {
})
}
pub async fn get_songbird(ctx: &Context) -> Arc<Songbird> {
songbird::get(ctx)
.await
.expect("Songbird Voice client placed in at initialization")
/**
* Finds a voice channel that the user is currently in.
*/
fn find_voice_channel(
cache: &Arc<Cache>,
guild_id: &GuildId,
user: &User,
) -> SirenResult<ChannelId> {
let guild = match guild_id.to_guild_cached(cache) {
Some(g) => g,
None => return Err(SirenError::new(404, "Guild not found".to_string())),
};
match guild
.voice_states
.get(&user.id)
.and_then(|voice_state| voice_state.channel_id)
{
Some(channel) => Ok(channel),
None => return Err(SirenError::new(401, "User is not in a voice channel".to_string())),
}
}