From 23887792a232cf6ddd99cc067f3f854647e5292e Mon Sep 17 00:00:00 2001 From: "DavidCrompton1192@gmail.com" Date: Sat, 11 Mar 2023 18:18:36 -0500 Subject: [PATCH] Basic guild_popin --- src/events.rs | 14 +++++++++++--- src/main.rs | 2 ++ src/utils/guild_popin.rs | 41 ++++++++++++++++++++++++++++++++++++++++ src/utils/mod.rs | 1 + 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 src/utils/guild_popin.rs diff --git a/src/events.rs b/src/events.rs index 9c6e2c3..377ac09 100644 --- a/src/events.rs +++ b/src/events.rs @@ -4,6 +4,8 @@ use serenity::model::prelude::Guild; use serenity::prelude::Context; use serenity::prelude::*; +use crate::utils::*; + pub struct Handler; #[async_trait] @@ -14,11 +16,17 @@ impl EventHandler for Handler { // private channels, and more. // // In this case, just print what the current user's username is. - async fn ready(&self, _ctx: Context, ready: Ready) { + async fn ready(&self, ctx: Context, ready: Ready) { println!("{} is connected!", ready.user.name); + println!("Initialize guild voice popin state"); + + guildPopin::init(ctx).await; } - async fn guild_create(&self, _ctx: Context, guild: Guild, _new: bool) { - println!("Guild joined: {}", guild.name); + async fn guild_create(&self, ctx: Context, guild: Guild, _new: bool) { + println!("Guild joined: {}", guild.name); + + guildPopin::addGuild(ctx, guild).await; } } + diff --git a/src/main.rs b/src/main.rs index ec0344d..cffde60 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,8 @@ use commands::*; mod events; use events::*; +mod utils; + #[tokio::main] async fn main() { // Trace async functions diff --git a/src/utils/guild_popin.rs b/src/utils/guild_popin.rs new file mode 100644 index 0000000..74e7ae0 --- /dev/null +++ b/src/utils/guild_popin.rs @@ -0,0 +1,41 @@ +use std::sync::Arc; +use std::time::{Duration,Instant}; +use std::collections::HashMap; +use serenity::model::id::GuildId; + +use serenity::model::prelude::Guild; +use serenity::prelude::{Context, RwLock, TypeMapKey}; + +#[derive(Debug)] +struct GuildPopIn { + last_join: Instant, + min: Duration, + max: Duration, +} + +impl TypeMapKey for GuildPopIn { + type Value = Arc>>; +} + +fn init_pop_state() -> GuildPopIn { + GuildPopIn { + last_join: Instant::now(), + min: Duration::from_secs(1), + max: Duration::from_secs(5) + } +} + +pub async fn init(ctx: Context) { + let mut data = ctx.data.write().await; + data.insert::(Arc::new(RwLock::new(HashMap::default()))) +} + +pub async fn addGuild(ctx: Context, guild: Guild) { + let data = ctx.data.write().await; + let pops = data.get::().expect("Guild Popin States Not Initialized"); + { + let mut pops = pops.write().await; + let popin = pops.insert(guild.id, init_pop_state()); + println!("GuildsPopIns: {:?}", pops); + } +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index e69de29..aa202b0 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -0,0 +1 @@ +pub mod guild_popin;