Changing bot commands to be used by endpoints
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use log::{debug, warn, error};
|
||||
|
||||
use serenity::model::prelude::GuildId;
|
||||
use serenity::model::user::User;
|
||||
use serenity::{prelude::*, async_trait};
|
||||
use serenity::builder::CreateApplicationCommand;
|
||||
use serenity::model::application::interaction::application_command::ApplicationCommandInteraction;
|
||||
use siren::ServiceError;
|
||||
use songbird::EventHandler;
|
||||
|
||||
use crate::bot::commands::audio::{join, leave, add_song, get_songbird, AudioConfigs};
|
||||
use crate::AppState;
|
||||
use crate::bot::commands::audio::{join, leave, add_song, get_songbird};
|
||||
use crate::db::guilds::QueryGuild;
|
||||
|
||||
use super::{create_response, edit_response};
|
||||
|
||||
@@ -46,7 +53,8 @@ pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) {
|
||||
return;
|
||||
}
|
||||
|
||||
match join(&ctx, &command.guild_id, &command.user).await {
|
||||
let manager = get_songbird(ctx).await;
|
||||
match join(&ctx.cache, manager,&command.guild_id, &command.user).await {
|
||||
Ok(_) => {
|
||||
let guild_id = match command.guild_id {
|
||||
Some(g) => g,
|
||||
@@ -65,12 +73,8 @@ pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) {
|
||||
let call_handler = handler_lock.lock().await;
|
||||
call_handler.queue().is_empty()
|
||||
};
|
||||
let audio_config = {
|
||||
let data_read = ctx.data.read().await;
|
||||
data_read.get::<AudioConfigs>().expect("Expected AudioConfigs in TypeMap.").clone()
|
||||
};
|
||||
let ac = audio_config.read().await;
|
||||
match add_song(handler_lock.clone(), &track_url, is_queue_empty, ac.get(&guild_id)).await {
|
||||
let guild = QueryGuild::get(guild_id.0 as i64).unwrap();
|
||||
match add_song(handler_lock.clone(), &track_url, is_queue_empty, Some(guild.volume)).await {
|
||||
Ok(added_song) => {
|
||||
let track_title = added_song.title.unwrap();
|
||||
debug!("Added track: {}", track_title);
|
||||
@@ -86,7 +90,7 @@ pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) {
|
||||
if let Err(why) = edit_response(&ctx, &command, format!("Failed to add song: {}", why)).await {
|
||||
error!("Failed to edit response message: {}", why);
|
||||
}
|
||||
if let Err(why) = leave(&ctx, &command.guild_id).await {
|
||||
if let Err(why) = leave(manager, &command.guild_id).await {
|
||||
error!("Failed to leave voice channel: {}", why);
|
||||
}
|
||||
return;
|
||||
@@ -103,6 +107,40 @@ pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn play(state: Arc<AppState>, guild_id: Option<GuildId>, user: &User, track_url: String) -> Result<(), ServiceError> {
|
||||
match join(&state.cache, Arc::clone(&state.songbird), &guild_id, user).await {
|
||||
Ok(_) => {
|
||||
let guild_id = match guild_id {
|
||||
Some(g) => g,
|
||||
None => {
|
||||
return Err(ServiceError {
|
||||
status: 422,
|
||||
message: "No guild ID set".to_string()
|
||||
});
|
||||
}
|
||||
};
|
||||
if let Some(handler_lock) = state.songbird.get(guild_id) {
|
||||
let is_queue_empty = {
|
||||
let call_handler = handler_lock.lock().await;
|
||||
call_handler.queue().is_empty()
|
||||
};
|
||||
let guild = QueryGuild::get(guild_id.0 as i64)?;
|
||||
match add_song(handler_lock.clone(), &track_url, is_queue_empty, Some(guild.volume)).await {
|
||||
Ok(_) => {},
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
Err(err) => {
|
||||
return Err(ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
||||
command.name("play").description("Plays the given track").create_option(|option| { option
|
||||
.name("track")
|
||||
|
||||
Reference in New Issue
Block a user