Compare commits

..

6 Commits

Author SHA1 Message Date
23887792a2 Basic guild_popin 2023-03-11 18:18:36 -05:00
c9164cd2db Change structure 2023-03-11 11:31:57 -05:00
816ad6f8df Use vec 2023-03-10 18:19:28 -05:00
d4993ab906 Clean Intents 2023-03-10 18:19:20 -05:00
dabefbb342 Guild Create Event 2023-03-10 18:17:21 -05:00
0deb810f20 Start voice lib 2023-03-10 18:17:13 -05:00
4 changed files with 61 additions and 10 deletions

View File

@@ -1,8 +1,11 @@
use serenity::async_trait;
use serenity::model::gateway::Ready;
use serenity::model::prelude::Guild;
use serenity::prelude::Context;
use serenity::prelude::*;
use crate::utils::*;
pub struct Handler;
#[async_trait]
@@ -13,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);
// for g_id in ctx.cache.guilds() {
// let g = ctx.cache.guild(g_id).expect("Unable to fetch guild");
// println!("Initializing {}...", g.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);
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
@@ -23,14 +25,12 @@ async fn main() {
// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
// Set gateway intents, which decides what events the bot will be notified about
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::GUILD_VOICE_STATES
| GatewayIntents::GUILD_MESSAGE_REACTIONS
| GatewayIntents::GUILD_MESSAGE_TYPING
let intents = GatewayIntents::GUILDS
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::DIRECT_MESSAGE_REACTIONS
| GatewayIntents::DIRECT_MESSAGE_TYPING
| GatewayIntents::MESSAGE_CONTENT;
| GatewayIntents::MESSAGE_CONTENT
| GatewayIntents::GUILD_VOICE_STATES;
let framework = StandardFramework::new()
.configure(|c| c.prefix("ALAN! "))

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

1
src/utils/mod.rs Normal file
View File

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