Compare commits
25 Commits
947d4d64c4
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 0daee61d61 | |||
| c7558645c2 | |||
| 632a115930 | |||
| 95806edca6 | |||
| c4b01bf78a | |||
| c47eff4fc9 | |||
| 9fdea3d643 | |||
| 577f286773 | |||
| 51053b043b | |||
| 3e1bb11104 | |||
| 57457a0724 | |||
| 7797dbf89a | |||
| f6020bb859 | |||
| ec115960d9 | |||
| d06ab36d35 | |||
| b8f8c07e55 | |||
| 940d6ca512 | |||
| 8c450f1980 | |||
| d0e911d802 | |||
| 4ef476698b | |||
| 5a2fd0a3f7 | |||
| d2f0909299 | |||
| eb6a55eefd | |||
| a6456ad9de | |||
| fb3f38596e |
@@ -20,8 +20,15 @@
|
|||||||
systemd.services.alan = {
|
systemd.services.alan = {
|
||||||
wantedBy = ["networking-online.target"];
|
wantedBy = ["networking-online.target"];
|
||||||
enable = true;
|
enable = true;
|
||||||
|
path = [
|
||||||
|
pkgs.new_alan
|
||||||
|
pkgs.ffmpeg_5-headless
|
||||||
|
];
|
||||||
|
environment = {
|
||||||
|
DING_SOUND = "${pkgs.new_alan}/share/sounds/ding.mp3";
|
||||||
|
};
|
||||||
script = ''
|
script = ''
|
||||||
${pkgs.new_alan}/bin/new_alan
|
${pkgs.new_alan}/bin/new_alan
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -40,6 +40,8 @@
|
|||||||
buildInputs = with pkgs; [
|
buildInputs = with pkgs; [
|
||||||
# Add additional build inputs here
|
# Add additional build inputs here
|
||||||
libopus
|
libopus
|
||||||
|
stt
|
||||||
|
tts
|
||||||
] ++ lib.optionals pkgs.stdenv.isDarwin ([
|
] ++ lib.optionals pkgs.stdenv.isDarwin ([
|
||||||
# Additional darwin specific inputs can be set here
|
# Additional darwin specific inputs can be set here
|
||||||
pkgs.libiconv
|
pkgs.libiconv
|
||||||
@@ -66,6 +68,10 @@
|
|||||||
# artifacts from above.
|
# artifacts from above.
|
||||||
newalan = craneLib.buildPackage (commonArgs // {
|
newalan = craneLib.buildPackage (commonArgs // {
|
||||||
inherit cargoArtifacts;
|
inherit cargoArtifacts;
|
||||||
|
postInstall = ''
|
||||||
|
mkdir -p $out/share/sounds
|
||||||
|
cp ${./ding.mp3} $out/share/sounds/ding.mp3
|
||||||
|
'';
|
||||||
});
|
});
|
||||||
in rec {
|
in rec {
|
||||||
checks = pkgs.lib.optionalAttrs (system == "x86_64-linux") {
|
checks = pkgs.lib.optionalAttrs (system == "x86_64-linux") {
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
use serenity::framework::standard::macros::group;
|
use serenity::framework::standard::macros::group;
|
||||||
|
|
||||||
pub mod ping;
|
pub mod ping;
|
||||||
use ping::*;
|
use ping::*;
|
||||||
|
|
||||||
|
pub mod vc;
|
||||||
|
use vc::*;
|
||||||
|
|
||||||
#[group]
|
#[group]
|
||||||
#[commands(ping)]
|
#[commands(ping, join)]
|
||||||
pub struct General;
|
pub struct General;
|
||||||
|
|||||||
30
src/commands/vc.rs
Normal file
30
src/commands/vc.rs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
use serenity::framework::standard::macros::command;
|
||||||
|
use serenity::framework::standard::CommandResult;
|
||||||
|
use serenity::model::channel::Message;
|
||||||
|
use serenity::prelude::Context;
|
||||||
|
|
||||||
|
use crate::vc;
|
||||||
|
|
||||||
|
#[command]
|
||||||
|
#[only_in(guilds)]
|
||||||
|
pub async fn join(ctx: &Context, msg: &Message) -> CommandResult {
|
||||||
|
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);
|
||||||
|
|
||||||
|
let connect_to = match channel_id {
|
||||||
|
Some(channel) => channel,
|
||||||
|
None => {
|
||||||
|
msg.reply(ctx, "Not in a voice channel").await;
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
vc::join(ctx.clone(), guild.clone(), connect_to).await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -22,7 +22,6 @@ impl EventHandler for Handler {
|
|||||||
|
|
||||||
async fn guild_create(&self, ctx: Context, guild: Guild, _new: bool) {
|
async fn guild_create(&self, ctx: Context, guild: Guild, _new: bool) {
|
||||||
println!("Guild joined: {}", guild.name);
|
println!("Guild joined: {}", guild.name);
|
||||||
|
|
||||||
guild_popin::add_guild(ctx, guild).await;
|
guild_popin::add_guild(ctx, guild).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
26
src/main.rs
26
src/main.rs
@@ -2,13 +2,13 @@
|
|||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use serenity::framework::standard::StandardFramework;
|
use serenity::framework::standard::{StandardFramework, WithWhiteSpace};
|
||||||
use serenity::prelude::*;
|
use serenity::prelude::*;
|
||||||
|
|
||||||
// This trait adds the `register_songbird` and `register_songbird_with` methods
|
// This trait adds the `register_songbird` and `register_songbird_with` methods
|
||||||
// to the client builder below, making it easy to install this voice client.
|
// 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`.
|
// The voice client can be retrieved in any command using `songbird::get(ctx).await`.
|
||||||
use songbird::SerenityInit;
|
use songbird::{SerenityInit, Config, driver::DecodeMode};
|
||||||
|
|
||||||
mod commands;
|
mod commands;
|
||||||
use commands::*;
|
use commands::*;
|
||||||
@@ -17,6 +17,7 @@ mod events;
|
|||||||
use events::*;
|
use events::*;
|
||||||
|
|
||||||
mod utils;
|
mod utils;
|
||||||
|
mod vc;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
@@ -25,7 +26,8 @@ async fn main() {
|
|||||||
// Configure the client with your Discord bot token in the environment.
|
// Configure the client with your Discord bot token in the environment.
|
||||||
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
|
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
|
||||||
// Set gateway intents, which decides what events the bot will be notified about
|
// Set gateway intents, which decides what events the bot will be notified about
|
||||||
let intents = GatewayIntents::GUILDS
|
let intents = GatewayIntents::non_privileged()
|
||||||
|
| GatewayIntents::privileged()
|
||||||
| GatewayIntents::DIRECT_MESSAGES
|
| GatewayIntents::DIRECT_MESSAGES
|
||||||
| GatewayIntents::DIRECT_MESSAGE_REACTIONS
|
| GatewayIntents::DIRECT_MESSAGE_REACTIONS
|
||||||
| GatewayIntents::DIRECT_MESSAGE_TYPING
|
| GatewayIntents::DIRECT_MESSAGE_TYPING
|
||||||
@@ -33,19 +35,29 @@ async fn main() {
|
|||||||
| GatewayIntents::GUILD_VOICE_STATES;
|
| GatewayIntents::GUILD_VOICE_STATES;
|
||||||
|
|
||||||
let framework = StandardFramework::new()
|
let framework = StandardFramework::new()
|
||||||
.configure(|c| c.prefix("ALAN! "))
|
.configure(|c| {
|
||||||
|
c.prefix("ALAN! ");
|
||||||
|
c.with_whitespace(WithWhiteSpace {
|
||||||
|
commands: true,
|
||||||
|
groups: true,
|
||||||
|
prefixes: true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
.group(&GENERAL_GROUP);
|
.group(&GENERAL_GROUP);
|
||||||
|
|
||||||
|
let songbird_config = Config::default()
|
||||||
|
.decode_mode(DecodeMode::Decode);
|
||||||
|
|
||||||
let mut client = Client::builder(&token, intents)
|
let mut client = Client::builder(&token, intents)
|
||||||
.event_handler(Handler)
|
.event_handler(Handler)
|
||||||
.framework(framework)
|
.framework(framework)
|
||||||
.register_songbird()
|
.register_songbird_from_config(songbird_config)
|
||||||
.await
|
.await
|
||||||
.expect("Err creating client");
|
.expect("Err creating client");
|
||||||
|
|
||||||
// TODO: do checks when getting data in add guild etc.
|
|
||||||
println!("Initialize guild voice popin state");
|
|
||||||
utils::guild_popin::init(&client).await;
|
utils::guild_popin::init(&client).await;
|
||||||
|
vc::init(&client).await;
|
||||||
|
|
||||||
if let Err(why) = client.start().await {
|
if let Err(why) = client.start().await {
|
||||||
println!("Client error: {:?}", why);
|
println!("Client error: {:?}", why);
|
||||||
|
|||||||
@@ -1,19 +1,20 @@
|
|||||||
|
use rand::prelude::*;
|
||||||
|
use serenity::model::id::GuildId;
|
||||||
|
use serenity::Client;
|
||||||
|
use songbird::Call;
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::{Duration, Instant};
|
|
||||||
use rand::prelude::*;
|
|
||||||
use serenity::Client;
|
|
||||||
use songbird::id::ChannelId;
|
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
use std::collections::HashMap;
|
use std::time::Duration;
|
||||||
use serenity::model::id::GuildId;
|
|
||||||
|
|
||||||
use serenity::model::prelude::{Guild, ChannelType};
|
use serenity::model::prelude::{ChannelType, Guild};
|
||||||
use serenity::prelude::{Context, RwLock, TypeMapKey};
|
use serenity::prelude::{Context, RwLock, TypeMapKey, Mutex};
|
||||||
|
|
||||||
|
use crate::vc;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct GuildPopIn {
|
struct GuildPopIn {
|
||||||
last_join: Instant,
|
|
||||||
min: u64, // milliseconds
|
min: u64, // milliseconds
|
||||||
max: u64, // milliseconds
|
max: u64, // milliseconds
|
||||||
}
|
}
|
||||||
@@ -24,9 +25,8 @@ impl TypeMapKey for GuildPopIn {
|
|||||||
|
|
||||||
fn init_pop_state() -> GuildPopIn {
|
fn init_pop_state() -> GuildPopIn {
|
||||||
GuildPopIn {
|
GuildPopIn {
|
||||||
last_join: Instant::now(),
|
min: 30*60*1000,
|
||||||
min: 1000,
|
max: 180*60*1000,
|
||||||
max: 5000
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,56 +34,95 @@ async fn popin(ctx: Context, guild: Guild) {
|
|||||||
println!("Gonna join, I'll do it");
|
println!("Gonna join, I'll do it");
|
||||||
let mut most = None;
|
let mut most = None;
|
||||||
let mut n = 0;
|
let mut n = 0;
|
||||||
for (id,chan) in guild.channels {
|
for (id, chan) in guild.clone().channels {
|
||||||
match chan.guild() {
|
match chan.guild() {
|
||||||
Some(g_chan) => {
|
Some(g_chan) => {
|
||||||
if g_chan.kind == ChannelType::Voice {
|
if g_chan.kind == ChannelType::Voice {
|
||||||
match g_chan.members(&ctx.cache).await {
|
match g_chan.members(&ctx.cache).await {
|
||||||
Ok(members) => {
|
Ok(members) => {
|
||||||
if members.len() > n {
|
if members.len() > n {
|
||||||
n = members.len();
|
n = members.len();
|
||||||
most = Some(id);
|
most = Some(id);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(error) => {
|
Err(_error) => {}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
}
|
None => {}
|
||||||
},
|
}
|
||||||
None => {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
let mut call = None;
|
||||||
match most {
|
match most {
|
||||||
Some(chan) => {
|
Some(chan) => {
|
||||||
let manager = songbird::get(&ctx).await
|
call = vc::join(ctx.clone(), guild.clone(), chan).await;
|
||||||
.expect("Songbird: intialization");
|
}
|
||||||
let (_,_status) = manager.join(guild.id, chan).await;
|
None => {
|
||||||
},
|
println!("No good channel to join");
|
||||||
None => {
|
}
|
||||||
println!("No good channel to join")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
()
|
tokio::spawn(popout_soon(ctx.clone(), guild, call));
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn popin_soon(ctx: Context, guild: Guild) {
|
async fn popin_soon(ctx: Context, guild: Guild) {
|
||||||
let data = ctx.data.read().await;
|
let data = ctx.data.read().await;
|
||||||
let pops = data.get::<GuildPopIn>().expect("Guild Popin states not initialized");
|
let pops = data
|
||||||
|
.get::<GuildPopIn>()
|
||||||
|
.expect("Guild Popin states not initialized");
|
||||||
let pops = pops.read().await;
|
let pops = pops.read().await;
|
||||||
let popwhen = pops.get(&guild.id).expect(
|
let popwhen = pops
|
||||||
format!("PopIn: guild {} not found", guild.name.as_str()).as_str()
|
.get(&guild.id)
|
||||||
);
|
.expect(format!("PopIn: guild {} not found", guild.name.as_str()).as_str());
|
||||||
let mut rng = thread_rng();
|
let mut rng = thread_rng();
|
||||||
let join_in = rng.gen_range(Range {
|
let join_in = rng.gen_range(Range {
|
||||||
start: popwhen.min,
|
start: popwhen.min,
|
||||||
end: popwhen.max
|
end: popwhen.max,
|
||||||
});
|
});
|
||||||
println!("Joining in: {}", join_in);
|
println!("Joining in: {}", join_in);
|
||||||
sleep(Duration::from_millis(join_in));
|
sleep(Duration::from_millis(join_in));
|
||||||
tokio::spawn(popin(ctx.clone(),guild));
|
tokio::spawn(popin(ctx.clone(), guild));
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn popout(ctx: Context, guild: Guild, call: Option<Arc<Mutex<Call>>>) {
|
||||||
|
match call {
|
||||||
|
Some(call) => {
|
||||||
|
let mut call = call.lock().await;
|
||||||
|
let status = call.leave().await;
|
||||||
|
match status {
|
||||||
|
Ok(_) => {},
|
||||||
|
Err(_err) => {
|
||||||
|
println!("Failed to leave call");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
start_dingdong(ctx.clone(), guild);
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn popout_soon(ctx: Context, guild: Guild, call: Option<Arc<Mutex<Call>>>) {
|
||||||
|
let data = ctx.data.read().await;
|
||||||
|
let pops = data
|
||||||
|
.get::<GuildPopIn>()
|
||||||
|
.expect("Guild Popin states not initialized");
|
||||||
|
let pops = pops.read().await;
|
||||||
|
let popwhen = pops
|
||||||
|
.get(&guild.id)
|
||||||
|
.expect(format!("PopIn: guild {} not found", guild.name.as_str()).as_str());
|
||||||
|
let mut rng = thread_rng();
|
||||||
|
let join_in = rng.gen_range(Range {
|
||||||
|
start: popwhen.min,
|
||||||
|
end: popwhen.max,
|
||||||
|
});
|
||||||
|
println!("Leaving in: {}", join_in);
|
||||||
|
sleep(Duration::from_millis(join_in));
|
||||||
|
tokio::spawn(popout(ctx.clone(), guild, call.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn start_dingdong(ctx: Context, guild: Guild) {
|
||||||
|
tokio::spawn(async {
|
||||||
|
popin_soon(ctx, guild).await;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn init(client: &Client) {
|
pub async fn init(client: &Client) {
|
||||||
@@ -93,11 +132,13 @@ pub async fn init(client: &Client) {
|
|||||||
|
|
||||||
pub async fn add_guild(ctx: Context, guild: Guild) {
|
pub async fn add_guild(ctx: Context, guild: Guild) {
|
||||||
let data = ctx.data.write().await;
|
let data = ctx.data.write().await;
|
||||||
let pops = data.get::<GuildPopIn>().expect("Guild Popin States Not Initialized");
|
let pops = data
|
||||||
|
.get::<GuildPopIn>()
|
||||||
|
.expect("Guild Popin States Not Initialized");
|
||||||
{
|
{
|
||||||
let mut pops = pops.write().await;
|
let mut pops = pops.write().await;
|
||||||
pops.insert(guild.id, init_pop_state());
|
pops.insert(guild.id, init_pop_state());
|
||||||
println!("GuildsPopIns: {:?}", pops);
|
println!("GuildsPopIns: {:?}", pops);
|
||||||
tokio::spawn(popin_soon(ctx.clone(), guild));
|
start_dingdong(ctx.clone(), guild);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
249
src/vc/mod.rs
Normal file
249
src/vc/mod.rs
Normal file
@@ -0,0 +1,249 @@
|
|||||||
|
use std::{collections::HashMap, sync::Arc};
|
||||||
|
|
||||||
|
use serenity::{
|
||||||
|
async_trait,
|
||||||
|
model::prelude::{ChannelId, Guild, GuildId},
|
||||||
|
prelude::{Context, Mutex, RwLock, TypeMapKey}, Client,
|
||||||
|
};
|
||||||
|
|
||||||
|
use songbird::{
|
||||||
|
create_player,
|
||||||
|
events::context_data::{ConnectData, DisconnectData},
|
||||||
|
ffmpeg,
|
||||||
|
model::{
|
||||||
|
id::UserId,
|
||||||
|
payload::{ClientDisconnect, Speaking},
|
||||||
|
},
|
||||||
|
Call, CoreEvent, Event, EventContext, EventHandler,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Eq, Hash)]
|
||||||
|
struct CallLocation {
|
||||||
|
guild: GuildId,
|
||||||
|
channel: ChannelId
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for CallLocation {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.guild == other.guild && self.channel == other.channel
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct VoiceData {
|
||||||
|
call: Arc<Mutex<Call>>,
|
||||||
|
users: Arc<RwLock<HashMap<u32, Option<UserId>>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VoiceData {
|
||||||
|
pub fn new(call: Arc<Mutex<Call>>) -> Self {
|
||||||
|
Self {
|
||||||
|
call,
|
||||||
|
users: Arc::new(RwLock::new(HashMap::default()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct VCData {
|
||||||
|
loc: Arc<CallLocation>,
|
||||||
|
data: Arc<RwLock<VoiceData>>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TypeMapKey for VCData {
|
||||||
|
type Value = Arc<RwLock<HashMap<CallLocation, VCData>>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VCData {
|
||||||
|
pub fn new(loc: CallLocation, data: VoiceData) -> Self {
|
||||||
|
// You can manage state here, such as a buffer of audio packet bytes so
|
||||||
|
// you can later store them in intervals.
|
||||||
|
VCData {
|
||||||
|
loc: Arc::new(loc),
|
||||||
|
data: Arc::new(RwLock::new(data))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn clone(&self) -> Self {
|
||||||
|
VCData {
|
||||||
|
loc: self.loc.clone(),
|
||||||
|
data: self.data.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn init(client: &Client) {
|
||||||
|
let mut data = client.data.write().await;
|
||||||
|
data.insert::<VCData>(Arc::new(RwLock::new(HashMap::default())))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl EventHandler for VCData {
|
||||||
|
#[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.
|
||||||
|
{
|
||||||
|
let data = self.data.write().await;
|
||||||
|
let mut users = data.users.write().await;
|
||||||
|
users.insert(ssrc.clone(), user_id.clone());
|
||||||
|
}
|
||||||
|
println!(
|
||||||
|
"\n\n\nSpeaking state update: user {:?} has SSRC {:?}, using {:?}\n\n\n",
|
||||||
|
user_id, ssrc, speaking,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Ctx::SpeakingUpdate(data) => {
|
||||||
|
// You can implement logic here which reacts to a user starting
|
||||||
|
// or stopping speaking, and to map their SSRC to User ID.
|
||||||
|
let vcdata = self.data.read().await;
|
||||||
|
let users = vcdata.users.read().await;
|
||||||
|
println!(
|
||||||
|
"Source {}/{:?} has {} speaking.",
|
||||||
|
data.ssrc,
|
||||||
|
users.get(&data.ssrc),
|
||||||
|
if data.speaking { "started" } else { "stopped" },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Ctx::VoicePacket(data) => {
|
||||||
|
// An event which fires for every received audio packet,
|
||||||
|
// containing the decoded data.
|
||||||
|
if let Some(audio) = data.audio {
|
||||||
|
// println!("Audio packet's first 5 samples: {:?}", audio.get(..5.min(audio.len())));
|
||||||
|
// println!(
|
||||||
|
// "Audio packet sequence {:05} has {:04} bytes (decompressed from {}), SSRC {}",
|
||||||
|
// data.packet.sequence.0,
|
||||||
|
// audio.len() * std::mem::size_of::<i16>(),
|
||||||
|
// data.packet.payload.len(),
|
||||||
|
// data.packet.ssrc,
|
||||||
|
// );
|
||||||
|
} else {
|
||||||
|
// println!("RTP packet, but no audio. Driver may not be configured to decode.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ctx::RtcpPacket(data) => {
|
||||||
|
// An event which fires for every received rtcp packet,
|
||||||
|
// containing the call statistics and reporting information.
|
||||||
|
// println!("RTCP packet received: {:?}", data.packet);
|
||||||
|
}
|
||||||
|
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
|
||||||
|
// first speaking.
|
||||||
|
|
||||||
|
println!("Client disconnected: user {:?}", user_id);
|
||||||
|
}
|
||||||
|
Ctx::DriverConnect(ConnectData { channel_id, .. }) => {
|
||||||
|
println!("VoiceDriver is connected.");
|
||||||
|
}
|
||||||
|
Ctx::DriverDisconnect(DisconnectData {
|
||||||
|
channel_id,
|
||||||
|
guild_id,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
|
// TODO: Remove data from GuildVoiceData
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
// We won't be registering this struct for any more event classes.
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn play_file(call: Arc<Mutex<Call>>, file: String) {
|
||||||
|
let mut call = call.lock().await;
|
||||||
|
let ff_src = ffmpeg(file).await.expect("Unable to find file.");
|
||||||
|
let (audio, handle) = create_player(ff_src);
|
||||||
|
call.play(audio);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn join(ctx: Context, guild: Guild, cid: ChannelId) -> Option<Arc<Mutex<Call>>> {
|
||||||
|
let manager = songbird::get(&ctx).await.expect("Songbird: intialization");
|
||||||
|
let (call, status) = manager.join(guild.id, cid).await;
|
||||||
|
match status {
|
||||||
|
Ok(_) => {
|
||||||
|
let vc_data: VCData = VCData::new(
|
||||||
|
CallLocation {
|
||||||
|
guild: guild.id,
|
||||||
|
channel: cid
|
||||||
|
}, VoiceData::new(
|
||||||
|
call.clone()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
{
|
||||||
|
let data = ctx.data.read().await;
|
||||||
|
match data.get::<VCData>() {
|
||||||
|
Some(vc_guild) => {
|
||||||
|
let mut vc_guild = vc_guild.write().await;
|
||||||
|
vc_guild.insert(CallLocation {
|
||||||
|
guild: guild.id,
|
||||||
|
channel: cid
|
||||||
|
}, vc_data.clone());
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
println!("VoiceData for client hasn't been initialized");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let call_handle = call.clone();
|
||||||
|
{
|
||||||
|
let mut call = call.lock().await;
|
||||||
|
|
||||||
|
call.add_global_event(
|
||||||
|
CoreEvent::SpeakingStateUpdate.into(),
|
||||||
|
vc_data.clone()
|
||||||
|
);
|
||||||
|
|
||||||
|
call.add_global_event(
|
||||||
|
CoreEvent::SpeakingUpdate.into(),
|
||||||
|
vc_data.clone()
|
||||||
|
);
|
||||||
|
|
||||||
|
call.add_global_event(
|
||||||
|
CoreEvent::VoicePacket.into(),
|
||||||
|
vc_data.clone()
|
||||||
|
);
|
||||||
|
|
||||||
|
call.add_global_event(
|
||||||
|
CoreEvent::RtcpPacket.into(),
|
||||||
|
vc_data.clone()
|
||||||
|
);
|
||||||
|
|
||||||
|
call.add_global_event(
|
||||||
|
CoreEvent::ClientDisconnect.into(),
|
||||||
|
vc_data.clone()
|
||||||
|
);
|
||||||
|
|
||||||
|
call.add_global_event(
|
||||||
|
CoreEvent::DriverConnect.into(),
|
||||||
|
vc_data.clone()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let ding_src = std::env::var("DING_SOUND").expect("DING not found in DING_SOUND");
|
||||||
|
play_file(call_handle, ding_src).await;
|
||||||
|
return Some(call);
|
||||||
|
}
|
||||||
|
Err(_err) => {
|
||||||
|
println!("Error joining channel");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user