|
1 | 1 | import { config } from "@lib/config.js";
|
2 | 2 |
|
3 |
| -import { canUserInteractWithThread, getChannelFromInteraction, isHelpPost } from "@lib/channels.js"; |
4 |
| - |
5 |
| -import { type ThreadChannel, MessageFlags, SlashCommandBuilder, type ChatInputCommandInteraction } from "discord.js"; |
| 3 | +import { |
| 4 | + canUserInteractWithThread, |
| 5 | + getChannelFromInteraction, |
| 6 | + isHelpPost, |
| 7 | +} from "@lib/channels.js"; |
| 8 | + |
| 9 | +import { |
| 10 | + type ThreadChannel, |
| 11 | + MessageFlags, |
| 12 | + SlashCommandBuilder, |
| 13 | + type ChatInputCommandInteraction, |
| 14 | +} from "discord.js"; |
6 | 15 |
|
7 | 16 | // TODO: find a better way to do this
|
8 |
| -const getStateWord = (close) => close ? "closed" : "reopened"; |
9 |
| -const getStateVerb = (close) => close ? "close" : "reopene"; |
10 |
| - |
11 |
| -export async function handleIssueState(interaction: ChatInputCommandInteraction, close = true, lock = false) { |
12 |
| - const threadChannel = await getChannelFromInteraction(interaction) as ThreadChannel; |
13 |
| - |
14 |
| - const stateWord = getStateWord(close); |
15 |
| - const stateVerb = getStateVerb(close); |
16 |
| - |
17 |
| - const tagToAdd = close ? config.helpChannel.closedTag : config.helpChannel.openedTag; |
18 |
| - const tagToRemove = close ? config.helpChannel.openedTag : config.helpChannel.closedTag; |
| 17 | +const getStateWord = (close) => (close ? "closed" : "reopened"); |
| 18 | +const getStateVerb = (close) => (close ? "close" : "reopene"); |
| 19 | + |
| 20 | +export async function handleIssueState( |
| 21 | + interaction: ChatInputCommandInteraction, |
| 22 | + close = true, |
| 23 | + lock = false, |
| 24 | +) { |
| 25 | + const threadChannel = (await getChannelFromInteraction( |
| 26 | + interaction, |
| 27 | + )) as ThreadChannel; |
| 28 | + |
| 29 | + const stateWord = getStateWord(close); |
| 30 | + const stateVerb = getStateVerb(close); |
| 31 | + |
| 32 | + const tagToAdd = close |
| 33 | + ? config.helpChannel.closedTag |
| 34 | + : config.helpChannel.openedTag; |
| 35 | + const tagToRemove = close |
| 36 | + ? config.helpChannel.openedTag |
| 37 | + : config.helpChannel.closedTag; |
| 38 | + |
| 39 | + const postTags = threadChannel.appliedTags; |
| 40 | + |
| 41 | + try { |
| 42 | + // Update tags |
| 43 | + if (!postTags.includes(tagToAdd)) { |
| 44 | + postTags.push(tagToAdd); |
| 45 | + } |
19 | 46 |
|
20 |
| - const postTags = threadChannel.appliedTags; |
| 47 | + if (postTags.includes(tagToRemove)) { |
| 48 | + postTags.splice(postTags.indexOf(tagToRemove), 1); |
| 49 | + } |
21 | 50 |
|
22 |
| - try { |
23 |
| - // Update tags |
24 |
| - if (!postTags.includes(tagToAdd)) { |
25 |
| - postTags.push(tagToAdd); |
26 |
| - } |
| 51 | + await threadChannel.setAppliedTags(postTags, "Thread lifecycle"); |
27 | 52 |
|
28 |
| - if (postTags.includes(tagToRemove)) { |
29 |
| - postTags.splice(postTags.indexOf(tagToRemove), 1); |
30 |
| - } |
| 53 | + await interaction.reply({ |
| 54 | + content: `${interaction.user.toString()} ${stateWord} ${lock ? "and locked " : ""}the thread.`, |
| 55 | + flags: [MessageFlags.SuppressNotifications], |
| 56 | + }); |
31 | 57 |
|
32 |
| - await threadChannel.setAppliedTags(postTags, "Thread lifecycle"); |
33 |
| - |
34 |
| - await interaction.reply({ |
35 |
| - content: `${interaction.user.toString()} ${stateWord} ${lock ? "and locked " : ""}the thread.`, |
36 |
| - flags: [ MessageFlags.SuppressNotifications ] |
37 |
| - }); |
38 |
| - |
39 |
| - // Archive/lock the thread if necessary (it seems we can't lock a thread if it's already been archived) |
40 |
| - if (close && !threadChannel.archived) { |
41 |
| - try { |
42 |
| - if(lock) { |
43 |
| - await threadChannel.setLocked(lock); |
44 |
| - } else { |
45 |
| - await threadChannel.setArchived(true); |
46 |
| - } |
47 |
| - } catch (err) { |
48 |
| - console.error("Error archiving thread:", err); |
49 |
| - } |
| 58 | + // Archive/lock the thread if necessary (it seems we can't lock a thread if it's already been archived) |
| 59 | + if (close && !threadChannel.archived) { |
| 60 | + try { |
| 61 | + if (lock) { |
| 62 | + await threadChannel.setLocked(lock); |
| 63 | + } else { |
| 64 | + await threadChannel.setArchived(true); |
50 | 65 | }
|
51 |
| - } catch (e) { |
52 |
| - await interaction.reply({ |
53 |
| - content: `Could not ${stateVerb} the thread because of an unexpected error.`, |
54 |
| - ephemeral: true, |
55 |
| - }); |
| 66 | + } catch (err) { |
| 67 | + console.error("Error archiving thread:", err); |
| 68 | + } |
56 | 69 | }
|
| 70 | + } catch (e) { |
| 71 | + await interaction.reply({ |
| 72 | + content: `Could not ${stateVerb} the thread because of an unexpected error.`, |
| 73 | + ephemeral: true, |
| 74 | + }); |
| 75 | + } |
57 | 76 | }
|
58 | 77 |
|
59 |
| -export async function handleIssueStateCommand(interaction: ChatInputCommandInteraction, close: boolean, lock = false) { |
60 |
| - const interactionChannel = await getChannelFromInteraction(interaction); |
61 |
| - const stateVerb = getStateVerb(close); |
62 |
| - |
63 |
| - // Check if thread is a help post and if user can interact |
64 |
| - if (await isHelpPost(interactionChannel)) { |
65 |
| - if (canUserInteractWithThread(interaction.channel as ThreadChannel, interaction.user)) { |
66 |
| - return handleIssueState(interaction, close, lock); |
67 |
| - } else { |
68 |
| - await interaction.reply({ |
69 |
| - content: `You cannot ${stateVerb} this thread since you are not the OP.`, |
70 |
| - ephemeral: true, |
71 |
| - }); |
72 |
| - } |
| 78 | +export async function handleIssueStateCommand( |
| 79 | + interaction: ChatInputCommandInteraction, |
| 80 | + close: boolean, |
| 81 | + lock = false, |
| 82 | +) { |
| 83 | + const interactionChannel = await getChannelFromInteraction(interaction); |
| 84 | + const stateVerb = getStateVerb(close); |
| 85 | + |
| 86 | + // Check if thread is a help post and if user can interact |
| 87 | + if (await isHelpPost(interactionChannel)) { |
| 88 | + if ( |
| 89 | + canUserInteractWithThread( |
| 90 | + interaction.channel as ThreadChannel, |
| 91 | + interaction.user, |
| 92 | + ) |
| 93 | + ) { |
| 94 | + return handleIssueState(interaction, close, lock); |
73 | 95 | } else {
|
74 |
| - await interaction.reply({ |
75 |
| - content: `You can only run this command in a <#${config.helpChannel.id}> post.`, |
76 |
| - ephemeral: true, |
77 |
| - }); |
| 96 | + await interaction.reply({ |
| 97 | + content: `You cannot ${stateVerb} this thread since you are not the OP.`, |
| 98 | + ephemeral: true, |
| 99 | + }); |
78 | 100 | }
|
| 101 | + } else { |
| 102 | + await interaction.reply({ |
| 103 | + content: `You can only run this command in a <#${config.helpChannel.id}> post.`, |
| 104 | + ephemeral: true, |
| 105 | + }); |
| 106 | + } |
79 | 107 | }
|
80 | 108 |
|
81 | 109 | export default {
|
82 |
| - data: new SlashCommandBuilder() |
83 |
| - .setName("close") |
84 |
| - .setDescription("Closes your post") |
85 |
| - .addBooleanOption(option => |
86 |
| - option.setName("lock") |
87 |
| - .setDescription("Whether to lock the post or not") |
88 |
| - ), |
89 |
| - |
90 |
| - execute: (interaction: ChatInputCommandInteraction) => handleIssueStateCommand(interaction, true, interaction.options.getBoolean("lock")) |
| 110 | + data: new SlashCommandBuilder() |
| 111 | + .setName("close") |
| 112 | + .setDescription("Closes your post") |
| 113 | + .addBooleanOption((option) => |
| 114 | + option.setName("lock").setDescription("Whether to lock the post or not"), |
| 115 | + ), |
| 116 | + |
| 117 | + execute: (interaction: ChatInputCommandInteraction) => |
| 118 | + handleIssueStateCommand( |
| 119 | + interaction, |
| 120 | + true, |
| 121 | + interaction.options.getBoolean("lock"), |
| 122 | + ), |
91 | 123 | };
|
0 commit comments