mirror of
https://github.com/eRgo35/lyra.git
synced 2026-02-04 12:26:10 +01:00
songbird music support init
This commit is contained in:
41
src/commands/music/deafen.rs
Normal file
41
src/commands/music/deafen.rs
Normal 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(())
|
||||
}
|
||||
61
src/commands/music/join.rs
Normal file
61
src/commands/music/join.rs
Normal 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
|
||||
}
|
||||
}
|
||||
32
src/commands/music/leave.rs
Normal file
32
src/commands/music/leave.rs
Normal 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(())
|
||||
}
|
||||
3
src/commands/music/mod.rs
Normal file
3
src/commands/music/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod join;
|
||||
pub mod deafen;
|
||||
pub mod leave;
|
||||
Reference in New Issue
Block a user