diff --git a/Cargo.lock b/Cargo.lock index 0762df5..1fa0360 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -895,6 +895,7 @@ name = "lyra" version = "0.1.0" dependencies = [ "dotenv", + "regex", "reqwest", "serenity", "songbird", diff --git a/Cargo.toml b/Cargo.toml index 1c82562..69ddc17 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] dotenv = "0.15.0" +regex = "1.10.3" reqwest = "0.11.23" serenity = "0.12.0" songbird = { version = "0.4.0", features = ["builtin-queue", "serenity"] } diff --git a/src/commands/music/join.rs b/src/commands/music/join.rs index fe734aa..18d1fb4 100644 --- a/src/commands/music/join.rs +++ b/src/commands/music/join.rs @@ -1,11 +1,10 @@ -use serenity::async_trait; use serenity::framework::standard::macros::command; use serenity::framework::standard::CommandResult; use serenity::model::prelude::*; use serenity::prelude::*; -use songbird::events::{Event, EventContext, EventHandler as VoiceEventHandler, TrackEvent}; +use songbird::events::TrackEvent; -use crate::commands::misc::check_msg; +use crate::commands::{misc::check_msg, music::misc::TrackErrorNotifier}; #[command] #[only_in(guilds)] @@ -40,22 +39,4 @@ async fn join(ctx: &Context, msg: &Message) -> CommandResult { } Ok(()) -} - -struct TrackErrorNotifier; - -#[async_trait] -impl VoiceEventHandler for TrackErrorNotifier { - async fn act(&self, ctx: &EventContext<'_>) -> Option { - if let EventContext::Track(track_list) = ctx { - for (state, handle) in *track_list { - println!( - "Track {:?} had an error: {:?}", - handle.uuid(), - state.playing - ); - } - } - None - } -} +} \ No newline at end of file diff --git a/src/commands/music/misc.rs b/src/commands/music/misc.rs new file mode 100644 index 0000000..c9ff161 --- /dev/null +++ b/src/commands/music/misc.rs @@ -0,0 +1,20 @@ +use serenity::async_trait; +use songbird::events::{Event, EventContext, EventHandler as VoiceEventHandler}; + +pub struct TrackErrorNotifier; + +#[async_trait] +impl VoiceEventHandler for TrackErrorNotifier { + async fn act(&self, ctx: &EventContext<'_>) -> Option { + if let EventContext::Track(track_list) = ctx { + for (state, handle) in *track_list { + println!( + "Track {:?} had an error: {:?}", + handle.uuid(), + state.playing + ); + } + } + None + } +} \ No newline at end of file diff --git a/src/commands/music/mod.rs b/src/commands/music/mod.rs index 73e0309..4a2185a 100644 --- a/src/commands/music/mod.rs +++ b/src/commands/music/mod.rs @@ -1,7 +1,11 @@ pub mod deafen; pub mod join; pub mod leave; +pub mod misc; pub mod mute; pub mod play; +pub mod queue; +pub mod skip; +pub mod stop; pub mod undeafen; pub mod unmute; diff --git a/src/commands/music/play.rs b/src/commands/music/play.rs index af8e02e..a7bbbb0 100644 --- a/src/commands/music/play.rs +++ b/src/commands/music/play.rs @@ -1,8 +1,5 @@ use serenity::framework::standard::Args; -use serenity::framework::standard::{ - macros::command, - CommandResult, -}; +use serenity::framework::standard::{macros::command, CommandResult}; use reqwest::Client as HttpClient; @@ -13,8 +10,10 @@ use serenity::prelude::TypeMapKey; use serenity::model::prelude::*; use songbird::input::YoutubeDl; +use songbird::TrackEvent; use crate::commands::misc::check_msg; +use crate::commands::music::misc::TrackErrorNotifier; pub struct HttpKey; @@ -25,6 +24,25 @@ impl TypeMapKey for HttpKey { #[command] #[only_in(guilds)] async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { + let (guild_id, channel_id) = { + let guild = msg.guild(&ctx.cache).unwrap(); + let channel_id = guild + .voice_states + .get(&msg.author.id) + .and_then(|voice_state| voice_state.channel_id); + + (guild.id, channel_id) + }; + + let connect_to = match channel_id { + Some(channel) => channel, + None => { + check_msg(msg.reply(ctx, "Not in a voice channel").await); + + return Ok(()); + } + }; + let url = match args.single::() { Ok(url) => url, Err(_) => { @@ -40,8 +58,6 @@ async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { let do_search = !url.starts_with("http"); - let guild_id = msg.guild_id.unwrap(); - let http_client = { let data = ctx.data.read().await; data.get::() @@ -54,6 +70,11 @@ async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { .expect("Client placed at init") .clone(); + if let Ok(handler_lock) = manager.join(guild_id, connect_to).await { + let mut handler = handler_lock.lock().await; + handler.add_global_event(TrackEvent::Error.into(), TrackErrorNotifier); + } + if let Some(handler_lock) = manager.get(guild_id) { let mut handler = handler_lock.lock().await; diff --git a/src/commands/music/queue.rs b/src/commands/music/queue.rs new file mode 100644 index 0000000..7b58e2d --- /dev/null +++ b/src/commands/music/queue.rs @@ -0,0 +1,12 @@ +use serenity::framework::standard::macros::command; +use serenity::framework::standard::CommandResult; +use serenity::model::prelude::*; +use serenity::prelude::*; + +use crate::commands::misc::check_msg; + +#[command] +#[only_in(guilds)] +async fn queue(ctx: &Context, msg: &Message) -> CommandResult { + Ok(()) +} \ No newline at end of file diff --git a/src/commands/music/skip.rs b/src/commands/music/skip.rs new file mode 100644 index 0000000..0bf5cce --- /dev/null +++ b/src/commands/music/skip.rs @@ -0,0 +1,12 @@ +use serenity::framework::standard::macros::command; +use serenity::framework::standard::CommandResult; +use serenity::model::prelude::*; +use serenity::prelude::*; + +use crate::commands::misc::check_msg; + +#[command] +#[only_in(guilds)] +async fn skip(ctx: &Context, msg: &Message) -> CommandResult { + Ok(()) +} \ No newline at end of file diff --git a/src/commands/music/stop.rs b/src/commands/music/stop.rs new file mode 100644 index 0000000..31eb114 --- /dev/null +++ b/src/commands/music/stop.rs @@ -0,0 +1,40 @@ +use serenity::framework::standard::macros::command; +use serenity::framework::standard::CommandResult; +use serenity::model::prelude::*; +use serenity::prelude::*; + +use crate::commands::misc::check_msg; + +#[command] +#[only_in(guilds)] +async fn stop(ctx: &Context, msg: &Message) -> CommandResult { + let guild_id = msg.guild_id.unwrap(); + + let manager = songbird::get(ctx) + .await + .expect("Client placed at init") + .clone(); + + if let Some(handler_lock) = manager.get(guild_id) { + let mut handler = handler_lock.lock().await; + let queue = handler.queue(); + + if let Err(e) = handler.deafen(false).await { + check_msg( + msg.channel_id + .say(&ctx.http, format!("Failed: {:?}", e)) + .await, + ); + } + + check_msg(msg.channel_id.say(&ctx.http, "Playback stopped!").await); + } else { + check_msg( + msg.channel_id + .say(&ctx.http, "Not in a voice channel to undeafen in") + .await, + ); + } + + Ok(()) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 431fd00..b3f1376 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use serenity::gateway::ActivityData; +use serenity::model::prelude::Message; use songbird::SerenityInit; use reqwest::Client as HttpClient; @@ -8,13 +10,15 @@ use serenity::{ async_trait, client::{Client, EventHandler}, framework::{ - standard::{macros::group, Configuration}, + standard::{macros::group, macros::hook, Configuration}, StandardFramework, }, model::gateway::Ready, prelude::GatewayIntents, }; +use tracing::{info, instrument}; + mod commands; // music management commands @@ -23,6 +27,9 @@ use crate::commands::music::join::*; use crate::commands::music::leave::*; use crate::commands::music::mute::*; use crate::commands::music::play::*; +use crate::commands::music::queue::*; +use crate::commands::music::skip::*; +use crate::commands::music::stop::*; use crate::commands::music::undeafen::*; use crate::commands::music::unmute::*; @@ -36,13 +43,26 @@ struct Handler; #[async_trait] impl EventHandler for Handler { - async fn ready(&self, _: Context, ready: Ready) { - println!("{} is connected!", ready.user.name); + async fn ready(&self, ctx: Context, ready: Ready) { + info!("{} [{}] connected successfully!", ready.user.name, ready.user.id); + let prefix = std::env::var("PREFIX").expect("Environment variable `PREFIX` not found!"); + ctx.set_activity(Some(ActivityData::listening(prefix + "help"))); } } +#[hook] +async fn before(_: &Context, msg: &Message, command_name: &str) -> bool { + info!( + "Received command [{}] from user [{}]", + command_name, msg.author.name + ); + true +} + #[group] -#[commands(join, deafen, leave, mute, play, unmute, undeafen, ping, kashi)] +#[commands( + join, deafen, leave, mute, play, unmute, undeafen, ping, kashi, queue, stop, skip +)] struct General; #[tokio::main] @@ -54,15 +74,15 @@ async fn main() { std::env::var("DISCORD_TOKEN").expect("Environment variable `DISCORD_TOKEN` not found!"); let prefix = std::env::var("PREFIX").expect("Environment variable `PREFIX` not found!"); - let framework = StandardFramework::new().group(&GENERAL_GROUP); + let framework = StandardFramework::new().before(before).group(&GENERAL_GROUP); framework.configure(Configuration::new().prefix(prefix)); let intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT; let mut client = Client::builder(&token, intents) - .event_handler(Handler) .framework(framework) .register_songbird() + .event_handler(Handler) .type_map_insert::(HttpClient::new()) .await .expect("Error creating client");