songbird music support init

This commit is contained in:
2024-01-30 15:17:36 +01:00
parent edcf79cf8c
commit a5a9779cb9
9 changed files with 3226 additions and 4 deletions

8
src/commands/misc.rs Normal file
View File

@@ -0,0 +1,8 @@
use serenity::model::channel::Message;
use serenity::Result as SerenityResult;
pub fn check_msg(result: SerenityResult<Message>) {
if let Err(why) = result {
println!("Error sending message: {:?}", why);
}
}

2
src/commands/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod music;
pub mod misc;

View File

@@ -0,0 +1,41 @@
use serenity::model::prelude::*;
use serenity::prelude::*;
use serenity::framework::standard::macros::command;
use serenity::framework::standard::CommandResult;
use crate::commands::misc::check_msg;
#[command]
#[only_in(guilds)]
async fn deafen(ctx: &Context, msg: &Message) -> CommandResult {
let guild_id = msg.guild_id.unwrap();
let manager = songbird::get(ctx)
.await
.expect("Client placed at init")
.clone();
let handler_lock = match manager.get(guild_id) {
Some(handler) => handler,
None => {
check_msg(msg.reply(ctx, "Not in a voice channel").await);
return Ok(())
}
};
let mut handler = handler_lock.lock().await;
if handler.is_deaf() {
check_msg(msg.channel_id.say(&ctx.http, "Already deafened").await);
}
else {
if let Err(err) = handler.deafen(true).await {
check_msg(msg.channel_id.say(&ctx.http, format!("Failed: {:?}", err)).await);
}
check_msg(msg.channel_id.say(&ctx.http, "Deafened").await);
}
Ok(())
}

View File

@@ -0,0 +1,61 @@
use serenity::async_trait;
use serenity::model::prelude::*;
use serenity::prelude::*;
use serenity::framework::standard::macros::command;
use serenity::framework::standard::CommandResult;
use songbird::events::{Event, EventContext, EventHandler as VoiceEventHandler, TrackEvent};
use crate::commands::misc::check_msg;
#[command]
#[only_in(guilds)]
async fn join(ctx: &Context, msg: &Message) -> 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 manager = songbird::get(ctx)
.await
.expect("Songbird Voice 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);
}
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,32 @@
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 leave(ctx: &Context, msg: &Message) -> CommandResult {
let guild_id = msg.guild_id.unwrap();
let manager = songbird::get(ctx)
.await
.expect("Client placed in at init.")
.clone();
let has_handler = manager.get(guild_id).is_some();
if has_handler {
if let Err(err) = manager.remove(guild_id).await {
check_msg(msg.channel_id.say(&ctx.http, format!("Failed: {:?}", err)).await);
}
check_msg(msg.channel_id.say(&ctx.http, "Left voice channel").await);
}
else {
check_msg(msg.reply(ctx, "Not in a voice channel").await);
}
Ok(())
}

View File

@@ -0,0 +1,3 @@
pub mod join;
pub mod deafen;
pub mod leave;