Update youmubot-cf

This commit is contained in:
Natsu Kagami 2024-02-18 00:39:14 +01:00 committed by Natsu Kagami
parent 08d639944e
commit 2db59905b2
5 changed files with 93 additions and 77 deletions

View file

@ -5,9 +5,8 @@ use crate::{
use announcer::MemberToChannels; use announcer::MemberToChannels;
use chrono::Utc; use chrono::Utc;
use codeforces::{RatingChange, User}; use codeforces::{RatingChange, User};
use serenity::{http::CacheHttp, model::id::UserId, CacheAndHttp}; use serenity::{builder::CreateMessage, http::CacheHttp, model::id::UserId};
use std::sync::Arc; use youmubot_prelude::{announcer::CacheAndHttp, *};
use youmubot_prelude::*;
type Client = <CFClient as TypeMapKey>::Value; type Client = <CFClient as TypeMapKey>::Value;
@ -18,7 +17,7 @@ pub struct Announcer;
impl youmubot_prelude::Announcer for Announcer { impl youmubot_prelude::Announcer for Announcer {
async fn updates( async fn updates(
&mut self, &mut self,
http: Arc<CacheAndHttp>, http: CacheAndHttp,
data: AppData, data: AppData,
channels: MemberToChannels, channels: MemberToChannels,
) -> Result<()> { ) -> Result<()> {
@ -68,7 +67,7 @@ impl youmubot_prelude::Announcer for Announcer {
} }
async fn update_user( async fn update_user(
http: Arc<CacheAndHttp>, http: CacheAndHttp,
channels: &MemberToChannels, channels: &MemberToChannels,
client: &Client, client: &Client,
user_id: UserId, user_id: UserId,
@ -124,15 +123,15 @@ async fn update_user(
channels channels
.iter() .iter()
.map(|channel| { .map(|channel| {
channel.send_message(http.http(), |e| { channel.send_message(
e.content(format!("Rating change for {}!", user_id.mention())) http.http(),
.embed(|c| { CreateMessage::new()
crate::embed::rating_change_embed( .content(format!("Rating change for {}!", user_id.mention()))
&rc, &info, &contest, user_id, c, .embed(crate::embed::rating_change_embed(
&rc, &info, &contest, user_id,
)),
) )
}) })
})
})
.collect::<stream::FuturesUnordered<_>>() .collect::<stream::FuturesUnordered<_>>()
.map(|v| v.map(|_| ())) .map(|v| v.map(|_| ()))
.try_collect::<()>() .try_collect::<()>()

View file

@ -1,6 +1,9 @@
use codeforces::{Contest, RatingChange, User}; use codeforces::{Contest, RatingChange, User};
use inflector::Inflector; use inflector::Inflector;
use serenity::{builder::CreateEmbed, utils::MessageBuilder}; use serenity::{
builder::{CreateEmbed, CreateEmbedAuthor},
utils::MessageBuilder,
};
use std::borrow::Borrow; use std::borrow::Borrow;
use youmubot_prelude::*; use youmubot_prelude::*;
@ -9,7 +12,7 @@ fn unwrap_or_ref<'a, T: ?Sized, B: Borrow<T>>(opt: &'a Option<B>, default: &'a T
} }
/// Create an embed representing the user. /// 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 rank = unwrap_or_ref(&user.rank, "Unranked").to_title_case();
let max_rank = unwrap_or_ref(&user.max_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); 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())) .filter_map(|v| v.as_ref().map(|v| v.as_str()))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", "); .join(", ");
e.color(user.color()) CreateEmbed::new()
.author(|a| a.name(&rank)) .color(user.color())
.author(CreateEmbedAuthor::new(&rank))
.thumbnail(user.title_photo.to_string()) .thumbnail(user.title_photo.to_string())
.title(&user.handle) .title(&user.handle)
.url(user.profile_url()) .url(user.profile_url())
@ -61,38 +65,38 @@ pub fn rating_change_embed<'a>(
user: &User, user: &User,
contest: &Contest, contest: &Contest,
user_id: serenity::model::id::UserId, user_id: serenity::model::id::UserId,
e: &'a mut CreateEmbed, ) -> CreateEmbed {
) -> &'a mut CreateEmbed {
let delta = rating_change.new_rating - rating_change.old_rating; let delta = rating_change.new_rating - rating_change.old_rating;
let color = if delta < 0 { 0xff0000 } else { 0x00ff00 }; let color = if delta < 0 { 0xff0000 } else { 0x00ff00 };
let message = if delta > 0 { let message = if delta > 0 {
MessageBuilder::new() MessageBuilder::new()
.push(user_id.mention()) .push(user_id.mention().to_string())
.push(" competed in ") .push(" competed in ")
.push_bold_safe(&contest.name) .push_bold_safe(&contest.name)
.push(", gaining ") .push(", gaining ")
.push_bold_safe(delta) .push_bold_safe(delta.to_string())
.push(" rating placing at ") .push(" rating placing at ")
.push_bold(format!("#{}", rating_change.rank)) .push_bold(format!("#{}", rating_change.rank))
.push("! 🎂🎂🎂") .push("! 🎂🎂🎂")
.build() .build()
} else { } else {
MessageBuilder::new() MessageBuilder::new()
.push(user_id.mention()) .push(user_id.mention().to_string())
.push(" competed in ") .push(" competed in ")
.push_bold_safe(&contest.name) .push_bold_safe(&contest.name)
.push(", but lost ") .push(", but lost ")
.push_bold_safe(-delta) .push_bold_safe((-delta).to_string())
.push(" rating placing at ") .push(" rating placing at ")
.push_bold(format!("#{}", rating_change.rank)) .push_bold(format!("#{}", rating_change.rank))
.push("... 😭😭😭") .push("... 😭😭😭")
.build() .build()
}; };
e.author(|a| { CreateEmbed::new()
a.icon_url(user.avatar.to_string()) .author({
CreateEmbedAuthor::new(&user.handle)
.icon_url(user.avatar.to_string())
.url(user.profile_url()) .url(user.profile_url())
.name(&user.handle)
}) })
.color(color) .color(color)
.description(message) .description(message)

View file

@ -3,6 +3,7 @@ use codeforces::{Contest, Problem};
use dashmap::DashMap as HashMap; use dashmap::DashMap as HashMap;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use regex::{Captures, Regex}; use regex::{Captures, Regex};
use serenity::builder::CreateMessage;
use serenity::{ use serenity::{
builder::CreateEmbed, framework::standard::CommandError, model::channel::Message, builder::CreateEmbed, framework::standard::CommandError, model::channel::Message,
utils::MessageBuilder, utils::MessageBuilder,
@ -126,10 +127,12 @@ impl Hook for InfoHook {
.await; .await;
if !matches.is_empty() { if !matches.is_empty() {
m.channel_id m.channel_id
.send_message(&ctx, |c| { .send_message(
c.content("Here are the info of the given Codeforces links!") &ctx,
.embed(|e| print_info_message(&matches[..], e)) CreateMessage::new()
}) .content("Here are the info of the given Codeforces links!")
.embed(print_info_message(&matches[..])),
)
.await?; .await?;
} }
Ok(()) Ok(())
@ -149,10 +152,7 @@ fn parse<'a>(
matches matches
} }
fn print_info_message<'a>( fn print_info_message<'a>(info: &[(ContestOrProblem, &str)]) -> CreateEmbed {
info: &[(ContestOrProblem, &str)],
e: &'a mut CreateEmbed,
) -> &'a mut CreateEmbed {
let (problems, contests): (Vec<_>, Vec<_>) = info.iter().partition(|(v, _)| match v { let (problems, contests): (Vec<_>, Vec<_>) = info.iter().partition(|(v, _)| match v {
ContestOrProblem::Problem(_) => true, ContestOrProblem::Problem(_) => true,
ContestOrProblem::Contest(_, _) => false, ContestOrProblem::Contest(_, _) => false,
@ -236,7 +236,7 @@ fn print_info_message<'a>(
m.push_line(""); m.push_line("");
} }
} }
e.description(m.build()) CreateEmbed::new().description(m.build())
} }
#[allow(clippy::needless_lifetimes)] // Doesn't really work #[allow(clippy::needless_lifetimes)] // Doesn't really work

