Basic guild_popin

This commit is contained in:
2023-03-11 18:18:36 -05:00
parent c9164cd2db
commit 23887792a2
4 changed files with 55 additions and 3 deletions

View File

@@ -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;
}
}

View File

@@ -16,6 +16,8 @@ use commands::*;
mod events;
use events::*;
mod utils;
#[tokio::main]
async fn main() {
// Trace async functions

41
src/utils/guild_popin.rs Normal file
View File

@@ -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<RwLock<HashMap<GuildId, GuildPopIn>>>;
}
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::<GuildPopIn>(Arc::new(RwLock::new(HashMap::default())))
}
pub async fn addGuild(ctx: Context, guild: Guild) {
let data = ctx.data.write().await;
let pops = data.get::<GuildPopIn>().expect("Guild Popin States Not Initialized");
{
let mut pops = pops.write().await;
let popin = pops.insert(guild.id, init_pop_state());
println!("GuildsPopIns: {:?}", pops);
}
}

View File

@@ -0,0 +1 @@
pub mod guild_popin;