Skip to content

Commit 65d073e

Browse files
committed
chore: lint & format
1 parent 2694f21 commit 65d073e

14 files changed

+347
-252
lines changed

src/commands/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export { default as close } from "./util/close.js";
22
export { default as reopen } from "./util/reopen.js";
33

4-
export { default as walkthrough } from "./util/walkthrough.js";
4+
export { default as walkthrough } from "./util/walkthrough.js";

src/commands/util/close.ts

Lines changed: 105 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,123 @@
11
import { config } from "@lib/config.js";
22

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";
615

716
// 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+
}
1946

20-
const postTags = threadChannel.appliedTags;
47+
if (postTags.includes(tagToRemove)) {
48+
postTags.splice(postTags.indexOf(tagToRemove), 1);
49+
}
2150

22-
try {
23-
// Update tags
24-
if (!postTags.includes(tagToAdd)) {
25-
postTags.push(tagToAdd);
26-
}
51+
await threadChannel.setAppliedTags(postTags, "Thread lifecycle");
2752

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+
});
3157

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);
5065
}
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+
}
5669
}
70+
} catch (e) {
71+
await interaction.reply({
72+
content: `Could not ${stateVerb} the thread because of an unexpected error.`,
73+
ephemeral: true,
74+
});
75+
}
5776
}
5877

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);
7395
} 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+
});
78100
}
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+
}
79107
}
80108

81109
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+
),
91123
};

src/commands/util/reopen.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { handleIssueStateCommand } from "./close.js";
22

3-
import { SlashCommandBuilder, type ChatInputCommandInteraction } from "discord.js";
3+
import {
4+
SlashCommandBuilder,
5+
type ChatInputCommandInteraction,
6+
} from "discord.js";
47

58
export default {
6-
data: new SlashCommandBuilder()
7-
.setName("reopen")
8-
.setDescription("Reopens your post"),
9+
data: new SlashCommandBuilder()
10+
.setName("reopen")
11+
.setDescription("Reopens your post"),
912

10-
execute: (interaction: ChatInputCommandInteraction) => handleIssueStateCommand(interaction, false, false)
13+
execute: (interaction: ChatInputCommandInteraction) =>
14+
handleIssueStateCommand(interaction, false, false),
1115
};

src/commands/util/walkthrough.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,37 @@ import { isHelpPost as isHelpThread } from "@lib/channels.js";
44
import issueCategorySelector from "@components/issueCategorySelector.js";
55

66
import {
7-
type ChatInputCommandInteraction, SlashCommandBuilder,
8-
ActionRowBuilder, type StringSelectMenuBuilder, EmbedBuilder, type Embed, Colors,
7+
type ChatInputCommandInteraction,
8+
SlashCommandBuilder,
9+
ActionRowBuilder,
10+
type StringSelectMenuBuilder,
11+
EmbedBuilder,
12+
type Embed,
13+
Colors,
914
type PublicThreadChannel,
10-
type GuildTextBasedChannel
15+
type GuildTextBasedChannel,
1116
} from "discord.js";
1217

13-
export function generateMessage(question: string, component: StringSelectMenuBuilder, embeds: (EmbedBuilder | Embed)[] = []) {
18+
export function generateMessage(
19+
question: string,
20+
component: StringSelectMenuBuilder,
21+
embeds: (EmbedBuilder | Embed)[] = [],
22+
) {
1423
return {
1524
embeds: [
1625
...embeds,
17-
new EmbedBuilder()
18-
.setColor(Colors.White)
19-
.setDescription(question)
26+
new EmbedBuilder().setColor(Colors.White).setDescription(question),
2027
],
2128
components: [
2229
new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(component),
23-
]
24-
}
30+
],
31+
};
2532
}
2633

