Rand & Join
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -819,6 +819,7 @@ dependencies = [
|
|||||||
name = "new_alan"
|
name = "new_alan"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"rand",
|
||||||
"serenity",
|
"serenity",
|
||||||
"songbird",
|
"songbird",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
|||||||
@@ -12,4 +12,5 @@ tracing-futures = "0.2.5"
|
|||||||
serenity = { version = "0.11.5", features = ["voice", "client", "rustls_backend", "standard_framework"] }
|
serenity = { version = "0.11.5", features = ["voice", "client", "rustls_backend", "standard_framework"] }
|
||||||
tokio = { version = "1.26.0", features = ["macros", "rt-multi-thread", "signal"] }
|
tokio = { version = "1.26.0", features = ["macros", "rt-multi-thread", "signal"] }
|
||||||
songbird = "0.3.1"
|
songbird = "0.3.1"
|
||||||
|
rand = "0.8.5"
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,20 @@
|
|||||||
|
use std::ops::Range;
|
||||||
use std::sync::Arc;
|
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 std::collections::HashMap;
|
||||||
use serenity::model::id::GuildId;
|
use serenity::model::id::GuildId;
|
||||||
|
|
||||||
use serenity::model::prelude::Guild;
|
use serenity::model::prelude::{Guild, ChannelType};
|
||||||
use serenity::prelude::{Context, RwLock, TypeMapKey};
|
use serenity::prelude::{Context, RwLock, TypeMapKey};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct GuildPopIn {
|
struct GuildPopIn {
|
||||||
last_join: Instant,
|
last_join: Instant,
|
||||||
min: Duration,
|
min: u64, // milliseconds
|
||||||
max: Duration,
|
max: u64, // milliseconds
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TypeMapKey for GuildPopIn {
|
impl TypeMapKey for GuildPopIn {
|
||||||
@@ -20,11 +24,47 @@ impl TypeMapKey for GuildPopIn {
|
|||||||
fn init_pop_state() -> GuildPopIn {
|
fn init_pop_state() -> GuildPopIn {
|
||||||
GuildPopIn {
|
GuildPopIn {
|
||||||
last_join: Instant::now(),
|
last_join: Instant::now(),
|
||||||
min: Duration::from_secs(1),
|
min: 1000,
|
||||||
max: Duration::from_secs(5)
|
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) {
|
pub async fn init(ctx: Context) {
|
||||||
let mut data = ctx.data.write().await;
|
let mut data = ctx.data.write().await;
|
||||||
data.insert::<GuildPopIn>(Arc::new(RwLock::new(HashMap::default())))
|
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;
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user