feat: when someone joins, if their account name is in the list then they are automatically added
This commit is contained in:
parent
f382784141
commit
58def2e01e
1 changed files with 27 additions and 11 deletions
38
src/main.rs
38
src/main.rs
|
@ -3,8 +3,8 @@ use std::sync::atomic::{AtomicBool, Ordering};
|
|||
use std::time::Duration;
|
||||
|
||||
use serenity::async_trait;
|
||||
use serenity::model::channel::Message;
|
||||
use serenity::model::gateway::{GatewayIntents, Presence, Ready};
|
||||
use serenity::model::gateway::{GatewayIntents, Ready};
|
||||
use serenity::model::guild::Member;
|
||||
use serenity::model::id::GuildId;
|
||||
use serenity::prelude::*;
|
||||
use serenity::model::prelude::RoleId;
|
||||
|
@ -15,17 +15,33 @@ struct Handler {
|
|||
|
||||
#[async_trait]
|
||||
impl EventHandler for Handler {
|
||||
// This event will be dispatched for guilds, but not for direct messages.
|
||||
async fn message(&self, _ctx: Context, msg: Message) {
|
||||
println!("Received message: {}", msg.content);
|
||||
async fn guild_member_addition(&self, ctx: Context, mut new_member: Member) {
|
||||
let config = Config {
|
||||
server: GuildId(957961810147938334),
|
||||
member_role_current: RoleId::from(1144760670995370094),
|
||||
member_role_past: RoleId::from(1144760548072886353),
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
let members_lock = {
|
||||
let data_read = ctx.data.read().await;
|
||||
data_read.get::<Members>().expect("Expected Members in TypeMap.").clone()
|
||||
};
|
||||
|
||||
// As the intents set in this example, this event shall never be dispatched.
|
||||
// Try it by changing your status.
|
||||
async fn presence_update(&self, _ctx: Context, _new_data: Presence) {
|
||||
println!("Presence Update");
|
||||
let members = members_lock.read().await;
|
||||
if members.contains(&new_member.user.name) {
|
||||
let mut roles = vec![];
|
||||
|
||||
if !new_member.roles.contains(&config.member_role_past){
|
||||
roles.push(config.member_role_past);
|
||||
}
|
||||
if !new_member.roles.contains(&config.member_role_current){
|
||||
roles.push(config.member_role_current);
|
||||
}
|
||||
|
||||
if let Err(e) = new_member.add_roles(&ctx, &roles).await {
|
||||
println!("{:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn ready(&self, ctx: Context, ready: Ready) {
|
||||
|
|
Loading…
Reference in a new issue