Skip to content

Commit 4fdc4d4

Browse files
committed
add open/resolved tags
1 parent 34080bd commit 4fdc4d4

File tree

3 files changed

+92
-5
lines changed

3 files changed

+92
-5
lines changed

src/bot.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,19 @@ export class Bot {
7474
return botAdmins.includes(user.id);
7575
}
7676

77-
getTrustedMemberError(msg: Message) {
77+
isTrusted(msg: Message) {
7878
if (!msg.guild || !msg.member || !msg.channel.isTextBased()) {
79-
return ":warning: you can't use that command here.";
79+
return false;
8080
}
8181

8282
if (
8383
!msg.member.roles.cache.has(trustedRoleId) &&
8484
!msg.member.permissions.has('ManageMessages')
8585
) {
86-
return ":warning: you don't have permission to use that command.";
86+
return false;
8787
}
88+
89+
return true;
8890
}
8991

9092
async getTargetUser(msg: Message): Promise<User | undefined> {

src/env.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ export const howToGiveHelpChannel = process.env.HOW_TO_GIVE_HELP_CHANNEL!;
2121
export const helpForumChannel = process.env.HELP_FORUM_CHANNEL!;
2222
export const helpRequestsChannel = process.env.HELP_REQUESTS_CHANNEL!;
2323

24+
export const helpForumOpenTagName = process.env.HELP_FORUM_OPEN_TAG!;
25+
export const helpForumResolvedTagName = process.env.HELP_FORUM_RESOLVED_TAG!;
26+
2427
export const trustedRoleId = process.env.TRUSTED_ROLE_ID!;
2528

2629
export const rulesChannelId = process.env.RULES_CHANNEL!;

src/modules/helpForum.ts

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
import { ChannelType, ThreadChannel, TextChannel, Channel } from 'discord.js';
1+
import {
2+
ChannelType,
3+
ThreadChannel,
4+
TextChannel,
5+
Channel,
6+
ForumChannel,
7+
Message,
8+
} from 'discord.js';
29
import { Bot } from '../bot';
310
import { HelpThread } from '../entities/HelpThread';
411
import {
512
helpForumChannel,
13+
helpForumOpenTagName,
14+
helpForumResolvedTagName,
615
helpRequestsChannel,
716
howToGetHelpChannel,
817
howToGiveHelpChannel,
@@ -12,6 +21,8 @@ import {
1221
} from '../env';
1322
import { sendWithMessageOwnership } from '../util/send';
1423

24+
const MAX_TAG_COUNT = 5;
25+
1526
// Use a non-breaking space to force Discord to leave empty lines alone
1627
const postGuidelines = (here = true) =>
1728
listify(`
@@ -49,6 +60,13 @@ const howToGiveHelp = listify(`
4960
- \`!ask\` — for if an asker only posts "can I get help?"
5061
`);
5162

63+
const helperResolve = (owner: string, helper: string) => `
64+
<@${owner}>
65+
Because your issue seemed to be resolved, this post was marked as resolved by <@${helper}>.
66+
If your issue is not resolved, **you can reopen this post by running \`!reopen\`**.
67+
*If you have a different question, make a new post in <#${helpForumChannel}>.*
68+
`;
69+
5270
export async function helpForumModule(bot: Bot) {
5371
const channel = await bot.client.guilds.cache
5472
.first()
@@ -58,6 +76,8 @@ export async function helpForumModule(bot: Bot) {
5876
return;
5977
}
6078
const forumChannel = channel;
79+
const openTag = getTag(forumChannel, helpForumOpenTagName);
80+
const resolvedTag = getTag(forumChannel, helpForumResolvedTagName);
6181

6282
const helpRequestChannel = await bot.client.guilds.cache
6383
.first()
@@ -83,6 +103,8 @@ export async function helpForumModule(bot: Bot) {
83103
threadId: thread.id,
84104
ownerId: owner.user.id,
85105
}).save();
106+
107+
await setStatus(thread, openTag);
86108
});
87109

88110
bot.client.on('threadDelete', async thread => {
@@ -108,7 +130,7 @@ export async function helpForumModule(bot: Bot) {
108130

109131
// Ensure the user has permission to ping helpers
110132
const isAsker = msg.author.id === threadData.ownerId;
111-
const isTrusted = bot.getTrustedMemberError(msg) === undefined; // No error if trusted
133+
const isTrusted = bot.isTrusted(msg);
112134

113135
if (!isAsker && !isTrusted) {
114136
return sendWithMessageOwnership(
@@ -160,6 +182,50 @@ export async function helpForumModule(bot: Bot) {
160182
},
161183
});
162184

185+
bot.registerCommand({
186+
aliases: ['resolved', 'resolve', 'close', 'closed', 'done'],
187+
description: 'Help System: Mark a post as resolved',
188+
async listener(msg) {
189+
changeStatus(msg, true);
190+
},
191+
});
192+
193+
bot.registerCommand({
194+
aliases: ['reopen', 'open', 'unresolved', 'unresolve'],
195+
description: 'Help System: Reopen a resolved post',
196+
async listener(msg) {
197+
changeStatus(msg, false);
198+
},
199+
});
200+
201+
async function changeStatus(msg: Message, resolved: boolean) {
202+
const thread = msg.channel;
203+
if (thread?.type !== ChannelType.PublicThread) {
204+
return sendWithMessageOwnership(
205+
msg,
206+
':warning: Can only be run in a help post',
207+
);
208+
}
209+
210+
const threadData = await getHelpThread(thread.id);
211+
const isAsker = msg.author.id === threadData.ownerId;
212+
const isTrusted = bot.isTrusted(msg);
213+
214+
if (!isAsker && !isTrusted) {
215+
return sendWithMessageOwnership(
216+
msg,
217+
':warning: Only the asker can change the status of a help post',
218+
);
219+
}
220+
221+
await setStatus(thread, resolved ? resolvedTag : openTag);
222+
await msg.react('✅');
223+
224+
if (resolved && !isAsker) {
225+
await thread.send(helperResolve(thread.ownerId!, msg.author.id));
226+
}
227+
}
228+
163229
bot.registerAdminCommand({
164230
aliases: ['htgh'],
165231
async listener(msg) {
@@ -205,6 +271,22 @@ export async function helpForumModule(bot: Bot) {
205271
channel.parent?.id === forumChannel.id
206272
);
207273
}
274+
275+
function getTag(channel: ForumChannel, name: string) {
276+
const tag = channel.availableTags.find(x => x.name === name);
277+
if (!tag) throw new Error(`Could not find tag ${name}`);
278+
return tag.id;
279+
}
280+
281+
async function setStatus(thread: ThreadChannel, tag: string) {
282+
let tags = thread.appliedTags.filter(
283+
x => x !== openTag && x !== resolvedTag,
284+
);
285+
if (tags.length === MAX_TAG_COUNT) {
286+
tags = tags.slice(0, -1);
287+
}
288+
await thread.setAppliedTags([tag, ...tags]);
289+
}
208290
}
209291

210292
function listify(text: string) {

0 commit comments

Comments
 (0)