music commands plus activity

This commit is contained in:
2024-01-30 18:47:51 +01:00
parent c997e30186
commit 6ae7ba2233
10 changed files with 146 additions and 34 deletions

View File

@@ -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<Event> {
if let EventContext::Track(track_list) = ctx {
for (state, handle) in *track_list {
println!(
"Track {:?} had an error: {:?}",
handle.uuid(),
state.playing
);
}
}
None
}
}
}

View File

@@ -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<Event> {
if let EventContext::Track(track_list) = ctx {
for (state, handle) in *track_list {
println!(
"Track {:?} had an error: {:?}",
handle.uuid(),
state.playing
);
}
}
None
}
}

View File

@@ -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;

View File

@@ -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::<String>() {
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::<HttpKey>()
@@ -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;

View File

@@ -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(())
}

View File

@@ -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(())
}

View File

@@ -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(())
}