Skip to content

Commit

Permalink
feat: clan wars contributions are now posted in auto archived thread
Browse files Browse the repository at this point in the history
  • Loading branch information
motzel committed Mar 1, 2024
1 parent 361f1fa commit 6ab91d6
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions src/discord/worker/clan_contribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use std::sync::Arc;

use chrono::Utc;
use cli_table::{format::Justify, Cell, ColorChoice, Table};
use poise::serenity_prelude::{CreateAllowedMentions, CreateAttachment, CreateMessage};
use poise::serenity_prelude::{
AutoArchiveDuration, ChannelType, CreateAllowedMentions, CreateAttachment, CreateMessage,
CreateThread,
};
use serde::{Deserialize, Serialize};
use tokio_util::sync::CancellationToken;

Expand Down Expand Up @@ -213,17 +216,17 @@ impl BlClanContributionWorker {
"Maps".cell(),
"Total PP".cell(),
"Contributed PP".cell(),
"Efficiency".cell(),
"PP efficiency".cell(),
"Contribution".cell(),
"Score".cell(),
"Credits".cell(),
])
.color_choice(ColorChoice::Never);

match table.display() {
Ok(table_display) => {
//
let file_contents = format!(
"// {} //\n\nCaptured maps: {}\nTotal PP: {:.2}\n\n{}",
"// {} //\n\nCaptured maps: {}\nTotal captured PP: {:.2}\n\n{}",
clan_stats.clan_tag,
clan_stats.maps_count,
clan_stats.total_pp,
Expand All @@ -232,7 +235,7 @@ impl BlClanContributionWorker {

let message = CreateMessage::new()
.content(
"The current contribution of players to the maps captured by the clan:",
format!("Current player contributions to maps captured by the {} clan (<t:{}:R>)", clan_stats.clan_tag.clone(), Utc::now().timestamp()),
)
.add_file(CreateAttachment::bytes(
file_contents,
Expand All @@ -243,7 +246,34 @@ impl BlClanContributionWorker {
.send_message(&self.context, message)
.await
{
Ok(_) => {}
Ok(msg) => {
// create new thread if possible
tracing::info!("Creating clan wars contribution thread for the clan {}...", clan_stats.clan_tag.clone());

let thread_name = format!(
"{} clan wars contribution",
clan_stats.clan_tag,
);
match clan_wars_channel_id
.create_thread_from_message(
&self.context,
msg.id,
CreateThread::new(thread_name)
.auto_archive_duration(
AutoArchiveDuration::OneHour,
)
.kind(ChannelType::PublicThread),
)
.await
{
Ok(_) => {
tracing::info!("Clan wars contribution for the clan {} thread created.", clan_stats.clan_tag.clone());
},
Err(err) => {
tracing::error!("Can not create clan wars contribution thread for the clan {} on channel #{}: {}", clan_stats.clan_tag.clone(),clan_wars_channel_id, err);
},
};
}
Err(err) => {
tracing::error!("Can not post clan wars contribution to channel #{}: {}", clan_wars_channel_id, err);
}
Expand Down

0 comments on commit 6ab91d6

Please sign in to comment.