discord-bot/src/main.rs

144 lines
4.8 KiB
Rust
Raw Normal View History

2023-08-25 22:36:46 +00:00
use std::env;
use std::future::Future;
use std::sync::Arc;
2023-08-25 22:36:46 +00:00
use serenity::async_trait;
use serenity::model::channel::Message;
use serenity::model::gateway::{GatewayIntents, Presence, Ready};
use serenity::model::id::GuildId;
2023-08-25 22:36:46 +00:00
use serenity::prelude::*;
use serenity::model::prelude::RoleId;
2023-08-25 22:36:46 +00:00
struct Handler;
// eoghanconlon73
#[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) {
2023-08-25 22:36:46 +00:00
println!("Received message: {}", msg.content);
2023-08-25 22:36:46 +00:00
}
// 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");
}
async fn ready(&self, ctx: Context, ready: Ready) {
2023-08-25 22:36:46 +00:00
println!("{} is connected!", ready.user.name);
let config = Config {
members: vec![String::from("silver_golden"), String::from("eoghanconlon73")],
server: GuildId(957961810147938334),
member_role_current: RoleId::from(1144760670995370094),
member_role_past: RoleId::from(1144760548072886353),
};
bulk_check(&ctx, &config).await;
}
}
struct Config {
members: Vec<String>,
server: GuildId,
member_role_current: RoleId,
member_role_past: RoleId,
}
#[derive(Default)]
struct Members {
members: i32,
members_current: i32,
}
struct MemberCounter;
impl TypeMapKey for MemberCounter {
type Value = Arc<RwLock<Members>>;
}
async fn bulk_check(ctx: &Context, config: &Config){
let mut roles_set = [0,0,0];
let mut res = Members {
members: 0,
members_current: 0,
};
if let Ok(x) = config.server.members(ctx, None, None).await {
for mut member in x {
if config.members.contains(&member.user.name) {
let mut roles = vec![];
if !member.roles.contains(&config.member_role_past){
roles_set[0] += 1;
roles.push(config.member_role_past);
}
if !member.roles.contains(&config.member_role_current){
roles_set[1] += 1;
roles.push(config.member_role_current);
}
if let Err(e) = member.add_roles(&ctx, &roles).await {
println!("{:?}", e);
}
} else if member.roles.contains(&config.member_role_current) {
roles_set[2] += 1;
// if theya re not a current member and have the role then remove it
if let Err(e) = member.remove_role(&ctx, &config.member_role_current).await {
println!("{:?}", e);
}
}
if member.roles.contains(&config.member_role_past){
res.members += 1;
}
if member.roles.contains(&config.member_role_current){
res.members_current += 1;
}
}
2023-08-25 22:36:46 +00:00
}
// small bit of logging to note changes over time
println!("Changes: New: +{}, Current: +{}/-{}", roles_set[0], roles_set[1], roles_set[2]);
let counter_lock = {
let data_read = ctx.data.read().await;
data_read.get::<MemberCounter>().expect("Expected MemberCounter in TypeMap.").clone()
};
{
// The HashMap of CommandCounter is wrapped in an RwLock; since we want to write to it, we will
// open the lock in write mode.
let mut counter = counter_lock.write().await;
// And we write the amount of times the command has been called to it.
counter.members_current = res.members_current;
counter.members = res.members;
}
2023-08-25 22:36:46 +00:00
}
#[tokio::main]
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");
let token = String::from("");
// Intents are a bitflag, bitwise operations can be used to dictate which intents to use
let intents = GatewayIntents::GUILDS | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT | GatewayIntents::GUILD_MEMBERS;
// Build our client.
let mut client = Client::builder(token, intents)
.event_handler(Handler)
.await
.expect("Error creating client");
{
let mut data = client.data.write().await;
data.insert::<MemberCounter>(Arc::new(RwLock::new(Members::default())));
}
2023-08-25 22:36:46 +00:00
// Finally, start a single shard, and start listening to events.
//
// Shards will automatically attempt to reconnect, and will perform
// exponential backoff until it reconnects.
if let Err(why) = client.start().await {
println!("Client error: {:?}", why);
}
}