Cycling Join
Yeeeessh, cyyclic async functions are weird.
This commit is contained in:
@@ -22,7 +22,6 @@ impl EventHandler for Handler {
|
||||
|
||||
async fn guild_create(&self, ctx: Context, guild: Guild, _new: bool) {
|
||||
println!("Guild joined: {}", guild.name);
|
||||
|
||||
guild_popin::add_guild(ctx, guild).await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,18 @@
|
||||
use rand::prelude::*;
|
||||
use serenity::model::id::GuildId;
|
||||
use serenity::Client;
|
||||
use songbird::{create_player, ffmpeg, Call};
|
||||
use std::collections::HashMap;
|
||||
use std::ops::Range;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
use rand::prelude::*;
|
||||
use serenity::Client;
|
||||
use songbird::error::JoinError;
|
||||
use songbird::{ffmpeg, create_player};
|
||||
use songbird::id::ChannelId;
|
||||
use std::thread::sleep;
|
||||
use std::collections::HashMap;
|
||||
use serenity::model::id::GuildId;
|
||||
use std::time::Duration;
|
||||
|
||||
use serenity::model::prelude::{Guild, ChannelType};
|
||||
use serenity::model::prelude::{ChannelType, Guild};
|
||||
use serenity::prelude::{Context, RwLock, TypeMapKey};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct GuildPopIn {
|
||||
last_join: Instant,
|
||||
min: u64, // milliseconds
|
||||
max: u64, // milliseconds
|
||||
}
|
||||
@@ -26,9 +23,8 @@ impl TypeMapKey for GuildPopIn {
|
||||
|
||||
fn init_pop_state() -> GuildPopIn {
|
||||
GuildPopIn {
|
||||
last_join: Instant::now(),
|
||||
min: 1000,
|
||||
max: 5000
|
||||
min: 1000,
|
||||
max: 5000,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,70 +32,102 @@ async fn popin(ctx: Context, guild: Guild) {
|
||||
println!("Gonna join, I'll do it");
|
||||
let mut most = None;
|
||||
let mut n = 0;
|
||||
for (id,chan) in guild.channels {
|
||||
match chan.guild() {
|
||||
Some(g_chan) => {
|
||||
if g_chan.kind == ChannelType::Voice {
|
||||
match g_chan.members(&ctx.cache).await {
|
||||
Ok(members) => {
|
||||
if members.len() > n {
|
||||
n = members.len();
|
||||
most = Some(id);
|
||||
}
|
||||
},
|
||||
Err(_error) => {
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
None => {
|
||||
|
||||
}
|
||||
}
|
||||
for (id, chan) in guild.clone().channels {
|
||||
match chan.guild() {
|
||||
Some(g_chan) => {
|
||||
if g_chan.kind == ChannelType::Voice {
|
||||
match g_chan.members(&ctx.cache).await {
|
||||
Ok(members) => {
|
||||
if members.len() > n {
|
||||
n = members.len();
|
||||
most = Some(id);
|
||||
}
|
||||
}
|
||||
Err(_error) => {}
|
||||
};
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
match most {
|
||||
Some(chan) => {
|
||||
let manager = songbird::get(&ctx).await
|
||||
.expect("Songbird: intialization");
|
||||
let (call,status) = manager.join(guild.id, chan).await;
|
||||
match status {
|
||||
Ok(_) => {
|
||||
let mut call = call.lock().await;
|
||||
let ding_src = std::env::var("DING_SOUND")
|
||||
.expect("DING not found in DING_SOUND");
|
||||
let ding = ffmpeg(ding_src)
|
||||
.await
|
||||
.expect("no ding.");
|
||||
let (mut audio, handle) = create_player(ding);
|
||||
call.play(audio);
|
||||
},
|
||||
Err(_err) => {
|
||||
println!("Error joining channel")
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
println!("No good channel to join")
|
||||
}
|
||||
Some(chan) => {
|
||||
let manager = songbird::get(&ctx).await.expect("Songbird: intialization");
|
||||
let (call, status) = manager.join(guild.id, chan).await;
|
||||
match status {
|
||||
Ok(_) => {
|
||||
let mut call = call.lock().await;
|
||||
let ding_src =
|
||||
std::env::var("DING_SOUND").expect("DING not found in DING_SOUND");
|
||||
let ding = ffmpeg(ding_src).await.expect("no ding.");
|
||||
let (audio, handle) = create_player(ding);
|
||||
call.play(audio);
|
||||
tokio::spawn(popout_soon(ctx.clone(), guild, Some(call.clone())));
|
||||
}
|
||||
Err(_err) => {
|
||||
println!("Error joining channel");
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
println!("No good channel to join");
|
||||
}
|
||||
}
|
||||
// tokio::spawn(popout_soon(ctx.clone(), guild, None));
|
||||
}
|
||||
|
||||
async fn popin_soon(ctx: Context, guild: Guild) {
|
||||
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 popwhen = pops.get(&guild.id).expect(
|
||||
format!("PopIn: guild {} not found", guild.name.as_str()).as_str()
|
||||
);
|
||||
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
|
||||
start: popwhen.min,
|
||||
end: popwhen.max,
|
||||
});
|
||||
println!("Joining in: {}", 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<Call>) {
|
||||
match call {
|
||||
Some(mut call) => {
|
||||
call.leave().await;
|
||||
},
|
||||
None => {}
|
||||
}
|
||||
start_dingdong(ctx.clone(), guild);
|
||||
}
|
||||
|
||||
async fn popout_soon(ctx: Context, guild: Guild, call: Option<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) {
|
||||
@@ -109,11 +137,13 @@ pub async fn init(client: &Client) {
|
||||
|
||||
pub async fn add_guild(ctx: Context, guild: Guild) {
|
||||
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;
|
||||
pops.insert(guild.id, init_pop_state());
|
||||
println!("GuildsPopIns: {:?}", pops);
|
||||
tokio::spawn(popin_soon(ctx.clone(), guild));
|
||||
let mut pops = pops.write().await;
|
||||
pops.insert(guild.id, init_pop_state());
|
||||
println!("GuildsPopIns: {:?}", pops);
|
||||
start_dingdong(ctx.clone(), guild);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user