2021-05-23 01:25:58 +08:00
|
|
|
//! Discord handler
|
|
|
|
|
2021-08-05 22:49:54 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use audiopus::{Channels, SampleRate};
|
|
|
|
use audiopus::coder::Decoder;
|
2021-05-10 06:27:18 +08:00
|
|
|
use serde::Deserialize;
|
2021-08-05 22:49:54 +08:00
|
|
|
use serenity::prelude::{Mentionable, Mutex};
|
2021-05-10 06:27:18 +08:00
|
|
|
|
2021-08-02 22:25:50 +08:00
|
|
|
use slog::error;
|
2021-05-10 06:27:18 +08:00
|
|
|
// This trait adds the `register_songbird` and `register_songbird_with` methods
|
|
|
|
// to the client builder below, making it easy to install this voice client.
|
|
|
|
// The voice client can be retrieved in any command using `songbird::get(ctx).await`.
|
2021-05-22 09:05:19 +08:00
|
|
|
use songbird::input::Input;
|
2021-05-10 06:27:18 +08:00
|
|
|
|
|
|
|
// Import the `Context` to handle commands.
|
|
|
|
use serenity::client::Context;
|
|
|
|
|
|
|
|
use serenity::{
|
|
|
|
async_trait,
|
2021-05-22 09:05:19 +08:00
|
|
|
client::{EventHandler},
|
2021-05-10 06:27:18 +08:00
|
|
|
framework::{
|
|
|
|
standard::{
|
|
|
|
Args, CommandResult,
|
|
|
|
macros::{command, group},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
model::{channel::Message, gateway::Ready},
|
|
|
|
Result as SerenityResult,
|
|
|
|
};
|
2021-08-05 22:49:54 +08:00
|
|
|
use songbird::packet::PacketSize;
|
|
|
|
use songbird::packet::rtp::RtpExtensionPacket;
|
2021-05-10 20:01:35 +08:00
|
|
|
use songbird::{
|
|
|
|
model::payload::{ClientConnect, ClientDisconnect, Speaking},
|
|
|
|
CoreEvent,
|
|
|
|
Event,
|
|
|
|
EventContext,
|
|
|
|
EventHandler as VoiceEventHandler,
|
|
|
|
};
|
2021-08-02 22:25:50 +08:00
|
|
|
use tsproto_packets::packets::{Direction, InAudioBuf};
|
2021-05-10 20:01:35 +08:00
|
|
|
|
2021-08-02 22:25:50 +08:00
|
|
|
use crate::{I16_CONVERSION_DIVIDER, ListenerHolder};
|
2021-05-10 06:27:18 +08:00
|
|
|
|
|
|
|
pub(crate) struct Handler;
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl EventHandler for Handler {
|
|
|
|
async fn ready(&self, _: Context, ready: Ready) {
|
|
|
|
println!("{} is connected!", ready.user.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[group]
|
|
|
|
#[commands(deafen, join, leave, mute, play, ping, undeafen, unmute)]
|
|
|
|
pub struct General;
|
|
|
|
|
|
|
|
#[derive(Debug,Deserialize)]
|
|
|
|
struct Config {
|
|
|
|
discord_token: String,
|
|
|
|
teamspeak_server: String,
|
|
|
|
teamspeak_identity: String,
|
|
|
|
teamspeak_channel: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[command]
|
|
|
|
#[only_in(guilds)]
|
|
|
|
async fn deafen(ctx: &Context, msg: &Message) -> CommandResult {
|
|
|
|
let guild = msg.guild(&ctx.cache).await.unwrap();
|
|
|
|
let guild_id = guild.id;
|
|
|
|
|
|
|
|
let manager = songbird::get(ctx).await
|
|
|
|
.expect("Songbird Voice client placed in at initialisation.").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(e) = handler.deafen(true).await {
|
|
|
|
check_msg(msg.channel_id.say(&ctx.http, format!("Failed: {:?}", e)).await);
|
|
|
|
}
|
|
|
|
|
|
|
|
check_msg(msg.channel_id.say(&ctx.http, "Deafened").await);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[command]
|
|
|
|
#[only_in(guilds)]
|
|
|
|
async fn join(ctx: &Context, msg: &Message) -> CommandResult {
|
|
|
|
let guild = msg.guild(&ctx.cache).await.unwrap();
|
|
|
|
let guild_id = guild.id;
|
|
|
|
|
|
|
|
let channel_id = guild
|
|
|
|
.voice_states.get(&msg.author.id)
|
|
|
|
.and_then(|voice_state| voice_state.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 client placed in at initialisation.").clone();
|
|
|
|
|
2021-05-10 20:01:35 +08:00
|
|
|
let (handler_lock, conn_result) = manager.join(guild_id, connect_to).await;
|
|
|
|
|
|
|
|
if let Ok(_) = conn_result {
|
|
|
|
// NOTE: this skips listening for the actual connection result.
|
2021-05-11 00:28:57 +08:00
|
|
|
let channel: crate::AudioBufferDiscord;
|
2021-05-22 09:01:37 +08:00
|
|
|
let ts_buffer: crate::TsToDiscordPipeline;
|
2021-05-10 20:01:35 +08:00
|
|
|
{
|
|
|
|
let data_read = ctx.data.read().await;
|
2021-05-22 09:01:37 +08:00
|
|
|
let (ts_buf,chan) = data_read.get::<ListenerHolder>().expect("Expected CommandCounter in TypeMap.").clone();
|
|
|
|
channel = chan;
|
|
|
|
ts_buffer = ts_buf;
|
2021-05-10 20:01:35 +08:00
|
|
|
}
|
|
|
|
let mut handler = handler_lock.lock().await;
|
2021-05-22 09:01:37 +08:00
|
|
|
let discord_input = Input::float_pcm(true, songbird::input::Reader::Extension(Box::new(ts_buffer.clone())));
|
|
|
|
handler.play_only_source(discord_input);
|
2021-05-10 20:01:35 +08:00
|
|
|
handler.add_global_event(
|
|
|
|
CoreEvent::SpeakingStateUpdate.into(),
|
|
|
|
Receiver::new(channel.clone()),
|
|
|
|
);
|
|
|
|
|
|
|
|
handler.add_global_event(
|
|
|
|
CoreEvent::SpeakingUpdate.into(),
|
|
|
|
Receiver::new(channel.clone()),
|
|
|
|
);
|
|
|
|
|
|
|
|
handler.add_global_event(
|
|
|
|
CoreEvent::VoicePacket.into(),
|
|
|
|
Receiver::new(channel.clone()),
|
|
|
|
);
|
|
|
|
|
|
|
|
handler.add_global_event(
|
|
|
|
CoreEvent::RtcpPacket.into(),
|
|
|
|
Receiver::new(channel.clone()),
|
|
|
|
);
|
|
|
|
|
|
|
|
handler.add_global_event(
|
|
|
|
CoreEvent::ClientConnect.into(),
|
|
|
|
Receiver::new(channel.clone()),
|
|
|
|
);
|
|
|
|
|
|
|
|
handler.add_global_event(
|
|
|
|
CoreEvent::ClientDisconnect.into(),
|
|
|
|
Receiver::new(channel),
|
|
|
|
);
|
|
|
|
|
|
|
|
check_msg(msg.channel_id.say(&ctx.http, &format!("Joined {}", connect_to.mention())).await);
|
|
|
|
} else {
|
|
|
|
check_msg(msg.channel_id.say(&ctx.http, "Error joining the channel").await);
|
|
|
|
}
|
2021-05-10 06:27:18 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[command]
|
|
|
|
#[only_in(guilds)]
|
|
|
|
async fn leave(ctx: &Context, msg: &Message) -> CommandResult {
|
|
|
|
let guild = msg.guild(&ctx.cache).await.unwrap();
|
|
|
|
let guild_id = guild.id;
|
|
|
|
|
|
|
|
let manager = songbird::get(ctx).await
|
|
|
|
.expect("Songbird Voice client placed in at initialisation.").clone();
|
|
|
|
let has_handler = manager.get(guild_id).is_some();
|
|
|
|
|
|
|
|
if has_handler {
|
|
|
|
if let Err(e) = manager.remove(guild_id).await {
|
|
|
|
check_msg(msg.channel_id.say(&ctx.http, format!("Failed: {:?}", e)).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(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[command]
|
|
|
|
#[only_in(guilds)]
|
|
|
|
async fn mute(ctx: &Context, msg: &Message) -> CommandResult {
|
|
|
|
let guild = msg.guild(&ctx.cache).await.unwrap();
|
|
|
|
let guild_id = guild.id;
|
|
|
|
|
|
|
|
let manager = songbird::get(ctx).await
|
|
|
|
.expect("Songbird Voice client placed in at initialisation.").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_mute() {
|
|
|
|
check_msg(msg.channel_id.say(&ctx.http, "Already muted").await);
|
|
|
|
} else {
|
|
|
|
if let Err(e) = handler.mute(true).await {
|
|
|
|
check_msg(msg.channel_id.say(&ctx.http, format!("Failed: {:?}", e)).await);
|
|
|
|
}
|
|
|
|
|
|
|
|
check_msg(msg.channel_id.say(&ctx.http, "Now muted").await);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[command]
|
|
|
|
async fn ping(context: &Context, msg: &Message) -> CommandResult {
|
|
|
|
check_msg(msg.channel_id.say(&context.http, "Pong!").await);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[command]
|
|
|
|
#[only_in(guilds)]
|
|
|
|
async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
|
|
|
let url = match args.single::<String>() {
|
|
|
|
Ok(url) => url,
|
|
|
|
Err(_) => {
|
|
|
|
check_msg(msg.channel_id.say(&ctx.http, "Must provide a URL to a video or audio").await);
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if !url.starts_with("http") {
|
|
|
|
check_msg(msg.channel_id.say(&ctx.http, "Must provide a valid URL").await);
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let guild = msg.guild(&ctx.cache).await.unwrap();
|
|
|
|
let guild_id = guild.id;
|
|
|
|
|
|
|
|
let manager = songbird::get(ctx).await
|
|
|
|
.expect("Songbird Voice client placed in at initialisation.").clone();
|
|
|
|
|
|
|
|
if let Some(handler_lock) = manager.get(guild_id) {
|
|
|
|
let mut handler = handler_lock.lock().await;
|
|
|
|
|
|
|
|
let source = match songbird::ytdl(&url).await {
|
|
|
|
Ok(source) => source,
|
|
|
|
Err(why) => {
|
|
|
|
println!("Err starting source: {:?}", why);
|
|
|
|
|
|
|
|
check_msg(msg.channel_id.say(&ctx.http, "Error sourcing ffmpeg").await);
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
handler.play_source(source);
|
|
|
|
|
|
|
|
check_msg(msg.channel_id.say(&ctx.http, "Playing song").await);
|
|
|
|
} else {
|
|
|
|
check_msg(msg.channel_id.say(&ctx.http, "Not in a voice channel to play in").await);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[command]
|
|
|
|
#[only_in(guilds)]
|
|
|
|
async fn undeafen(ctx: &Context, msg: &Message) -> CommandResult {
|
|
|
|
let guild = msg.guild(&ctx.cache).await.unwrap();
|
|
|
|
let guild_id = guild.id;
|
|
|
|
|
|
|
|
let manager = songbird::get(ctx).await
|
|
|
|
.expect("Songbird Voice client placed in at initialisation.").clone();
|
|
|
|
|
|
|
|
if let Some(handler_lock) = manager.get(guild_id) {
|
|
|
|
let mut handler = handler_lock.lock().await;
|
|
|
|
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, "Undeafened").await);
|
|
|
|
} else {
|
|
|
|
check_msg(msg.channel_id.say(&ctx.http, "Not in a voice channel to undeafen in").await);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[command]
|
|
|
|
#[only_in(guilds)]
|
|
|
|
async fn unmute(ctx: &Context, msg: &Message) -> CommandResult {
|
|
|
|
let guild = msg.guild(&ctx.cache).await.unwrap();
|
|
|
|
let guild_id = guild.id;
|
|
|
|
|
|
|
|
let manager = songbird::get(ctx).await
|
|
|
|
.expect("Songbird Voice client placed in at initialisation.").clone();
|
|
|
|
|
|
|
|
if let Some(handler_lock) = manager.get(guild_id) {
|
|
|
|
let mut handler = handler_lock.lock().await;
|
|
|
|
if let Err(e) = handler.mute(false).await {
|
|
|
|
check_msg(msg.channel_id.say(&ctx.http, format!("Failed: {:?}", e)).await);
|
|
|
|
}
|
|
|
|
|
|
|
|
check_msg(msg.channel_id.say(&ctx.http, "Unmuted").await);
|
|
|
|
} else {
|
|
|
|
check_msg(msg.channel_id.say(&ctx.http, "Not in a voice channel to unmute in").await);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that a message successfully sent; if not, then logs why to stdout.
|
|
|
|
fn check_msg(result: SerenityResult<Message>) {
|
|
|
|
if let Err(why) = result {
|
|
|
|
println!("Error sending message: {:?}", why);
|
|
|
|
}
|
2021-05-10 20:01:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Receiver{
|
2021-05-11 00:28:57 +08:00
|
|
|
sink: crate::AudioBufferDiscord,
|
2021-08-05 22:49:54 +08:00
|
|
|
decoder: Arc<Mutex<Decoder>>,
|
2021-05-10 20:01:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Receiver {
|
2021-05-11 00:28:57 +08:00
|
|
|
pub fn new(voice_receiver: crate::AudioBufferDiscord) -> Self {
|
2021-05-10 20:01:35 +08:00
|
|
|
// You can manage state here, such as a buffer of audio packet bytes so
|
|
|
|
// you can later store them in intervals.
|
|
|
|
Self {
|
|
|
|
sink: voice_receiver,
|
2021-08-05 22:49:54 +08:00
|
|
|
decoder: Arc::new(Mutex::new(Decoder::new(SampleRate::Hz48000, Channels::Stereo).unwrap()))
|
2021-05-10 20:01:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl VoiceEventHandler for Receiver {
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
|
|
|
|
use EventContext as Ctx;
|
|
|
|
match ctx {
|
|
|
|
Ctx::SpeakingStateUpdate(
|
|
|
|
Speaking {speaking, ssrc, user_id, ..}
|
|
|
|
) => {
|
|
|
|
// Discord voice calls use RTP, where every sender uses a randomly allocated
|
|
|
|
// *Synchronisation Source* (SSRC) to allow receivers to tell which audio
|
|
|
|
// stream a received packet belongs to. As this number is not derived from
|
|
|
|
// the sender's user_id, only Discord Voice Gateway messages like this one
|
|
|
|
// inform us about which random SSRC a user has been allocated. Future voice
|
|
|
|
// packets will contain *only* the SSRC.
|
|
|
|
//
|
|
|
|
// You can implement logic here so that you can differentiate users'
|
|
|
|
// SSRCs and map the SSRC to the User ID and maintain this state.
|
|
|
|
// Using this map, you can map the `ssrc` in `voice_packet`
|
|
|
|
// to the user ID and handle their audio packets separately.
|
2021-05-11 00:42:52 +08:00
|
|
|
//println!(
|
|
|
|
// "Speaking state update: user {:?} has SSRC {:?}, using {:?}",
|
|
|
|
// user_id,
|
|
|
|
// ssrc,
|
|
|
|
// speaking,
|
|
|
|
// );
|
2021-05-10 20:01:35 +08:00
|
|
|
},
|
|
|
|
Ctx::SpeakingUpdate {ssrc, speaking} => {
|
|
|
|
// You can implement logic here which reacts to a user starting
|
|
|
|
// or stopping speaking.
|
2021-05-11 00:42:52 +08:00
|
|
|
//println!(
|
|
|
|
// "Source {} has {} speaking.",
|
|
|
|
// ssrc,
|
|
|
|
// if *speaking {"started"} else {"stopped"},
|
|
|
|
// );
|
2021-05-10 20:01:35 +08:00
|
|
|
},
|
|
|
|
Ctx::VoicePacket {audio, packet, payload_offset, payload_end_pad} => {
|
|
|
|
// An event which fires for every received audio packet,
|
2021-08-02 22:25:50 +08:00
|
|
|
|
|
|
|
// get raw opus package, we don't decode here and leave that to the AudioHandler
|
|
|
|
let last_bytes = packet.payload.len() - payload_end_pad;
|
2021-08-05 22:49:54 +08:00
|
|
|
let data = &packet.payload[*payload_offset..last_bytes];
|
|
|
|
let start = if packet.extension != 0 {
|
|
|
|
match RtpExtensionPacket::new(data) {
|
|
|
|
Some(v) => v.packet_size(),
|
|
|
|
None => {
|
|
|
|
eprintln!("Extension packet indicated, but insufficient space.");
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
let opus_slice = &data[start..];
|
2021-08-02 22:25:50 +08:00
|
|
|
let dur;
|
|
|
|
{
|
2021-08-05 22:50:28 +08:00
|
|
|
let time = std::time::Instant::now();
|
|
|
|
let mut lock = self.sink.lock().await;
|
|
|
|
dur = time.elapsed();
|
|
|
|
if let Err(e) = lock.handle_packet(packet.ssrc, packet.sequence.0.0, opus_slice.to_vec()) {
|
|
|
|
eprintln!("Failed to handle Discord voice packet: {}",e);
|
|
|
|
}
|
|
|
|
if dur.as_millis() > 1 {
|
|
|
|
eprintln!("Acquiring lock took {}ms",dur.as_millis());
|
2021-05-11 00:28:57 +08:00
|
|
|
}
|
2021-08-02 22:25:50 +08:00
|
|
|
}
|
2021-08-05 22:49:54 +08:00
|
|
|
|
2021-05-10 20:01:35 +08:00
|
|
|
},
|
|
|
|
Ctx::RtcpPacket {packet, payload_offset, payload_end_pad} => {
|
|
|
|
// An event which fires for every received rtcp packet,
|
|
|
|
// containing the call statistics and reporting information.
|
2021-05-11 00:28:57 +08:00
|
|
|
//println!("RTCP packet received: {:?}", packet);
|
2021-05-10 20:01:35 +08:00
|
|
|
},
|
|
|
|
Ctx::ClientConnect(
|
|
|
|
ClientConnect {audio_ssrc, video_ssrc, user_id, ..}
|
|
|
|
) => {
|
|
|
|
// You can implement your own logic here to handle a user who has joined the
|
|
|
|
// voice channel e.g., allocate structures, map their SSRC to User ID.
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"Client connected: user {:?} has audio SSRC {:?}, video SSRC {:?}",
|
|
|
|
user_id,
|
|
|
|
audio_ssrc,
|
|
|
|
video_ssrc,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
Ctx::ClientDisconnect(
|
|
|
|
ClientDisconnect {user_id, ..}
|
|
|
|
) => {
|
|
|
|
// You can implement your own logic here to handle a user who has left the
|
|
|
|
// voice channel e.g., finalise processing of statistics etc.
|
|
|
|
// You will typically need to map the User ID to their SSRC; observed when
|
|
|
|
// speaking or connecting.
|
|
|
|
|
|
|
|
println!("Client disconnected: user {:?}", user_id);
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
// We won't be registering this struct for any more event classes.
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
2021-05-10 06:27:18 +08:00
|
|
|
}
|