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) {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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::ops::Range;
|
||||||
use std::sync::Arc;
|
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::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};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct GuildPopIn {
|
struct GuildPopIn {
|
||||||
last_join: Instant,
|
|
||||||
min: u64, // milliseconds
|
min: u64, // milliseconds
|
||||||
max: u64, // milliseconds
|
max: u64, // milliseconds
|
||||||
}
|
}
|
||||||
@@ -26,9 +23,8 @@ impl TypeMapKey for GuildPopIn {
|
|||||||
|
|
||||||
fn init_pop_state() -> GuildPopIn {
|
fn init_pop_state() -> GuildPopIn {
|
||||||
GuildPopIn {
|
GuildPopIn {
|
||||||
last_join: Instant::now(),
|
min: 1000,
|
||||||
min: 1000,
|
max: 5000,
|
||||||
max: 5000
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,70 +32,102 @@ 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 => {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
match most {
|
match most {
|
||||||
Some(chan) => {
|
Some(chan) => {
|
||||||
let manager = songbird::get(&ctx).await
|
let manager = songbird::get(&ctx).await.expect("Songbird: intialization");
|
||||||
.expect("Songbird: intialization");
|
let (call, status) = manager.join(guild.id, chan).await;
|
||||||
let (call,status) = manager.join(guild.id, chan).await;
|
match status {
|
||||||
match status {
|
Ok(_) => {
|
||||||
Ok(_) => {
|
let mut call = call.lock().await;
|
||||||
let mut call = call.lock().await;
|
let ding_src =
|
||||||
let ding_src = std::env::var("DING_SOUND")
|
std::env::var("DING_SOUND").expect("DING not found in DING_SOUND");
|
||||||
.expect("DING not found in DING_SOUND");
|
let ding = ffmpeg(ding_src).await.expect("no ding.");
|
||||||
let ding = ffmpeg(ding_src)
|
let (audio, handle) = create_player(ding);
|
||||||
.await
|
call.play(audio);
|
||||||
.expect("no ding.");
|
tokio::spawn(popout_soon(ctx.clone(), guild, Some(call.clone())));
|
||||||
let (mut audio, handle) = create_player(ding);
|
}
|
||||||
call.play(audio);
|
Err(_err) => {
|
||||||
},
|
println!("Error joining channel");
|
||||||
Err(_err) => {
|
}
|
||||||
println!("Error joining channel")
|
}
|
||||||
}
|
}
|
||||||
}
|
None => {
|
||||||
},
|
println!("No good channel to join");
|
||||||
None => {
|
}
|
||||||
println!("No good channel to join")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// tokio::spawn(popout_soon(ctx.clone(), guild, None));
|
||||||
}
|
}
|
||||||
|
|
||||||
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<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) {
|
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) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user