-
Notifications
You must be signed in to change notification settings - Fork 15
feat: add command to ping random members #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
7ef33c7
08dcde9
df10f43
487732d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| use crate::{ | ||
| ids::{FIRST_YEAR_ROLE_ID, SECOND_YEAR_ROLE_ID}, | ||
| Context, Error, | ||
| }; | ||
| use rand::seq::IteratorRandom; | ||
| use serenity::all::{Mentionable as _, RoleId, UserId}; | ||
| use std::collections::HashSet; | ||
|
|
||
| #[poise::command(slash_command)] | ||
| pub async fn random(ctx: Context<'_>) -> Result<(), Error> { | ||
|
hrideshmg marked this conversation as resolved.
Outdated
|
||
| let guild = ctx.guild_id().ok_or("No guild id")?; | ||
| let members = guild.members(ctx.http(), None, None).await?; | ||
|
hrideshmg marked this conversation as resolved.
|
||
|
|
||
|
AnandajithS marked this conversation as resolved.
|
||
| let first_year_role = RoleId::new(FIRST_YEAR_ROLE_ID); | ||
| let second_year_role = RoleId::new(SECOND_YEAR_ROLE_ID); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are only first and second years included as eligible members? Ideally one should be able to specify which all years they want to target with it falling back to all 3 as a default |
||
| let eligible_members: Vec<_> = members // Filtering out bots and other ineligible members | ||
| .into_iter() | ||
| .filter(|m| { | ||
| !m.user.bot | ||
| && (m.roles.contains(&first_year_role) || m.roles.contains(&second_year_role)) | ||
| }) | ||
| .collect(); | ||
|
|
||
| if eligible_members.is_empty() { | ||
| ctx.say("No eligible members found.").await?; | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let recent_picks = { | ||
| // Accessing recently picked members to avoid repetition | ||
| let data = ctx.data(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is |
||
| let recent_random_picks = data.recent_random_picks.lock().unwrap(); | ||
| recent_random_picks.clone() | ||
|
Comment on lines
+47
to
+49
|
||
| }; | ||
|
|
||
| let available_members: Vec<_> = eligible_members // Members who haven't been picked recently | ||
| .iter() | ||
| .filter(|member| !recent_picks.contains(&member.user.id)) | ||
| .collect(); | ||
|
|
||
| /* Since ThreadRng is not Send, keeping it alive across an .await causes command future to become non-Send. | ||
| So, we are enclosing the selection in it's own scope so the ThreadRng is dropped before we hit any .await, | ||
|
hrideshmg marked this conversation as resolved.
|
||
| allowing the command future to remain Send | ||
| */ | ||
|
|
||
| let selected = { | ||
| let mut rng = rand::thread_rng(); | ||
|
|
||
| let mut selected: Vec<_> = available_members.into_iter().choose_multiple(&mut rng, 5); | ||
|
|
||
| if selected.len() < 5 { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. number should be configurable via the slash syntax rather than hardcoded to 5 |
||
| // If not enough members are available, fetch recently picked members | ||
| let remaining_needed = 5 - selected.len(); | ||
|
|
||
| let selected_ids: HashSet<UserId> = selected.iter().map(|m| m.user.id).collect(); | ||
|
|
||
| let additional: Vec<_> = eligible_members | ||
| .iter() | ||
| .filter(|m| !selected_ids.contains(&m.user.id)) | ||
| .choose_multiple(&mut rng, remaining_needed); | ||
|
|
||
| selected.extend(additional); | ||
| } | ||
| selected | ||
| }; | ||
|
hrideshmg marked this conversation as resolved.
|
||
|
|
||
| let ping_message = selected | ||
| .iter() | ||
| .map(|m| m.user.mention().to_string()) | ||
| .collect::<Vec<_>>() | ||
| .join("\n"); | ||
|
|
||
| { | ||
| let data = ctx.data(); | ||
| let mut recent_random_picks = data.recent_random_picks.lock().unwrap(); | ||
| recent_random_picks.extend(selected.iter().map(|m| m.user.id)); // Adding selected members to recently picked set | ||
|
Comment on lines
+92
to
+94
|
||
|
|
||
| let eligible_ids: HashSet<UserId> = eligible_members.iter().map(|m| m.user.id).collect(); | ||
| recent_random_picks.retain(|user_id| eligible_ids.contains(user_id)); | ||
| if recent_random_picks.len() >= eligible_ids.len() { | ||
| // If all eligible members have been picked atleast once, clear the recently picked set | ||
| recent_random_picks.clear(); | ||
|
hrideshmg marked this conversation as resolved.
|
||
| } | ||
| } | ||
| ctx.say(format!( | ||
| "Pinging {} members: {}", | ||
| selected.len(), | ||
| ping_message | ||
| )) | ||
| .await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.