From 2db59905b294b8a037fb3e4c1b9731f4ddccb4cb Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Sun, 18 Feb 2024 00:39:14 +0100 Subject: [PATCH] Update youmubot-cf --- youmubot-cf/src/announcer.rs | 25 ++++++++-------- youmubot-cf/src/embed.rs | 56 +++++++++++++++++++----------------- youmubot-cf/src/hook.rs | 18 ++++++------ youmubot-cf/src/lib.rs | 18 ++++++------ youmubot-cf/src/live.rs | 53 ++++++++++++++++++++-------------- 5 files changed, 93 insertions(+), 77 deletions(-) diff --git a/youmubot-cf/src/announcer.rs b/youmubot-cf/src/announcer.rs index 64e2993..335b19d 100644 --- a/youmubot-cf/src/announcer.rs +++ b/youmubot-cf/src/announcer.rs @@ -5,9 +5,8 @@ use crate::{ use announcer::MemberToChannels; use chrono::Utc; use codeforces::{RatingChange, User}; -use serenity::{http::CacheHttp, model::id::UserId, CacheAndHttp}; -use std::sync::Arc; -use youmubot_prelude::*; +use serenity::{builder::CreateMessage, http::CacheHttp, model::id::UserId}; +use youmubot_prelude::{announcer::CacheAndHttp, *}; type Client = ::Value; @@ -18,7 +17,7 @@ pub struct Announcer; impl youmubot_prelude::Announcer for Announcer { async fn updates( &mut self, - http: Arc, + http: CacheAndHttp, data: AppData, channels: MemberToChannels, ) -> Result<()> { @@ -68,7 +67,7 @@ impl youmubot_prelude::Announcer for Announcer { } async fn update_user( - http: Arc, + http: CacheAndHttp, channels: &MemberToChannels, client: &Client, user_id: UserId, @@ -124,14 +123,14 @@ async fn update_user( channels .iter() .map(|channel| { - channel.send_message(http.http(), |e| { - e.content(format!("Rating change for {}!", user_id.mention())) - .embed(|c| { - crate::embed::rating_change_embed( - &rc, &info, &contest, user_id, c, - ) - }) - }) + channel.send_message( + http.http(), + CreateMessage::new() + .content(format!("Rating change for {}!", user_id.mention())) + .embed(crate::embed::rating_change_embed( + &rc, &info, &contest, user_id, + )), + ) }) .collect::>() .map(|v| v.map(|_| ())) diff --git a/youmubot-cf/src/embed.rs b/youmubot-cf/src/embed.rs index 3551645..cb2ce5e 100644 --- a/youmubot-cf/src/embed.rs +++ b/youmubot-cf/src/embed.rs @@ -1,6 +1,9 @@ use codeforces::{Contest, RatingChange, User}; use inflector::Inflector; -use serenity::{builder::CreateEmbed, utils::MessageBuilder}; +use serenity::{ + builder::{CreateEmbed, CreateEmbedAuthor}, + utils::MessageBuilder, +}; use std::borrow::Borrow; use youmubot_prelude::*; @@ -9,7 +12,7 @@ fn unwrap_or_ref<'a, T: ?Sized, B: Borrow>(opt: &'a Option, default: &'a T } /// Create an embed representing the user. -pub fn user_embed<'a>(user: &User, e: &'a mut CreateEmbed) -> &'a mut CreateEmbed { +pub fn user_embed<'a>(user: &User) -> CreateEmbed { let rank = unwrap_or_ref(&user.rank, "Unranked").to_title_case(); let max_rank = unwrap_or_ref(&user.max_rank, "Unranked").to_title_case(); let rating = user.rating.unwrap_or(1500); @@ -24,8 +27,9 @@ pub fn user_embed<'a>(user: &User, e: &'a mut CreateEmbed) -> &'a mut CreateEmbe .filter_map(|v| v.as_ref().map(|v| v.as_str())) .collect::>() .join(", "); - e.color(user.color()) - .author(|a| a.name(&rank)) + CreateEmbed::new() + .color(user.color()) + .author(CreateEmbedAuthor::new(&rank)) .thumbnail(user.title_photo.to_string()) .title(&user.handle) .url(user.profile_url()) @@ -61,48 +65,48 @@ pub fn rating_change_embed<'a>( user: &User, contest: &Contest, user_id: serenity::model::id::UserId, - e: &'a mut CreateEmbed, -) -> &'a mut CreateEmbed { +) -> CreateEmbed { let delta = rating_change.new_rating - rating_change.old_rating; let color = if delta < 0 { 0xff0000 } else { 0x00ff00 }; let message = if delta > 0 { MessageBuilder::new() - .push(user_id.mention()) + .push(user_id.mention().to_string()) .push(" competed in ") .push_bold_safe(&contest.name) .push(", gaining ") - .push_bold_safe(delta) + .push_bold_safe(delta.to_string()) .push(" rating placing at ") .push_bold(format!("#{}", rating_change.rank)) .push("! 🎂🎂🎂") .build() } else { MessageBuilder::new() - .push(user_id.mention()) + .push(user_id.mention().to_string()) .push(" competed in ") .push_bold_safe(&contest.name) .push(", but lost ") - .push_bold_safe(-delta) + .push_bold_safe((-delta).to_string()) .push(" rating placing at ") .push_bold(format!("#{}", rating_change.rank)) .push("... 😭😭😭") .build() }; - e.author(|a| { - a.icon_url(user.avatar.to_string()) - .url(user.profile_url()) - .name(&user.handle) - }) - .color(color) - .description(message) - .field("Contest Link", contest.url(), true) - .field( - "Rating Change", - format!( - "from **{}** to **{}**", - rating_change.old_rating, rating_change.new_rating - ), - false, - ) + CreateEmbed::new() + .author({ + CreateEmbedAuthor::new(&user.handle) + .icon_url(user.avatar.to_string()) + .url(user.profile_url()) + }) + .color(color) + .description(message) + .field("Contest Link", contest.url(), true) + .field( + "Rating Change", + format!( + "from **{}** to **{}**", + rating_change.old_rating, rating_change.new_rating + ), + false, + ) } diff --git a/youmubot-cf/src/hook.rs b/youmubot-cf/src/hook.rs index 01aa85e..483db33 100644 --- a/youmubot-cf/src/hook.rs +++ b/youmubot-cf/src/hook.rs @@ -3,6 +3,7 @@ use codeforces::{Contest, Problem}; use dashmap::DashMap as HashMap; use lazy_static::lazy_static; use regex::{Captures, Regex}; +use serenity::builder::CreateMessage; use serenity::{ builder::CreateEmbed, framework::standard::CommandError, model::channel::Message, utils::MessageBuilder, @@ -126,10 +127,12 @@ impl Hook for InfoHook { .await; if !matches.is_empty() { m.channel_id - .send_message(&ctx, |c| { - c.content("Here are the info of the given Codeforces links!") - .embed(|e| print_info_message(&matches[..], e)) - }) + .send_message( + &ctx, + CreateMessage::new() + .content("Here are the info of the given Codeforces links!") + .embed(print_info_message(&matches[..])), + ) .await?; } Ok(()) @@ -149,10 +152,7 @@ fn parse<'a>( matches } -fn print_info_message<'a>( - info: &[(ContestOrProblem, &str)], - e: &'a mut CreateEmbed, -) -> &'a mut CreateEmbed { +fn print_info_message<'a>(info: &[(ContestOrProblem, &str)]) -> CreateEmbed { let (problems, contests): (Vec<_>, Vec<_>) = info.iter().partition(|(v, _)| match v { ContestOrProblem::Problem(_) => true, ContestOrProblem::Contest(_, _) => false, @@ -236,7 +236,7 @@ fn print_info_message<'a>( m.push_line(""); } } - e.description(m.build()) + CreateEmbed::new().description(m.build()) } #[allow(clippy::needless_lifetimes)] // Doesn't really work diff --git a/youmubot-cf/src/lib.rs b/youmubot-cf/src/lib.rs index a24adf9..2cbae58 100644 --- a/youmubot-cf/src/lib.rs +++ b/youmubot-cf/src/lib.rs @@ -1,5 +1,6 @@ use codeforces::Contest; use serenity::{ + builder::{CreateMessage, EditMessage}, framework::standard::{ macros::{command, group}, Args, CommandResult, @@ -83,12 +84,13 @@ pub async fn profile(ctx: &Context, m: &Message, mut args: Args) -> CommandResul match account { Some(v) => { m.channel_id - .send_message(&ctx, |send| { - send.content(format!( - "{}: Here is the user that you requested", - m.author.mention() - )) - .embed(|e| embed::user_embed(&v, e)) + .send_message(&ctx, { + CreateMessage::new() + .content(format!( + "{}: Here is the user that you requested", + m.author.mention() + )) + .embed(embed::user_embed(&v)) }) .await } @@ -231,7 +233,7 @@ pub async fn ranks(ctx: &Context, m: &Message) -> CommandResult { last_updated.to_rfc2822() )); - msg.edit(ctx, |f| f.content(m.build())).await?; + msg.edit(ctx, EditMessage::new().content(m.build())).await?; Ok(true) }) }, @@ -410,7 +412,7 @@ pub(crate) async fn contest_rank_table( .push_line(contest.url()) .push_codeblock(table.build(), None) .push_line(format!("Page **{}/{}**", page + 1, total_pages)); - msg.edit(ctx, |e| e.content(m.build())).await?; + msg.edit(ctx, EditMessage::new().content(m.build())).await?; Ok(true) }) }, diff --git a/youmubot-cf/src/live.rs b/youmubot-cf/src/live.rs index 2ae7e75..ae9bfdb 100644 --- a/youmubot-cf/src/live.rs +++ b/youmubot-cf/src/live.rs @@ -2,6 +2,7 @@ use crate::{db::CfSavedUsers, hook::ContestCache, CFClient}; use chrono::TimeZone; use codeforces::{Contest, ContestPhase, Problem, ProblemResult, ProblemResultType, RanklistRow}; use serenity::{ + builder::{CreateMessage, EditMessage}, model::{ guild::Member, id::{ChannelId, GuildId, UserId}, @@ -73,9 +74,11 @@ pub async fn watch_contest( Some(t) => t, None => { channel - .send_message(ctx, |f| { - f.content(format!("Contest is already being watched: {}", contest_id)) - }) + .send_message( + ctx, + CreateMessage::new() + .content(format!("Contest is already being watched: {}", contest_id)), + ) .await?; return Ok(()); } @@ -85,9 +88,10 @@ pub async fn watch_contest( Ok((p, _)) => p, Err(e) => { channel - .send_message(ctx, |f| { - f.content(format!("Cannot get info about contest: {}", e)) - }) + .send_message( + ctx, + CreateMessage::new().content(format!("Cannot get info about contest: {}", e)), + ) .await?; return Ok(()); } @@ -101,20 +105,22 @@ pub async fn watch_contest( Some(s) => s, None => { channel - .send_message(ctx, |f| { - f.content(format!( + .send_message( + ctx, + CreateMessage::new().content(format!( "Contest **{}** found, but we don't know when it will begin!", contest.name - )) - }) + )), + ) .await?; return Ok(()); } }; channel - .send_message(ctx, |f| { - f.content(format!("Contest **{}** found, but has not started yet. Youmu will start watching as soon as it begins! (which is in about {})", contest.name, start_time.format(""))) - }) + .send_message( + ctx, + CreateMessage::new().content(format!("Contest **{}** found, but has not started yet. Youmu will start watching as soon as it begins! (which is in about {})", contest.name, start_time.format(""))) + ) .await?; tokio::time::sleep( (start_time - chrono::Utc::now() + chrono::Duration::seconds(30)) @@ -125,7 +131,10 @@ pub async fn watch_contest( } let mut msg = channel - .send_message(&ctx, |e| e.content("Youmu is building the member list...")) + .send_message( + &ctx, + CreateMessage::new().content("Youmu is building the member list..."), + ) .await?; let http = data.get::().unwrap(); @@ -155,8 +164,9 @@ pub async fn watch_contest( .collect() .await; - msg.edit(&ctx, |e| { - e.content(format!( + msg.edit( + &ctx, + EditMessage::new().content(format!( "Youmu is watching contest **{}**, with the following members: {}", contest.name, member_results @@ -169,8 +179,8 @@ pub async fn watch_contest( .build()) .collect::>() .join(", "), - )) - }) + )), + ) .await?; msg.pin(ctx).await.ok(); @@ -178,9 +188,10 @@ pub async fn watch_contest( if let Ok(messages) = scan_changes(http, &mut member_results, &mut contest).await { for message in messages { channel - .send_message(&ctx, |e| { - e.content(format!("**{}**: {}", contest.name, message)) - }) + .send_message( + &ctx, + CreateMessage::new().content(format!("**{}**: {}", contest.name, message)), + ) .await .ok(); }