27-
export async function doWalkthrough(channel: GuildTextBasedChannel, interaction?: ChatInputCommandInteraction) {
34+
export async function doWalkthrough(
35+
channel: GuildTextBasedChannel,
36+
interaction?: ChatInputCommandInteraction,
37+
) {
2838
if (await isHelpThread(channel)) {
2939
const threadChannel = channel as PublicThreadChannel; // necessary type cast, isHelpThread does the check already
3040

@@ -34,9 +44,12 @@ export async function doWalkthrough(channel: GuildTextBasedChannel, interaction?
3444
}
3545

3646
// Generate the message with the action row
37-
const message = generateMessage("What are you creating this issue for?", issueCategorySelector);
47+
const message = generateMessage(
48+
"What are you creating this issue for?",
49+
issueCategorySelector,
50+
);
3851

39-
if(interaction) {
52+
if (interaction) {
4053
// TODO: check if walkthrough has already been sent
4154
return interaction.reply(message);
4255
} else {
@@ -48,11 +61,15 @@ export async function doWalkthrough(channel: GuildTextBasedChannel, interaction?
4861
export default {
4962
data: new SlashCommandBuilder()
5063
.setName("walkthrough")
51-
.setDescription("Sends the walkthrough message in case the bot didn't automatically send it."),
64+
.setDescription(
65+
"Sends the walkthrough message in case the bot didn't automatically send it.",
66+
),
5267

5368
async execute(interaction: ChatInputCommandInteraction) {
54-
const interactionChannel = await interaction.client.channels.fetch(interaction.channelId) as GuildTextBasedChannel;
69+
const interactionChannel = (await interaction.client.channels.fetch(
70+
interaction.channelId,
71+
)) as GuildTextBasedChannel;
5572

5673
return doWalkthrough(interactionChannel);
57-
}
74+
},
5875
};

src/deploy-commands.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ import { REST, Routes } from "discord.js";
66
// Construct and prepare an instance of the REST module
77
const rest = new REST().setToken(config.token);
88

9-
const commandData = Object.values(commands).map(command => command.data);
9+
const commandData = Object.values(commands).map((command) => command.data);
1010

11-
console.log(`Started refreshing ${commandData.length} application (/) commands.`);
11+
console.log(
12+
`Started refreshing ${commandData.length} application (/) commands.`,
13+
);
1214

1315
// The put method is used to fully refresh all commands in the guild with the current set
1416
// biome-ignore lint/suspicious/noExplicitAny: TODO: need to figure out the proper type
1517
const data: any = await rest.put(
16-
Routes.applicationGuildCommands("1063886601165471814", config.serverId), // TODO: guess client ID from token
17-
{ body: commandData },
18+
Routes.applicationGuildCommands("1063886601165471814", config.serverId), // TODO: guess client ID from token
19+
{ body: commandData },
1820
);
1921

20-
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
22+
console.log(`Successfully reloaded ${data.length} application (/) commands.`);

src/events/commands.ts

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,35 @@ import * as commands from "@commands/index.js";
33
import { type Client, Events } from "discord.js";
44

55
export default function registerEvents(client: Client) {
6-
return client.on(Events.InteractionCreate, async (interaction) => {
7-
if (interaction.isChatInputCommand()) {
8-
const command = commands[interaction.commandName];
9-
10-
if (!command) {
11-
console.error(`No command matching ${interaction.commandName} was found.`);
12-
return;
13-
}
14-
15-
try {
16-
await command.execute(interaction);
17-
} catch (error) {
18-
console.error(error);
19-
20-
// TODO: make generic replyOrFollowUp method
21-
if (interaction.replied || interaction.deferred) {
22-
await interaction.followUp({
23-
content: "There was an error while executing this command!",
24-
ephemeral: true,
25-
});
26-
} else {
27-
await interaction.reply({
28-
content: "There was an error while executing this command!",
29-
ephemeral: true,
30-
});
31-
}
32-
}
6+
return client.on(Events.InteractionCreate, async (interaction) => {
7+
if (interaction.isChatInputCommand()) {
8+
const command = commands[interaction.commandName];
9+
10+
if (!command) {
11+
console.error(
12+
`No command matching ${interaction.commandName} was found.`,
13+
);
14+
return;
15+
}
16+
17+
try {
18+
await command.execute(interaction);
19+
} catch (error) {
20+
console.error(error);
21+
22+
// TODO: make generic replyOrFollowUp method
23+
if (interaction.replied || interaction.deferred) {
24+
await interaction.followUp({
25+
content: "There was an error while executing this command!",
26+
ephemeral: true,
27+
});
28+
} else {
29+
await interaction.reply({
30+
content: "There was an error while executing this command!",
31+
ephemeral: true,
32+
});
3333
}
34-
});
35-
}
34+
}
35+
}
36+
});
37+
}

0 commit comments

Comments
 (0)