Compare commits
4 Commits
message_fo
...
cleanup
| Author | SHA1 | Date | |
|---|---|---|---|
|
4a69e0f578
|
|||
|
cad6cf22fa
|
|||
|
9f0e934f92
|
|||
|
92be6c26a6
|
@@ -1,10 +1,40 @@
|
||||
use crate::state::{State, StateRef};
|
||||
use anyhow::Context;
|
||||
use std::{error::Error, sync::Arc};
|
||||
use twilight_model::{
|
||||
gateway::payload::incoming::InteractionCreate,
|
||||
id::{marker::GuildMarker, Id},
|
||||
};
|
||||
|
||||
pub(crate) async fn leave_if_alone(
|
||||
guild_id: Id<GuildMarker>,
|
||||
state: State,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||
let user = state
|
||||
.cache
|
||||
.current_user()
|
||||
.context("Cannot get current user")?;
|
||||
let user_voice_state = state
|
||||
.cache
|
||||
.voice_state(user.id, guild_id)
|
||||
.context("Cannot get voice state")?;
|
||||
let channel = state
|
||||
.cache
|
||||
.channel(user_voice_state.channel_id())
|
||||
.context("Cannot get channel")?;
|
||||
let channel_voice_states = state
|
||||
.cache
|
||||
.voice_channel_states(channel.id)
|
||||
.context("Cannot get voice channel")?;
|
||||
let count = channel_voice_states.count();
|
||||
|
||||
// count is 1 if the bot is the only one in the channel
|
||||
if count == 1 {
|
||||
leave_channel(guild_id, Arc::clone(&state)).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn leave_channel(
|
||||
guild_id: Id<GuildMarker>,
|
||||
state: Arc<StateRef>,
|
||||
|
||||
@@ -3,7 +3,7 @@ pub(crate) use join::join;
|
||||
|
||||
mod leave;
|
||||
pub(crate) use leave::leave;
|
||||
pub(crate) use leave::leave_channel;
|
||||
pub(crate) use leave::leave_if_alone;
|
||||
|
||||
mod pause;
|
||||
pub(crate) use pause::pause;
|
||||
|
||||
@@ -59,7 +59,6 @@ async fn get_tracks(
|
||||
if stderr.contains("YouTube said: The playlist does not exist.") {
|
||||
return Err("YouTube said: The playlist does not exist.".into());
|
||||
}
|
||||
return Err(stderr.into());
|
||||
}
|
||||
return Err("No tracks found".into());
|
||||
}
|
||||
@@ -177,10 +176,10 @@ pub(crate) async fn play(
|
||||
for yttrack in &tracks {
|
||||
tracing::debug!("track: {:?}", yttrack);
|
||||
let url = yttrack
|
||||
.url
|
||||
.original_url
|
||||
.clone()
|
||||
.or(yttrack.original_url.clone())
|
||||
.ok_or("")?;
|
||||
.or(yttrack.url.clone())
|
||||
.ok_or("Could not find url")?;
|
||||
let mut src = YoutubeDl::new(reqwest::Client::new(), url.clone());
|
||||
let src_copy = src.clone();
|
||||
let track: Track = src_copy.into();
|
||||
|
||||
@@ -59,9 +59,9 @@ pub(crate) async fn build_queue_embeds(queue: &[TrackHandle], page: usize) -> Ve
|
||||
}
|
||||
message.push('\n');
|
||||
|
||||
let max_pages = queue.len() / TRACKS_PER_PAGE;
|
||||
if max_pages > 0 {
|
||||
message.push_str(&format!("page {}/{}", 1 + page, 1 + max_pages));
|
||||
let n_pages = (queue.len() + TRACKS_PER_PAGE - 1) / TRACKS_PER_PAGE;
|
||||
if n_pages > 1 {
|
||||
message.push_str(&format!("page {}/{}", 1 + page, n_pages));
|
||||
}
|
||||
vec![EmbedBuilder::new()
|
||||
.description(&message)
|
||||
@@ -69,10 +69,7 @@ pub(crate) async fn build_queue_embeds(queue: &[TrackHandle], page: usize) -> Ve
|
||||
.build()]
|
||||
}
|
||||
|
||||
pub(crate) fn build_action_row(page: usize, max_pages: usize) -> Vec<Component> {
|
||||
if max_pages == 0 {
|
||||
return Vec::new();
|
||||
}
|
||||
pub(crate) fn build_action_row(page: usize, n_pages: usize) -> Vec<Component> {
|
||||
vec![Component::ActionRow(ActionRow {
|
||||
components: vec![
|
||||
Component::Button(Button {
|
||||
@@ -85,6 +82,16 @@ pub(crate) fn build_action_row(page: usize, max_pages: usize) -> Vec<Component>
|
||||
url: None,
|
||||
disabled: page == 0,
|
||||
}),
|
||||
Component::Button(Button {
|
||||
custom_id: Some(format!("page:{}", page)),
|
||||
style: ButtonStyle::Primary,
|
||||
label: Some("Refresh".to_string()),
|
||||
emoji: Some(ReactionType::Unicode {
|
||||
name: "🔄".to_string(),
|
||||
}),
|
||||
url: None,
|
||||
disabled: false,
|
||||
}),
|
||||
Component::Button(Button {
|
||||
custom_id: Some(format!("page:{}", page + 1)),
|
||||
style: ButtonStyle::Primary,
|
||||
@@ -93,7 +100,7 @@ pub(crate) fn build_action_row(page: usize, max_pages: usize) -> Vec<Component>
|
||||
name: "➡️".to_string(),
|
||||
}),
|
||||
url: None,
|
||||
disabled: page >= max_pages,
|
||||
disabled: page >= n_pages - 1,
|
||||
}),
|
||||
],
|
||||
})]
|
||||
@@ -139,7 +146,8 @@ pub(crate) async fn queue(
|
||||
}
|
||||
|
||||
let embeds = build_queue_embeds(&queue, 0).await;
|
||||
let action_row = build_action_row(0, queue.len() / TRACKS_PER_PAGE);
|
||||
let n_pages = (queue.len() + TRACKS_PER_PAGE - 1) / TRACKS_PER_PAGE;
|
||||
let action_row = build_action_row(0, n_pages);
|
||||
|
||||
state
|
||||
.http
|
||||
|
||||
196
src/handler.rs
196
src/handler.rs
@@ -1,75 +1,19 @@
|
||||
use crate::commands::queue::{build_action_row, build_queue_embeds, TRACKS_PER_PAGE};
|
||||
use crate::commands::{
|
||||
delete, join, leave, leave_channel, loop_queue, pause, play, queue, resume, skip, stop,
|
||||
delete, join, leave, leave_if_alone, loop_queue, pause, play, queue, resume, skip, stop,
|
||||
};
|
||||
use crate::interaction_commands::InteractionCommand;
|
||||
use crate::state::State;
|
||||
use futures::Future;
|
||||
use std::error::Error;
|
||||
use crate::utils::spawn;
|
||||
use anyhow::Context;
|
||||
use std::sync::Arc;
|
||||
use tracing::debug;
|
||||
use twilight_gateway::Event;
|
||||
use twilight_model::application::interaction::application_command::{
|
||||
CommandData, CommandOptionValue,
|
||||
};
|
||||
use twilight_model::application::interaction::message_component::MessageComponentInteractionData;
|
||||
use twilight_model::application::interaction::InteractionData;
|
||||
use twilight_model::gateway::payload::incoming::VoiceStateUpdate;
|
||||
use twilight_model::gateway::payload::incoming::InteractionCreate;
|
||||
use twilight_model::http::interaction::{InteractionResponse, InteractionResponseType};
|
||||
use twilight_util::builder::InteractionResponseDataBuilder;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum InteractionCommand {
|
||||
Play(String),
|
||||
Stop,
|
||||
Pause,
|
||||
Skip,
|
||||
Loop,
|
||||
Resume,
|
||||
Leave,
|
||||
Join,
|
||||
Queue,
|
||||
NotImplemented,
|
||||
}
|
||||
|
||||
fn spawn(
|
||||
fut: impl Future<Output = Result<(), Box<dyn Error + Send + Sync + 'static>>> + Send + 'static,
|
||||
) {
|
||||
tokio::spawn(async move {
|
||||
if let Err(why) = fut.await {
|
||||
tracing::debug!("handler error: {:?}", why);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub(crate) async fn leave_if_alone(
|
||||
update: VoiceStateUpdate,
|
||||
state: State,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||
let guild_id = update.guild_id.ok_or("Guild ID not found")?;
|
||||
let user = state
|
||||
.cache
|
||||
.current_user()
|
||||
.ok_or("Cannot get current user")?;
|
||||
let user_voice_state = state
|
||||
.cache
|
||||
.voice_state(user.id, guild_id)
|
||||
.ok_or("Cannot get voice state")?;
|
||||
let channel = state
|
||||
.cache
|
||||
.channel(user_voice_state.channel_id())
|
||||
.ok_or("Cannot get channel")?;
|
||||
let channel_voice_states = state
|
||||
.cache
|
||||
.voice_channel_states(channel.id)
|
||||
.ok_or("Cannot get voice channel")?;
|
||||
let count = channel_voice_states.count();
|
||||
|
||||
// count is 1 if the bot is the only one in the channel
|
||||
if count == 1 {
|
||||
leave_channel(guild_id, Arc::clone(&state)).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) struct Handler {
|
||||
state: State,
|
||||
}
|
||||
@@ -79,6 +23,13 @@ impl Handler {
|
||||
Self { state }
|
||||
}
|
||||
pub(crate) async fn act(&self, event: Event) -> anyhow::Result<()> {
|
||||
self.handle_messages(&event).await?;
|
||||
self.handle_voice_state_update(&event).await?;
|
||||
self.handle_interaction(&event).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_messages(&self, event: &Event) -> anyhow::Result<()> {
|
||||
match event {
|
||||
Event::MessageCreate(message) if message.content.starts_with('!') => {
|
||||
if message.content.contains("!delete") {
|
||||
@@ -86,51 +37,66 @@ impl Handler {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_voice_state_update(&self, event: &Event) -> anyhow::Result<()> {
|
||||
match event {
|
||||
Event::VoiceStateUpdate(update) => {
|
||||
spawn(leave_if_alone(*update.clone(), Arc::clone(&self.state)));
|
||||
let guild_id = update.guild_id.context("Guild ID not found")?;
|
||||
spawn(leave_if_alone(guild_id, Arc::clone(&self.state)));
|
||||
Ok(())
|
||||
}
|
||||
Event::InteractionCreate(interaction) => {
|
||||
tracing::info!("interaction: {:?}", &interaction);
|
||||
match &interaction.data {
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_interaction(&self, event: &Event) -> anyhow::Result<()> {
|
||||
match event {
|
||||
Event::InteractionCreate(interaction) => match &interaction.data {
|
||||
Some(InteractionData::ApplicationCommand(command)) => {
|
||||
let interaction_command = parse_interaction_command(command);
|
||||
debug!("{:?}", interaction_command);
|
||||
match interaction_command {
|
||||
self.handle_application_command(command.clone().into(), interaction.clone())
|
||||
}
|
||||
Some(InteractionData::MessageComponent(data)) => {
|
||||
self.handle_message_component(data, interaction.clone())
|
||||
.await
|
||||
}
|
||||
_ => Ok(()),
|
||||
},
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_application_command(
|
||||
&self,
|
||||
command: InteractionCommand,
|
||||
interaction: Box<InteractionCreate>,
|
||||
) -> anyhow::Result<()> {
|
||||
{
|
||||
match command {
|
||||
InteractionCommand::Play(query) => {
|
||||
spawn(play(interaction, Arc::clone(&self.state), query))
|
||||
}
|
||||
InteractionCommand::Stop => {
|
||||
spawn(stop(interaction, Arc::clone(&self.state)))
|
||||
}
|
||||
InteractionCommand::Pause => {
|
||||
spawn(pause(interaction, Arc::clone(&self.state)))
|
||||
}
|
||||
InteractionCommand::Skip => {
|
||||
spawn(skip(interaction, Arc::clone(&self.state)))
|
||||
}
|
||||
InteractionCommand::Loop => {
|
||||
spawn(loop_queue(interaction, Arc::clone(&self.state)))
|
||||
}
|
||||
InteractionCommand::Resume => {
|
||||
spawn(resume(interaction, Arc::clone(&self.state)))
|
||||
}
|
||||
InteractionCommand::Leave => {
|
||||
spawn(leave(interaction, Arc::clone(&self.state)))
|
||||
}
|
||||
InteractionCommand::Join => {
|
||||
spawn(join(interaction, Arc::clone(&self.state)))
|
||||
}
|
||||
InteractionCommand::Queue => {
|
||||
spawn(queue(interaction, Arc::clone(&self.state)))
|
||||
}
|
||||
InteractionCommand::Stop => spawn(stop(interaction, Arc::clone(&self.state))),
|
||||
InteractionCommand::Pause => spawn(pause(interaction, Arc::clone(&self.state))),
|
||||
InteractionCommand::Skip => spawn(skip(interaction, Arc::clone(&self.state))),
|
||||
InteractionCommand::Loop => spawn(loop_queue(interaction, Arc::clone(&self.state))),
|
||||
InteractionCommand::Resume => spawn(resume(interaction, Arc::clone(&self.state))),
|
||||
InteractionCommand::Leave => spawn(leave(interaction, Arc::clone(&self.state))),
|
||||
InteractionCommand::Join => spawn(join(interaction, Arc::clone(&self.state))),
|
||||
InteractionCommand::Queue => spawn(queue(interaction, Arc::clone(&self.state))),
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Some(InteractionData::MessageComponent(data)) => {
|
||||
tracing::info!("message component: {:?}", data);
|
||||
}
|
||||
|
||||
async fn handle_message_component(
|
||||
&self,
|
||||
data: &MessageComponentInteractionData,
|
||||
interaction: Box<InteractionCreate>,
|
||||
) -> anyhow::Result<()> {
|
||||
if !data.custom_id.starts_with("page:") {
|
||||
return Ok(());
|
||||
}
|
||||
@@ -139,7 +105,6 @@ impl Handler {
|
||||
.trim_start_matches("page:")
|
||||
.parse::<usize>()
|
||||
.unwrap_or(0);
|
||||
tracing::info!("page: {:?}", page);
|
||||
|
||||
if let Some(guild_id) = interaction.guild_id {
|
||||
let mut queue = Vec::new();
|
||||
@@ -147,8 +112,10 @@ impl Handler {
|
||||
let call = call_lock.lock().await;
|
||||
queue = call.queue().current_queue();
|
||||
}
|
||||
let n_pages = (queue.len() + TRACKS_PER_PAGE - 1) / TRACKS_PER_PAGE;
|
||||
let page = page.min(n_pages - 1).max(0);
|
||||
let embeds = build_queue_embeds(&queue, page).await;
|
||||
let action_row = build_action_row(page, queue.len() / TRACKS_PER_PAGE);
|
||||
let action_row = build_action_row(page, n_pages);
|
||||
|
||||
let interaction_response_data = InteractionResponseDataBuilder::new()
|
||||
.embeds(embeds)
|
||||
@@ -168,39 +135,4 @@ impl Handler {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
event => {
|
||||
tracing::info!("unhandled event: {:?}", event);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_interaction_command(command: &CommandData) -> InteractionCommand {
|
||||
debug!("command: {:?}", command);
|
||||
match command.name.as_str() {
|
||||
"play" => {
|
||||
if let Some(query_option) = command.options.iter().find(|opt| opt.name == "query") {
|
||||
if let CommandOptionValue::String(query) = &query_option.value {
|
||||
InteractionCommand::Play(query.clone())
|
||||
} else {
|
||||
InteractionCommand::NotImplemented
|
||||
}
|
||||
} else {
|
||||
InteractionCommand::NotImplemented
|
||||
}
|
||||
}
|
||||
"stop" => InteractionCommand::Stop,
|
||||
"pause" => InteractionCommand::Pause,
|
||||
"skip" => InteractionCommand::Skip,
|
||||
"loop" => InteractionCommand::Loop,
|
||||
"resume" => InteractionCommand::Resume,
|
||||
"leave" => InteractionCommand::Leave,
|
||||
"join" => InteractionCommand::Join,
|
||||
"queue" => InteractionCommand::Queue,
|
||||
_ => InteractionCommand::NotImplemented,
|
||||
}
|
||||
}
|
||||
|
||||
44
src/interaction_commands.rs
Normal file
44
src/interaction_commands.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use twilight_model::application::interaction::application_command::{
|
||||
CommandData, CommandOptionValue,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum InteractionCommand {
|
||||
Play(String),
|
||||
Stop,
|
||||
Pause,
|
||||
Skip,
|
||||
Loop,
|
||||
Resume,
|
||||
Leave,
|
||||
Join,
|
||||
Queue,
|
||||
NotImplemented,
|
||||
}
|
||||
|
||||
impl From<Box<CommandData>> for InteractionCommand {
|
||||
fn from(command: Box<CommandData>) -> InteractionCommand {
|
||||
match command.name.as_str() {
|
||||
"play" => {
|
||||
if let Some(query_option) = command.options.iter().find(|opt| opt.name == "query") {
|
||||
if let CommandOptionValue::String(query) = &query_option.value {
|
||||
InteractionCommand::Play(query.clone())
|
||||
} else {
|
||||
InteractionCommand::NotImplemented
|
||||
}
|
||||
} else {
|
||||
InteractionCommand::NotImplemented
|
||||
}
|
||||
}
|
||||
"stop" => InteractionCommand::Stop,
|
||||
"pause" => InteractionCommand::Pause,
|
||||
"skip" => InteractionCommand::Skip,
|
||||
"loop" => InteractionCommand::Loop,
|
||||
"resume" => InteractionCommand::Resume,
|
||||
"leave" => InteractionCommand::Leave,
|
||||
"join" => InteractionCommand::Join,
|
||||
"queue" => InteractionCommand::Queue,
|
||||
_ => InteractionCommand::NotImplemented,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,11 @@ mod handler;
|
||||
use handler::Handler;
|
||||
mod colors;
|
||||
mod commands;
|
||||
mod interaction_commands;
|
||||
mod metadata;
|
||||
mod signal;
|
||||
mod state;
|
||||
mod utils;
|
||||
|
||||
use crate::commands::get_chat_commands;
|
||||
use dotenv::dotenv;
|
||||
|
||||
12
src/utils.rs
Normal file
12
src/utils.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use futures::Future;
|
||||
use std::error::Error;
|
||||
|
||||
pub(crate) fn spawn(
|
||||
fut: impl Future<Output = Result<(), Box<dyn Error + Send + Sync + 'static>>> + Send + 'static,
|
||||
) {
|
||||
tokio::spawn(async move {
|
||||
if let Err(why) = fut.await {
|
||||
tracing::debug!("handler error: {:?}", why);
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user