Skip to content

Commit

Permalink
feat: schedule system to remove roles
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioRibera committed Aug 26, 2024
1 parent b68b760 commit 58fd202
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 39 deletions.
18 changes: 16 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ gen_welcome = { version = "0.1.0", path = "crates/gen_welcome" }
urlencoding = "2.1.3"
lazy_static = "1.4.0"
time = "0.3.36"
tokio_schedule = "0.3.2"
chrono = "0.4.38"

[dependencies.parking_lot]
version = "0.12"
Expand Down
70 changes: 33 additions & 37 deletions src/events/new_members_mention.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serenity::all::{ChannelId, Context, CreateMessage, EventHandler, Message, RoleId};
use serenity::all::{ChannelId, Context, EventHandler, Message, RoleId};
use serenity::async_trait;
use std::collections::HashMap;
use tokio_schedule::Job;

pub struct NewMembersMention;

Expand All @@ -18,49 +18,45 @@ async fn log(ctx: &Context, msg: impl serde::Serialize) {
#[async_trait]
impl EventHandler for NewMembersMention {
async fn message(&self, ctx: Context, msg: Message) {
if !msg.content.starts_with("!new members") {
if !(msg.mention_roles.contains(&NEW_MEMBERS_ROLE_ID) && msg.channel_id == WELCOME_CHANNEL)
{
tracing::info!("No hubo mencion");
return;
}
// if !(msg.mention_roles.contains(&NEW_MEMBERS_ROLE_ID) && msg.channel_id == WELCOME_CHANNEL)
// {
// return;
// }

// let Some(guild) = msg.guild(ctx.cache.as_ref()) else {
// ctx.http
// .send_message(
// INTERNAL_LOGS,
// Vec::new(),
// &format!("Cannot get GUILD from message: {}", msg.clone().link()),
// )
// .await
// .unwrap();
// return;
// };

let mut message = "Resultado de reseteo de roles para los nuevos:\n\n".to_owned();

let members = msg
.guild(ctx.cache.as_ref())
.unwrap()
.members
.iter()
.filter(|(_, v)| v.roles.contains(&NEW_MEMBERS_ROLE_ID))
.for_each(|(_, v)| message.push_str(&format!("- {}\n", v.display_name())));

// for (_, v) in members.iter() {
// let emoji = if v.remove_role(&ctx, NEW_MEMBERS_ROLE_ID).await.is_ok() {
// ":white_check_mark:"
// } else {
// ":x:"
// };
// }

if !message.is_empty() {
INTERNAL_LOGS
.send_message(&ctx, CreateMessage::new().content(&message))
.await
.unwrap();
.filter_map(|(_, v)| v.roles.contains(&NEW_MEMBERS_ROLE_ID).then(|| v.clone()))
.collect::<Vec<_>>();

tracing::info!("New Members: {}", members.len());

{
let members = members.clone();
let remove_role = tokio_schedule::every(30)
.minute()
.until(&(chrono::Utc::now() + chrono::Duration::hours(1)))
.in_timezone(&chrono::Utc)
.perform(move || {
let ctx = ctx.clone();
let members = members.clone();
async move {
for v in members.iter() {
if let Err(e) = v.remove_role(&ctx, NEW_MEMBERS_ROLE_ID).await {
tracing::error!(
"Failed to remove role of: {} - {:?}\nReason: {e:?}",
v.display_name(),
v.nick
);
}
}
}
});

tokio::spawn(remove_role);
}
}
}

0 comments on commit 58fd202

Please sign in to comment.