View file

@ -1,5 +1,6 @@
use codeforces::Contest; use codeforces::Contest;
use serenity::{ use serenity::{
builder::{CreateMessage, EditMessage},
framework::standard::{ framework::standard::{
macros::{command, group}, macros::{command, group},
Args, CommandResult, Args, CommandResult,
@ -83,12 +84,13 @@ pub async fn profile(ctx: &Context, m: &Message, mut args: Args) -> CommandResul
match account { match account {
Some(v) => { Some(v) => {
m.channel_id m.channel_id
.send_message(&ctx, |send| { .send_message(&ctx, {
send.content(format!( CreateMessage::new()
.content(format!(
"{}: Here is the user that you requested", "{}: Here is the user that you requested",
m.author.mention() m.author.mention()
)) ))
.embed(|e| embed::user_embed(&v, e)) .embed(embed::user_embed(&v))
}) })
.await .await
} }
@ -231,7 +233,7 @@ pub async fn ranks(ctx: &Context, m: &Message) -> CommandResult {
last_updated.to_rfc2822() last_updated.to_rfc2822()
)); ));
msg.edit(ctx, |f| f.content(m.build())).await?; msg.edit(ctx, EditMessage::new().content(m.build())).await?;
Ok(true) Ok(true)
}) })
}, },
@ -410,7 +412,7 @@ pub(crate) async fn contest_rank_table(
.push_line(contest.url()) .push_line(contest.url())
.push_codeblock(table.build(), None) .push_codeblock(table.build(), None)
.push_line(format!("Page **{}/{}**", page + 1, total_pages)); .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) Ok(true)
}) })
}, },

