Rand & Join
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -819,6 +819,7 @@ dependencies = [
|
||||
name = "new_alan"
|
||||
version = "0.0.1"
|
||||
dependencies = [
|
||||
"rand",
|
||||
"serenity",
|
||||
"songbird",
|
||||
"tokio",
|
||||
|
||||
@@ -12,4 +12,5 @@ tracing-futures = "0.2.5"
|
||||
serenity = { version = "0.11.5", features = ["voice", "client", "rustls_backend", "standard_framework"] }
|
||||
tokio = { version = "1.26.0", features = ["macros", "rt-multi-thread", "signal"] }
|
||||
songbird = "0.3.1"
|
||||
rand = "0.8.5"
|
||||
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
use std::ops::Range;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration,Instant};
|
||||
use std::time::{Duration, Instant};
|
||||
use rand::prelude::*;
|
||||
use songbird::id::ChannelId;
|
||||
use std::thread::sleep;
|
||||
use std::collections::HashMap;
|
||||
use serenity::model::id::GuildId;
|
||||
|
||||
use serenity::model::prelude::Guild;
|
||||
use serenity::model::prelude::{Guild, ChannelType};
|
||||
use serenity::prelude::{Context, RwLock, TypeMapKey};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct GuildPopIn {
|
||||
last_join: Instant,
|
||||
min: Duration,
|
||||
max: Duration,
|
||||
min: u64, // milliseconds
|
||||
max: u64, // milliseconds
|
||||
}
|
||||
|
||||
impl TypeMapKey for GuildPopIn {
|
||||
@@ -20,11 +24,47 @@ impl TypeMapKey for GuildPopIn {
|
||||
fn init_pop_state() -> GuildPopIn {
|
||||
GuildPopIn {
|
||||
last_join: Instant::now(),
|
||||
min: Duration::from_secs(1),
|
||||
max: Duration::from_secs(5)
|
||||
min: 1000,
|
||||
max: 5000
|
||||
}
|
||||
}
|
||||
|
||||
async fn popin(ctx: Context, guild: Guild) {
|
||||
for (_id,chan) in guild.channels {
|
||||
match chan.guild() {
|
||||
Some(g_chan) => {
|
||||
if g_chan.kind == ChannelType::Voice {
|
||||
|
||||
let manager = songbird::get(&ctx).await
|
||||
.expect("Songbird: intialization");
|
||||
let (_,_status) = manager.join(guild.id, _id).await;
|
||||
break;
|
||||
}
|
||||
},
|
||||
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 = 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
|
||||
});
|
||||
sleep(Duration::from_millis(join_in));
|
||||
tokio::spawn(popin(ctx.clone(),guild));
|
||||
}
|
||||
|
||||
pub async fn init(ctx: Context) {
|
||||
let mut data = ctx.data.write().await;
|
||||
data.insert::<GuildPopIn>(Arc::new(RwLock::new(HashMap::default())))
|
||||
@@ -37,5 +77,6 @@ pub async fn add_guild(ctx: Context, guild: Guild) {
|
||||
let mut pops = pops.write().await;
|
||||
pops.insert(guild.id, init_pop_state());
|
||||
println!("GuildsPopIns: {:?}", pops);
|
||||
tokio::spawn(popin_soon(ctx.clone(), guild));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user