This commit is contained in:
JerryXiao 2023-04-09 17:00:05 +08:00
parent 99dc2c3448
commit 6cdf433197
Signed by: Jerry
GPG key ID: 22618F758B5BE2E5
4 changed files with 1272 additions and 787 deletions

1989
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -7,10 +7,10 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[profile.release]
lto = true
lto = false
[dependencies]
toml = "0.5"
toml = "0.7"
serde = { version = "1.0", features = ["derive"] }
# pushing [f32] to [u8] and vice verca
@ -18,7 +18,7 @@ byte-slice-cast = "1"
# tokio tracing from songbird
tracing = "0.1"
tracing-subscriber = "0.2"
tracing-subscriber = "0.3"
tracing-futures = "0.2"
### TS stuff
@ -49,15 +49,18 @@ version = "0.2"
## discord specific
[dependencies.songbird]
version = "0.2"
version = "0.3"
features = ["driver","gateway"]
[dependencies.serenity]
version = "0.10"
version = "0.11"
features = ["client", "standard_framework", "voice","native_tls_backend"]
## tokio
[dependencies.tokio]
version = "1.0"
features = ["macros", "rt-multi-thread","signal", "sync"]
version = "1.27"
features = ["macros", "rt-multi-thread","signal", "sync"]
[patch.crates-io]
songbird = { git = "https://github.com/serenity-rs/songbird", branch="current" }

View file

@ -1,5 +1,6 @@
//! Discord handler
use std::result::Result::Ok;
use serde::Deserialize;
use serenity::prelude::Mentionable;
@ -26,7 +27,7 @@ use serenity::{
use songbird::packet::PacketSize;
use songbird::packet::rtp::RtpExtensionPacket;
use songbird::{
model::payload::{ClientConnect, ClientDisconnect, Speaking},
model::payload::{ClientDisconnect, Speaking},
CoreEvent,
Event,
EventContext,
@ -59,7 +60,7 @@ struct Config {
#[command]
#[only_in(guilds)]
async fn deafen(ctx: &Context, msg: &Message) -> CommandResult {
let guild = msg.guild(&ctx.cache).await.unwrap();
let guild = msg.guild(&ctx.cache).unwrap();
let guild_id = guild.id;
let manager = songbird::get(ctx).await
@ -92,7 +93,9 @@ async fn deafen(ctx: &Context, msg: &Message) -> CommandResult {
#[command]
#[only_in(guilds)]
async fn join(ctx: &Context, msg: &Message) -> CommandResult {
let guild = msg.guild(&ctx.cache).await.unwrap();
check_msg(msg.channel_id.say(&ctx.http, "Joining!").await);
let guild = msg.guild(&ctx.cache).unwrap();
let guild_id = guild.id;
let channel_id = guild
@ -146,11 +149,6 @@ async fn join(ctx: &Context, msg: &Message) -> CommandResult {
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),
@ -158,7 +156,7 @@ async fn join(ctx: &Context, msg: &Message) -> CommandResult {
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);
check_msg(msg.channel_id.say(&ctx.http, &format!("Error joining the channel, {:?}", conn_result)).await);
}
Ok(())
@ -167,7 +165,7 @@ async fn join(ctx: &Context, msg: &Message) -> CommandResult {
#[command]
#[only_in(guilds)]
async fn leave(ctx: &Context, msg: &Message) -> CommandResult {
let guild = msg.guild(&ctx.cache).await.unwrap();
let guild = msg.guild(&ctx.cache).unwrap();
let guild_id = guild.id;
let manager = songbird::get(ctx).await
@ -190,7 +188,7 @@ async fn leave(ctx: &Context, msg: &Message) -> CommandResult {
#[command]
#[only_in(guilds)]
async fn mute(ctx: &Context, msg: &Message) -> CommandResult {
let guild = msg.guild(&ctx.cache).await.unwrap();
let guild = msg.guild(&ctx.cache).unwrap();
let guild_id = guild.id;
let manager = songbird::get(ctx).await
@ -245,7 +243,7 @@ async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
return Ok(());
}
let guild = msg.guild(&ctx.cache).await.unwrap();
let guild = msg.guild(&ctx.cache).unwrap();
let guild_id = guild.id;
let manager = songbird::get(ctx).await
@ -278,7 +276,7 @@ async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
#[command]
#[only_in(guilds)]
async fn undeafen(ctx: &Context, msg: &Message) -> CommandResult {
let guild = msg.guild(&ctx.cache).await.unwrap();
let guild = msg.guild(&ctx.cache).unwrap();
let guild_id = guild.id;
let manager = songbird::get(ctx).await
@ -301,7 +299,7 @@ async fn undeafen(ctx: &Context, msg: &Message) -> CommandResult {
#[command]
#[only_in(guilds)]
async fn unmute(ctx: &Context, msg: &Message) -> CommandResult {
let guild = msg.guild(&ctx.cache).await.unwrap();
let guild = msg.guild(&ctx.cache).unwrap();
let guild_id = guild.id;
let manager = songbird::get(ctx).await
@ -417,19 +415,6 @@ impl VoiceEventHandler for Receiver {
// containing the call statistics and reporting information.
//println!("RTCP packet received: {:?}", packet);
},
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, ..}
) => {
@ -448,4 +433,4 @@ impl VoiceEventHandler for Receiver {
None
}
}
}

View file

@ -1,5 +1,6 @@
use std::io::Seek;
use std::{io::Read, mem::size_of, sync::Arc, time::Duration};
use std::result::Result::Ok;
use byte_slice_cast::AsByteSlice;
use serde::Deserialize;
use songbird::input::reader::MediaSource;
@ -33,6 +34,7 @@ use serenity::{
framework::{
StandardFramework,
},
prelude::GatewayIntents,
};
@ -71,7 +73,7 @@ impl MediaSource for TsToDiscordPipeline {
false
}
fn len(&self) -> Option<u64> {
fn byte_len(&self) -> Option<u64> {
None
}
}
@ -161,7 +163,9 @@ async fn main() -> Result<()> {
);
// init discord client
let mut client = Client::builder(&config.discord_token)
let intents = GatewayIntents::non_privileged()
| GatewayIntents::MESSAGE_CONTENT;
let mut client = Client::builder(&config.discord_token, intents)
.event_handler(discord::Handler)
.framework(framework)
.register_songbird_with(songbird.into())
@ -272,7 +276,7 @@ async fn main() -> Result<()> {
con.send_audio(processed)?;
let dur = start.elapsed();
if dur >= Duration::from_millis(1) {
eprintln!("Audio pipeline took {}ms",dur.as_millis());
debug!(logger, "Audio pipeline took {}ms",dur.as_millis());
}
}
}
@ -329,4 +333,4 @@ async fn process_discord_audio(voice_buffer: &AudioBufferDiscord, encoder: &Arc<
Some(OutAudio::new(&AudioData::C2S { id: 0, codec: CodecType::OpusMusic, data: &encoded[..length] }))
}).await.expect("Join error for audio processing thread!");
res
}
}