View file

@ -2,6 +2,7 @@ use crate::{db::CfSavedUsers, hook::ContestCache, CFClient};
use chrono::TimeZone; use chrono::TimeZone;
use codeforces::{Contest, ContestPhase, Problem, ProblemResult, ProblemResultType, RanklistRow}; use codeforces::{Contest, ContestPhase, Problem, ProblemResult, ProblemResultType, RanklistRow};
use serenity::{ use serenity::{
builder::{CreateMessage, EditMessage},
model::{ model::{
guild::Member, guild::Member,
id::{ChannelId, GuildId, UserId}, id::{ChannelId, GuildId, UserId},
@ -73,9 +74,11 @@ pub async fn watch_contest(
Some(t) => t, Some(t) => t,
None => { None => {
channel channel
.send_message(ctx, |f| { .send_message(
f.content(format!("Contest is already being watched: {}", contest_id)) ctx,
}) CreateMessage::new()
.content(format!("Contest is already being watched: {}", contest_id)),
)
.await?; .await?;
return Ok(()); return Ok(());
} }
@ -85,9 +88,10 @@ pub async fn watch_contest(
Ok((p, _)) => p, Ok((p, _)) => p,
Err(e) => { Err(e) => {
channel channel
.send_message(ctx, |f| { .send_message(
f.content(format!("Cannot get info about contest: {}", e)) ctx,
}) CreateMessage::new().content(format!("Cannot get info about contest: {}", e)),
)
.await?; .await?;
return Ok(()); return Ok(());
} }
@ -101,20 +105,22 @@ pub async fn watch_contest(
Some(s) => s, Some(s) => s,
None => { None => {
channel channel
.send_message(ctx, |f| { .send_message(
f.content(format!( ctx,
CreateMessage::new().content(format!(
"Contest **{}** found, but we don't know when it will begin!", "Contest **{}** found, but we don't know when it will begin!",
contest.name contest.name
)) )),
}) )
.await?; .await?;
return Ok(()); return Ok(());
} }
}; };
channel channel
.send_message(ctx, |f| { .send_message(
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("<t:%s:R>"))) 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("<t:%s:R>")))
)
.await?; .await?;
tokio::time::sleep( tokio::time::sleep(
(start_time - chrono::Utc::now() + chrono::Duration::seconds(30)) (start_time - chrono::Utc::now() + chrono::Duration::seconds(30))
@ -125,7 +131,10 @@ pub async fn watch_contest(
} }
let mut msg = channel 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?; .await?;
let http = data.get::<CFClient>().unwrap(); let http = data.get::<CFClient>().unwrap();
@ -155,8 +164,9 @@ pub async fn watch_contest(
.collect() .collect()
.await; .await;
msg.edit(&ctx, |e| { msg.edit(
e.content(format!( &ctx,
EditMessage::new().content(format!(
"Youmu is watching contest **{}**, with the following members: {}", "Youmu is watching contest **{}**, with the following members: {}",
contest.name, contest.name,
member_results member_results
@ -169,8 +179,8 @@ pub async fn watch_contest(
.build()) .build())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", "), .join(", "),
)) )),
}) )
.await?; .await?;
msg.pin(ctx).await.ok(); 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 { if let Ok(messages) = scan_changes(http, &mut member_results, &mut contest).await {
for message in messages { for message in messages {
channel channel
.send_message(&ctx, |e| { .send_message(
e.content(format!("**{}**: {}", contest.name, message)) &ctx,
}) CreateMessage::new().content(format!("**{}**: {}", contest.name, message)),
)
.await .await
.ok(); .ok();
} }