Compare commits
6 Commits
ecb0414f12
...
23887792a2
| Author | SHA1 | Date | |
|---|---|---|---|
| 23887792a2 | |||
| c9164cd2db | |||
| 816ad6f8df | |||
| d4993ab906 | |||
| dabefbb342 | |||
| 0deb810f20 |
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
10
src/main.rs
10
src/main.rs
@@ -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
41
src/utils/guild_popin.rs
Normal 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
1
src/utils/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod guild_popin;
|
||||
Reference in New Issue
Block a user