From 4451379b2042bd649ff213baa4bfffaa1335741d Mon Sep 17 00:00:00 2001 From: Jeffrey Meng Date: Thu, 31 Oct 2024 00:07:57 -0400 Subject: [PATCH] minor improvements --- Cargo.lock | 17 +++++++++++++++ Cargo.toml | 2 ++ src/create_pairing.rs | 7 +++---- src/helpers.rs | 5 +++-- src/send_pairing.rs | 49 +++++++++++++++++++------------------------ src/types.rs | 2 +- 6 files changed, 47 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0c7dfa8..9c163e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,6 +41,12 @@ dependencies = [ "libc", ] +[[package]] +name = "anyhow" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c042108f3ed77fd83760a5fd79b53be043192bb3b9dba91d8c574c0ada7850c8" + [[package]] name = "arrayvec" version = "0.7.6" @@ -775,10 +781,12 @@ checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" name = "matchy_meetups_bot" version = "0.1.0" dependencies = [ + "anyhow", "itertools", "poise", "rand", "rand_chacha", + "redb", "serenity", "tokio", ] @@ -1015,6 +1023,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "redb" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84b1de48a7cf7ba193e81e078d17ee2b786236eed1d3f7c60f8a09545efc4925" +dependencies = [ + "libc", +] + [[package]] name = "redox_syscall" version = "0.5.7" diff --git a/Cargo.toml b/Cargo.toml index 38d91fc..11186f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,5 @@ tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros"] } rand = "0.8.5" rand_chacha = "0.3.1" itertools = "0.13.0" +redb = "2.2.0" +anyhow = "1.0.91" diff --git a/src/create_pairing.rs b/src/create_pairing.rs index 78fef8a..858ef78 100644 --- a/src/create_pairing.rs +++ b/src/create_pairing.rs @@ -1,9 +1,9 @@ use crate::helpers::log_error; use crate::pair::pair; -use crate::types::{Context, Error}; +use crate::types::Context; +use anyhow::Result; use itertools::Itertools; use poise::futures_util::future::join_all; - /// Generate a potential pairing of users who have reacted to a message #[poise::command( slash_command, @@ -18,8 +18,7 @@ pub async fn create_pairing( ctx: Context<'_>, #[description = "Link to a message with reactions -- a pairing will be made between users who reacted."] message_link: String, -) -> Result<(), Error> { - println!("{message_link}"); +) -> Result<()> { let message_id = message_link .split("/") .last() diff --git a/src/helpers.rs b/src/helpers.rs index 1f1942a..16efc90 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,5 +1,6 @@ -use crate::types::{Data, Error}; +use crate::types::Data; +use anyhow::Error; pub async fn log_error(error: poise::FrameworkError<'_, Data, Error>) { println!("Error: {:?}", error); -} \ No newline at end of file +} diff --git a/src/send_pairing.rs b/src/send_pairing.rs index e8b0b5a..70019c2 100644 --- a/src/send_pairing.rs +++ b/src/send_pairing.rs @@ -1,7 +1,8 @@ use crate::log_error; use crate::pair::pair; -use crate::types::{Context, Error}; -use poise::futures_util::future::join_all; +use crate::types::Context; +use anyhow::Error; +use poise::futures_util::future::{join_all, try_join_all}; /// Send a message to each member of the pairing. #[poise::command( @@ -60,38 +61,30 @@ pub async fn send_pairing( for pair in pairs { for user in &pair { let pairing: Vec<_> = pair.iter().filter(|u| *u != user).collect(); - let pairing_str = if pairing.len() == 1 { - // TODO: parallelize these async requests and check API ratelimiting - // TODO: the display name should be whatever the server - let pairing1 = pairing[0].to_user(&ctx).await?; - format!( - "<@{}> ({})", - pairing1.id, - pairing1.global_name.unwrap_or(pairing1.name) - ) - } else { - let pairing1 = pairing[0].to_user(&ctx).await?; - let pairing2 = pairing[1].to_user(&ctx).await?; - format!( - "<@{}> ({}) and <${}> ({})", - pairing1.id, - pairing1.global_name.unwrap_or(pairing1.name), - pairing2.id, - pairing2.global_name.unwrap_or(pairing2.name) - ) - }; + let pairing_str = try_join_all(pairing.iter().map(|uid| { + return async { + let u = uid.to_user(&ctx).await?; + Ok::(format!( + "<@{}> ({})", + u.id, + u.global_name.unwrap_or(u.name) + )) + }; + })) + .await? + .join(" and "); let message_str = format!("Hey, thanks for joining ICSSC's Matchy Meetups. Your pairing \ - for this round is with {pairing_str}! Please take this opportunity to reach out to them and \ + for this round is here! Please take this opportunity to reach out to them and \ schedule some time to hang out in the next two weeks. \ Don't forget to send pics to https://discord.com/channels/760915616793755669/1199228930222194779 \ - while you're there, and I hope you enjoy!\n\n\ - \t\t\t \\- Jeffrey \n\n\ - _(responses here will not be seen; please message Jeffrey directly if there are any issues)_"); + while you're there, and I hope you enjoy!\n\ + \t\t\t\t\t\t\t \\- Jeffrey \n\n\n\ + **Your pairing is with:** {pairing_str}\n\n\ + _(responses here will not be seen; please message Jeffrey directly if you have any questions)_"); let _ = user .create_dm_channel(&ctx) - .await - .unwrap() + .await? .say(&ctx, message_str) .await; diff --git a/src/types.rs b/src/types.rs index d98bd55..7e0091e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,3 +1,3 @@ +use anyhow::Error; pub type Data = (); -pub type Error = Box; pub type Context<'a> = poise::Context<'a, Data, Error>;