Skip to content

Commit

Permalink
Keep threads archived based on tags
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Feb 8, 2024
1 parent 60c8ba0 commit 15652e6
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 6 deletions.
16 changes: 10 additions & 6 deletions discord_bot/src/commands/close_ticket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use indoc::formatdoc;
use poise::{
serenity_prelude::{
ButtonStyle, CreateButton, CreateEmbed, CreateEmbedFooter, CreateMessage, EditThread,
ReactionType, Timestamp,
ForumTag, ReactionType, Timestamp,
},
CreateReply, ReplyHandle,
};

use crate::{check_is_contributor, webhooks::GetTagId, Context, WinstonError};

#[derive(Debug, poise::ChoiceParameter)]
enum CloseReason {
pub enum CloseReason {
#[name = "✅ Resolved"]
Resolved,
#[name = "⛔ Declined"]
Expand Down Expand Up @@ -41,6 +41,13 @@ impl CloseReason {
CloseReason::Duplicate => 0x1F85DE,
}
}

pub fn is_close_tag(tag: &ForumTag) -> bool {
match tag.name.to_lowercase().as_str() {
"resolved" | "declined" | "unreproducible" | "duplicate" => true,
_ => false,
}
}
}

impl Display for CloseReason {
Expand Down Expand Up @@ -130,10 +137,7 @@ pub async fn close_ticket(
channel
.edit_thread(
ctx,
EditThread::default()
.applied_tags(vec![tag])
.locked(true)
.archived(true),
EditThread::default().applied_tags(vec![tag]).archived(true),
)
.await?;

Expand Down
2 changes: 2 additions & 0 deletions discord_bot/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod close_ticket;
mod create_task;
mod task_fixed;
mod thread_archiving;
mod ticket_reopen;

pub use close_ticket::*;
pub use create_task::*;
pub use task_fixed::*;
pub use thread_archiving::*;
pub use ticket_reopen::*;
71 changes: 71 additions & 0 deletions discord_bot/src/commands/thread_archiving.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use async_trait::async_trait;
use poise::serenity_prelude::{Context, EditThread, EventHandler, GuildChannel};

use crate::{CloseReason, TICKET_FORUM_ID};

pub struct ThreadArchivingHandler;

#[async_trait]
impl EventHandler for ThreadArchivingHandler {
/// Only if the thread has
async fn thread_update(&self, ctx: Context, old: Option<GuildChannel>, new: GuildChannel) {
// If the thread was updated and the archived state changed
let old_archive = old
.and_then(|old| old.thread_metadata)
.map(|meta| meta.archived)
.unwrap_or(false);
let new_archive = new
.thread_metadata
.map(|meta| meta.archived)
.unwrap_or(false);

if old_archive == new_archive {
return;
}

// Check that the parent is the ticket forum
let Some(parent) = new.parent_id else {
return;
};
let parent = match parent.to_channel(&ctx).await {
Ok(parent) => parent,
Err(e) => {
eprintln!("Error getting parent channel: {}", e);
return;
}
};

let Some(parent) = parent.guild() else {
return;
};

if parent.id != TICKET_FORUM_ID {
return;
}

let available_tags = parent.available_tags;
let close_tags = available_tags
.iter()
.filter(|tag| CloseReason::is_close_tag(tag))
.collect::<Vec<_>>();

let has_close_tag = new
.applied_tags
.iter()
.any(|tag| close_tags.iter().any(|close_tag| close_tag.id == *tag));

if new_archive == has_close_tag {
return;
}

let mut new = new;

if let Err(e) = new
.edit_thread(&ctx, EditThread::default().archived(has_close_tag))
.await
{
eprintln!("Error editing thread: {}", e);
return;
}
}
}
5 changes: 5 additions & 0 deletions discord_bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub type ApplicationContext<'a> = poise::ApplicationContext<'a, Data, WinstonErr

const GUILD_ID: serenity::GuildId = serenity::GuildId::new(1054708062520360960);
const CONTRIBUTOR_ROLE_ID: serenity::RoleId = serenity::RoleId::new(1054708457535713350);
const TICKET_FORUM_ID: u64 = 1199700329948782613;

const CLICKUP_LIST_ID: &str = "901502296591";
const CLICKUP_USER_ID: u32 = 62541886;
Expand Down Expand Up @@ -130,6 +131,7 @@ async fn startup_discord_bot() {
let client = serenity::ClientBuilder::new(discord_token, intents)
.event_handler(TaskFixedHandler)
.event_handler(TicketReopenHandler)
.event_handler(ThreadArchivingHandler)
.framework(framework)
.await;

Expand Down Expand Up @@ -194,4 +196,7 @@ pub enum WinstonError {

#[error("Failed to parse json: {0}")]
ParseJson(#[from] serde_json::Error),

#[error("Tag not found: {0}")]
TagNotFound(String),
}

0 comments on commit 15652e6

Please sign in to comment.