split main.rs into files
This commit is contained in:
31
src/commands/join.rs
Normal file
31
src/commands/join.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use std::{error::Error, num::NonZeroU64};
|
||||
use twilight_model::channel::Message;
|
||||
|
||||
use crate::state::State;
|
||||
|
||||
pub(crate) async fn join(
|
||||
msg: Message,
|
||||
state: State,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||
let user_id = msg.author.id;
|
||||
let guild_id = msg.guild_id.ok_or("No guild id attached to the message.")?;
|
||||
let channel_id = state
|
||||
.cache
|
||||
.voice_state(user_id, guild_id)
|
||||
.ok_or("Cannot get voice state for user")?
|
||||
.channel_id();
|
||||
let channel_id =
|
||||
NonZeroU64::new(channel_id.into()).ok_or("Joined voice channel must have nonzero ID.")?;
|
||||
state
|
||||
.songbird
|
||||
.join(guild_id, channel_id)
|
||||
.await
|
||||
.map_err(|e| format!("Could not join voice channel: {:?}", e))?;
|
||||
|
||||
// signal that we are not listening
|
||||
if let Some(call_lock) = state.songbird.get(guild_id) {
|
||||
let mut call = call_lock.lock().await;
|
||||
call.deafen(true).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
17
src/commands/leave.rs
Normal file
17
src/commands/leave.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use crate::state::State;
|
||||
use std::error::Error;
|
||||
use twilight_model::channel::Message;
|
||||
|
||||
pub(crate) async fn leave(
|
||||
msg: Message,
|
||||
state: State,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||
tracing::debug!(
|
||||
"leave command in channel {} by {}",
|
||||
msg.channel_id,
|
||||
msg.author.name
|
||||
);
|
||||
let guild_id = msg.guild_id.unwrap();
|
||||
state.songbird.leave(guild_id).await?;
|
||||
Ok(())
|
||||
}
|
||||
20
src/commands/mod.rs
Normal file
20
src/commands/mod.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
mod join;
|
||||
pub(crate) use join::join;
|
||||
|
||||
mod leave;
|
||||
pub(crate) use leave::leave;
|
||||
|
||||
mod pause;
|
||||
pub(crate) use pause::pause;
|
||||
|
||||
mod play;
|
||||
pub(crate) use play::play;
|
||||
|
||||
mod queue;
|
||||
pub(crate) use queue::queue;
|
||||
|
||||
mod resume;
|
||||
pub(crate) use resume::resume;
|
||||
|
||||
mod stop;
|
||||
pub(crate) use stop::stop;
|
||||
29
src/commands/pause.rs
Normal file
29
src/commands/pause.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use crate::state::State;
|
||||
use std::error::Error;
|
||||
use twilight_model::channel::Message;
|
||||
|
||||
pub(crate) async fn pause(
|
||||
msg: Message,
|
||||
state: State,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||
tracing::debug!(
|
||||
"pause command in channel {} by {}",
|
||||
msg.channel_id,
|
||||
msg.author.name
|
||||
);
|
||||
|
||||
let guild_id = msg.guild_id.unwrap();
|
||||
|
||||
if let Some(call_lock) = state.songbird.get(guild_id) {
|
||||
let call = call_lock.lock().await;
|
||||
call.queue().pause()?;
|
||||
}
|
||||
|
||||
state
|
||||
.http
|
||||
.create_message(msg.channel_id)
|
||||
.content("Paused the track")?
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
101
src/commands/play.rs
Normal file
101
src/commands/play.rs
Normal file
@@ -0,0 +1,101 @@
|
||||
use crate::commands::join;
|
||||
use crate::metadata::{Metadata, MetadataMap};
|
||||
use crate::state::State;
|
||||
use serde_json::Value;
|
||||
use songbird::input::{Compose, YoutubeDl};
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::{error::Error, ops::Sub, time::Duration};
|
||||
use tokio::process::Command;
|
||||
use tracing::debug;
|
||||
use twilight_model::channel::Message;
|
||||
use url::Url;
|
||||
|
||||
pub(crate) async fn play(
|
||||
msg: Message,
|
||||
state: State,
|
||||
query: String,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||
tracing::debug!(
|
||||
"play command in channel {} by {}",
|
||||
msg.channel_id,
|
||||
msg.author.name
|
||||
);
|
||||
|
||||
join(msg.clone(), state.clone()).await?;
|
||||
|
||||
let guild_id = msg.guild_id.unwrap();
|
||||
|
||||
// handle keyword queries
|
||||
let query = if Url::parse(&query).is_err() {
|
||||
format!("ytsearch:{query}")
|
||||
} else {
|
||||
query
|
||||
};
|
||||
|
||||
// handle playlist links
|
||||
let urls = if query.contains("list=") {
|
||||
get_playlist_urls(query).await?
|
||||
} else {
|
||||
vec![query]
|
||||
};
|
||||
|
||||
for url in urls {
|
||||
let mut src = YoutubeDl::new(reqwest::Client::new(), url.to_string());
|
||||
if let Ok(metadata) = src.aux_metadata().await {
|
||||
debug!("metadata: {:?}", metadata);
|
||||
|
||||
if let Some(call_lock) = state.songbird.get(guild_id) {
|
||||
let mut call = call_lock.lock().await;
|
||||
let handle = call.enqueue_with_preload(
|
||||
src.into(),
|
||||
metadata.duration.map(|duration| -> Duration {
|
||||
if duration.as_secs() > 5 {
|
||||
duration.sub(Duration::from_secs(5))
|
||||
} else {
|
||||
duration
|
||||
}
|
||||
}),
|
||||
);
|
||||
let mut x = handle.typemap().write().await;
|
||||
x.insert::<MetadataMap>(Metadata {
|
||||
title: metadata.title,
|
||||
duration: metadata.duration,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
state
|
||||
.http
|
||||
.create_message(msg.channel_id)
|
||||
.content("Cannot find any results")?
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_playlist_urls(
|
||||
url: String,
|
||||
) -> Result<Vec<String>, Box<dyn Error + Send + Sync + 'static>> {
|
||||
let output = Command::new("yt-dlp")
|
||||
.args(vec![&url, "--flat-playlist", "-j"])
|
||||
.output()
|
||||
.await?;
|
||||
|
||||
let reader = BufReader::new(output.stdout.as_slice());
|
||||
let urls = reader
|
||||
.lines()
|
||||
.flatten()
|
||||
.map(|line| {
|
||||
let entry: Value = serde_json::from_str(&line).unwrap();
|
||||
entry
|
||||
.get("webpage_url")
|
||||
.unwrap()
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(urls)
|
||||
}
|
||||
58
src/commands/queue.rs
Normal file
58
src/commands/queue.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
use crate::{metadata::MetadataMap, state::State};
|
||||
use std::error::Error;
|
||||
use twilight_model::channel::Message;
|
||||
|
||||
pub(crate) async fn queue(
|
||||
msg: Message,
|
||||
state: State,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||
tracing::debug!(
|
||||
"queue command in channel {} by {}",
|
||||
msg.channel_id,
|
||||
msg.author.name
|
||||
);
|
||||
let guild_id = msg.guild_id.unwrap();
|
||||
|
||||
if let Some(call_lock) = state.songbird.get(guild_id) {
|
||||
let call = call_lock.lock().await;
|
||||
let queue = call.queue().current_queue();
|
||||
let mut message = String::new();
|
||||
if queue.is_empty() {
|
||||
message.push_str("There are no tracks in the queue.\n");
|
||||
} else {
|
||||
message.push_str("Currently playing:\n");
|
||||
}
|
||||
for track in queue {
|
||||
let map = track.typemap().read().await;
|
||||
let metadata = map.get::<MetadataMap>().unwrap();
|
||||
message.push_str(
|
||||
format!(
|
||||
"* `{}",
|
||||
metadata.title.clone().unwrap_or("Unknown".to_string()),
|
||||
)
|
||||
.as_str(),
|
||||
);
|
||||
if let Some(duration) = metadata.duration {
|
||||
let res = duration.as_secs();
|
||||
let hours = res / (60 * 60);
|
||||
let res = res - hours * 60 * 60;
|
||||
let minutes = res / 60;
|
||||
let res = res - minutes * 60;
|
||||
let seconds = res;
|
||||
message.push_str(" (");
|
||||
if hours > 0 {
|
||||
message.push_str(format!("{:02}:", hours).as_str());
|
||||
}
|
||||
message.push_str(format!("{:02}:{:02}", minutes, seconds).as_str());
|
||||
message.push(')');
|
||||
}
|
||||
message.push_str("`\n");
|
||||
}
|
||||
state
|
||||
.http
|
||||
.create_message(msg.channel_id)
|
||||
.content(&message)?
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
29
src/commands/resume.rs
Normal file
29
src/commands/resume.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use crate::state::State;
|
||||
use std::error::Error;
|
||||
use twilight_model::channel::Message;
|
||||
|
||||
pub(crate) async fn resume(
|
||||
msg: Message,
|
||||
state: State,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||
tracing::debug!(
|
||||
"resume command in channel {} by {}",
|
||||
msg.channel_id,
|
||||
msg.author.name
|
||||
);
|
||||
|
||||
let guild_id = msg.guild_id.unwrap();
|
||||
|
||||
if let Some(call_lock) = state.songbird.get(guild_id) {
|
||||
let call = call_lock.lock().await;
|
||||
call.queue().resume()?;
|
||||
}
|
||||
|
||||
state
|
||||
.http
|
||||
.create_message(msg.channel_id)
|
||||
.content("Resumed the track")?
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
29
src/commands/stop.rs
Normal file
29
src/commands/stop.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use crate::state::State;
|
||||
use std::error::Error;
|
||||
use twilight_model::channel::Message;
|
||||
|
||||
pub(crate) async fn stop(
|
||||
msg: Message,
|
||||
state: State,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||
tracing::debug!(
|
||||
"stop command in channel {} by {}",
|
||||
msg.channel_id,
|
||||
msg.author.name
|
||||
);
|
||||
|
||||
let guild_id = msg.guild_id.unwrap();
|
||||
|
||||
if let Some(call_lock) = state.songbird.get(guild_id) {
|
||||
let call = call_lock.lock().await;
|
||||
call.queue().stop();
|
||||
}
|
||||
|
||||
state
|
||||
.http
|
||||
.create_message(msg.channel_id)
|
||||
.content("Stopped the track and cleared the queue")?
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user