split main.rs into files
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -416,6 +416,7 @@ dependencies = [
|
|||||||
"twilight-model",
|
"twilight-model",
|
||||||
"twilight-standby",
|
"twilight-standby",
|
||||||
"twilight-util",
|
"twilight-util",
|
||||||
|
"url",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -22,3 +22,4 @@ twilight-cache-inmemory = "0.15"
|
|||||||
twilight-util = { version = "0.15", features=["builder"] }
|
twilight-util = { version = "0.15", features=["builder"] }
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
url = "2.5.0"
|
||||||
|
|||||||
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(())
|
||||||
|
}
|
||||||
76
src/handler.rs
Normal file
76
src/handler.rs
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
use crate::commands::{join, leave, pause, play, queue, resume, stop};
|
||||||
|
use crate::state::State;
|
||||||
|
|
||||||
|
use futures::Future;
|
||||||
|
use std::error::Error;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use twilight_gateway::Event;
|
||||||
|
use twilight_model::channel::Message;
|
||||||
|
|
||||||
|
enum ChatCommand {
|
||||||
|
Play(Message, String),
|
||||||
|
Stop(Message),
|
||||||
|
Pause(Message),
|
||||||
|
Resume(Message),
|
||||||
|
Leave(Message),
|
||||||
|
Join(Message),
|
||||||
|
Queue(Message),
|
||||||
|
NotImplemented,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_command(event: Event) -> Option<ChatCommand> {
|
||||||
|
match event {
|
||||||
|
Event::MessageCreate(msg_create) => {
|
||||||
|
if msg_create.guild_id.is_none() || !msg_create.content.starts_with('!') {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let split: Vec<&str> = msg_create.content.splitn(2, ' ').collect();
|
||||||
|
match split.as_slice() {
|
||||||
|
["!play", query] => {
|
||||||
|
Some(ChatCommand::Play(msg_create.0.clone(), query.to_string()))
|
||||||
|
}
|
||||||
|
["!stop"] | ["!stop", _] => Some(ChatCommand::Stop(msg_create.0)),
|
||||||
|
["!pause"] | ["!pause", _] => Some(ChatCommand::Pause(msg_create.0)),
|
||||||
|
["!resume"] | ["!resume", _] => Some(ChatCommand::Resume(msg_create.0)),
|
||||||
|
["!leave"] | ["!leave", _] => Some(ChatCommand::Leave(msg_create.0)),
|
||||||
|
["!join"] | ["!join", _] => Some(ChatCommand::Join(msg_create.0)),
|
||||||
|
["!queue"] | ["!queue", _] => Some(ChatCommand::Queue(msg_create.0)),
|
||||||
|
_ => Some(ChatCommand::NotImplemented),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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) struct Handler {
|
||||||
|
state: State,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Handler {
|
||||||
|
pub(crate) fn new(state: State) -> Self {
|
||||||
|
Self { state }
|
||||||
|
}
|
||||||
|
pub(crate) async fn act(&mut self, event: Event) {
|
||||||
|
match parse_command(event) {
|
||||||
|
Some(ChatCommand::Play(msg, query)) => spawn(play(msg, Arc::clone(&self.state), query)),
|
||||||
|
Some(ChatCommand::Stop(msg)) => spawn(stop(msg, Arc::clone(&self.state))),
|
||||||
|
Some(ChatCommand::Pause(msg)) => spawn(pause(msg, Arc::clone(&self.state))),
|
||||||
|
Some(ChatCommand::Resume(msg)) => spawn(resume(msg, Arc::clone(&self.state))),
|
||||||
|
Some(ChatCommand::Leave(msg)) => spawn(leave(msg, Arc::clone(&self.state))),
|
||||||
|
Some(ChatCommand::Join(msg)) => spawn(join(msg, Arc::clone(&self.state))),
|
||||||
|
Some(ChatCommand::Queue(msg)) => spawn(queue(msg, Arc::clone(&self.state))),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
335
src/main.rs
335
src/main.rs
@@ -1,95 +1,30 @@
|
|||||||
|
mod handler;
|
||||||
|
use handler::Handler;
|
||||||
|
mod commands;
|
||||||
|
mod metadata;
|
||||||
|
mod state;
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use serde_json::Value;
|
use songbird::{shards::TwilightMap, Songbird};
|
||||||
use songbird::{
|
use state::StateRef;
|
||||||
input::{Compose, YoutubeDl},
|
use std::{env, error::Error, sync::Arc};
|
||||||
shards::TwilightMap,
|
use tokio::{
|
||||||
typemap::TypeMapKey,
|
select,
|
||||||
Songbird,
|
signal::unix::{signal, SignalKind},
|
||||||
|
sync::watch,
|
||||||
};
|
};
|
||||||
use std::{
|
use tracing::{debug, info};
|
||||||
env,
|
|
||||||
error::Error,
|
|
||||||
future::Future,
|
|
||||||
io::{BufRead, BufReader},
|
|
||||||
num::NonZeroU64,
|
|
||||||
ops::Sub,
|
|
||||||
sync::Arc,
|
|
||||||
time::Duration,
|
|
||||||
};
|
|
||||||
use tokio::process::Command;
|
|
||||||
use tracing::debug;
|
|
||||||
use twilight_cache_inmemory::InMemoryCache;
|
use twilight_cache_inmemory::InMemoryCache;
|
||||||
use twilight_gateway::{
|
use twilight_gateway::{
|
||||||
stream::{self, ShardEventStream},
|
stream::{self, ShardEventStream},
|
||||||
Event, Intents, Shard,
|
Intents, Shard,
|
||||||
};
|
};
|
||||||
use twilight_http::Client as HttpClient;
|
use twilight_http::Client as HttpClient;
|
||||||
use twilight_model::application::command::CommandType;
|
use twilight_model::application::command::CommandType;
|
||||||
use twilight_model::{channel::Message, id::Id};
|
use twilight_model::id::Id;
|
||||||
use twilight_standby::Standby;
|
use twilight_standby::Standby;
|
||||||
use twilight_util::builder::command::{CommandBuilder, StringBuilder};
|
use twilight_util::builder::command::{CommandBuilder, StringBuilder};
|
||||||
|
|
||||||
type State = Arc<StateRef>;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct StateRef {
|
|
||||||
http: HttpClient,
|
|
||||||
cache: InMemoryCache,
|
|
||||||
songbird: Songbird,
|
|
||||||
standby: Standby,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Metadata {
|
|
||||||
title: Option<String>,
|
|
||||||
artist: Option<String>,
|
|
||||||
}
|
|
||||||
struct MetadataMap;
|
|
||||||
impl TypeMapKey for MetadataMap {
|
|
||||||
type Value = Metadata;
|
|
||||||
}
|
|
||||||
|
|
||||||
enum ChatCommand {
|
|
||||||
Play(Message, String),
|
|
||||||
Stop(Message),
|
|
||||||
Leave(Message),
|
|
||||||
Join(Message),
|
|
||||||
Queue(Message),
|
|
||||||
NotImplemented,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_command(event: Event) -> Option<ChatCommand> {
|
|
||||||
match event {
|
|
||||||
Event::MessageCreate(msg_create) => {
|
|
||||||
if msg_create.guild_id.is_none() || !msg_create.content.starts_with('!') {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
let split: Vec<&str> = msg_create.content.splitn(2, ' ').collect();
|
|
||||||
match split.as_slice() {
|
|
||||||
["!play", query] => {
|
|
||||||
Some(ChatCommand::Play(msg_create.0.clone(), query.to_string()))
|
|
||||||
}
|
|
||||||
["!stop"] | ["!stop", _] => Some(ChatCommand::Stop(msg_create.0)),
|
|
||||||
["!leave"] | ["!leave", _] => Some(ChatCommand::Leave(msg_create.0)),
|
|
||||||
["!join"] | ["!join", _] => Some(ChatCommand::Join(msg_create.0)),
|
|
||||||
["!queue"] | ["!queue", _] => Some(ChatCommand::Queue(msg_create.0)),
|
|
||||||
_ => Some(ChatCommand::NotImplemented),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
@@ -97,6 +32,20 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
|||||||
// Initialize the tracing subscriber.
|
// Initialize the tracing subscriber.
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
|
let (stop_tx, mut stop_rx) = watch::channel(());
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let mut sigterm = signal(SignalKind::terminate()).unwrap();
|
||||||
|
let mut sigint = signal(SignalKind::interrupt()).unwrap();
|
||||||
|
loop {
|
||||||
|
select! {
|
||||||
|
_ = sigterm.recv() => println!("Receive SIGTERM"),
|
||||||
|
_ = sigint.recv() => println!("Receive SIGTERM"),
|
||||||
|
};
|
||||||
|
stop_tx.send(()).unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
let (mut shards, state) = {
|
let (mut shards, state) = {
|
||||||
let token = env::var("DISCORD_TOKEN")?;
|
let token = env::var("DISCORD_TOKEN")?;
|
||||||
let app_id = env::var("DISCORD_APP_ID")?.parse()?;
|
let app_id = env::var("DISCORD_APP_ID")?.parse()?;
|
||||||
@@ -148,209 +97,43 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
|||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut handler = Handler::new(Arc::clone(&state));
|
||||||
let mut stream = ShardEventStream::new(shards.iter_mut());
|
let mut stream = ShardEventStream::new(shards.iter_mut());
|
||||||
loop {
|
loop {
|
||||||
let event = match stream.next().await {
|
select! {
|
||||||
Some((_, Ok(event))) => event,
|
biased;
|
||||||
Some((_, Err(source))) => {
|
_ = stop_rx.changed() => {
|
||||||
tracing::warn!(?source, "error receiving event");
|
for guild in state.cache.iter().guilds(){
|
||||||
|
info!("Leaving guild {:?}", guild.id());
|
||||||
if source.is_fatal() {
|
state.songbird.leave(guild.id()).await?;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
// need to grab next event to properly leave voice channels
|
||||||
|
stream.next().await;
|
||||||
|
break;
|
||||||
|
},
|
||||||
|
next = stream.next() => {
|
||||||
|
let event = match next {
|
||||||
|
Some((_, Ok(event))) => event,
|
||||||
|
Some((_, Err(source))) => {
|
||||||
|
tracing::warn!(?source, "error receiving event");
|
||||||
|
|
||||||
continue;
|
if source.is_fatal() {
|
||||||
}
|
break;
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
debug!("Event: {:?}", &event);
|
|
||||||
|
|
||||||
state.cache.update(&event);
|
|
||||||
state.standby.process(&event);
|
|
||||||
state.songbird.process(&event).await;
|
|
||||||
|
|
||||||
match parse_command(event) {
|
|
||||||
Some(ChatCommand::Play(msg, query)) => spawn(play(msg, Arc::clone(&state), query)),
|
|
||||||
Some(ChatCommand::Stop(msg)) => spawn(stop(msg, Arc::clone(&state))),
|
|
||||||
Some(ChatCommand::Leave(msg)) => spawn(leave(msg, Arc::clone(&state))),
|
|
||||||
Some(ChatCommand::Join(msg)) => spawn(join(msg, Arc::clone(&state))),
|
|
||||||
Some(ChatCommand::Queue(msg)) => spawn(queue(msg, Arc::clone(&state))),
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
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(())
|
|
||||||
}
|
|
||||||
|
|
||||||
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(())
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
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!(
|
|
||||||
"* {}\n",
|
|
||||||
metadata.title.clone().unwrap_or("Unknown".to_string()),
|
|
||||||
)
|
|
||||||
.as_str(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
state
|
|
||||||
.http
|
|
||||||
.create_message(msg.channel_id)
|
|
||||||
.content(&message)?
|
|
||||||
.await?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}),
|
|
||||||
);
|
continue;
|
||||||
let mut x = handle.typemap().write().await;
|
}
|
||||||
x.insert::<MetadataMap>(Metadata {
|
None => break,
|
||||||
title: metadata.title,
|
};
|
||||||
artist: metadata.artist,
|
debug!("Event: {:?}", &event);
|
||||||
});
|
|
||||||
|
state.cache.update(&event);
|
||||||
|
state.standby.process(&event);
|
||||||
|
state.songbird.process(&event).await;
|
||||||
|
|
||||||
|
handler.act(event).await;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
state
|
|
||||||
.http
|
|
||||||
.create_message(msg.channel_id)
|
|
||||||
.content("Cannot find any results")?
|
|
||||||
.await?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
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 mut call = call_lock.lock().await;
|
|
||||||
call.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
state
|
|
||||||
.http
|
|
||||||
.create_message(msg.channel_id)
|
|
||||||
.content("Stopped the track")?
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
11
src/metadata.rs
Normal file
11
src/metadata.rs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
use songbird::typemap::TypeMapKey;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
pub(crate) struct Metadata {
|
||||||
|
pub(crate) title: Option<String>,
|
||||||
|
pub(crate) duration: Option<Duration>,
|
||||||
|
}
|
||||||
|
pub(crate) struct MetadataMap;
|
||||||
|
impl TypeMapKey for MetadataMap {
|
||||||
|
type Value = Metadata;
|
||||||
|
}
|
||||||
15
src/state.rs
Normal file
15
src/state.rs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
use songbird::Songbird;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use twilight_cache_inmemory::InMemoryCache;
|
||||||
|
use twilight_http::Client as HttpClient;
|
||||||
|
use twilight_standby::Standby;
|
||||||
|
|
||||||
|
pub(crate) type State = Arc<StateRef>;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(crate) struct StateRef {
|
||||||
|
pub(crate) http: HttpClient,
|
||||||
|
pub(crate) cache: InMemoryCache,
|
||||||
|
pub(crate) songbird: Songbird,
|
||||||
|
pub(crate) standby: Standby,
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user