Skip to content

Commit 714f404

Browse files
committed
send responses to help channel messages in thread
1 parent f5e37d8 commit 714f404

File tree

3 files changed

+43
-32
lines changed

3 files changed

+43
-32
lines changed

src/modules/helpthread.ts

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const titleSetCooldown = 5 * 60 * 1000;
8888
export class HelpThreadModule extends Module {
8989
@listener({ event: 'messageCreate' })
9090
async onNewQuestion(msg: Message) {
91-
if (!this.isHelpChannel(msg.channel)) return;
91+
if (!isHelpChannel(msg.channel)) return;
9292
if (msg.author.id === this.client.user!.id) return;
9393
this.updateHelpInfo(msg.channel);
9494
let thread = await msg.startThread({
@@ -108,7 +108,7 @@ export class HelpThreadModule extends Module {
108108
@listener({ event: 'threadUpdate' })
109109
async onThreadExpire(thread: ThreadChannel) {
110110
if (
111-
!this.isHelpThread(thread) ||
111+
!isHelpThread(thread) ||
112112
!((await thread.fetch()) as ThreadChannel).archived ||
113113
this.manuallyArchivedThreads.delete(thread.id)
114114
)
@@ -123,7 +123,7 @@ export class HelpThreadModule extends Module {
123123
description: 'Help System: Close an active help thread',
124124
})
125125
async close(msg: Message) {
126-
if (!this.isHelpThread(msg.channel))
126+
if (!isHelpThread(msg.channel))
127127
return await sendWithMessageOwnership(
128128
msg,
129129
':warning: This can only be run in a help thread',
@@ -168,38 +168,16 @@ export class HelpThreadModule extends Module {
168168

169169
@listener({ event: 'messageCreate' })
170170
deletePinMessage(msg: Message) {
171-
if (
172-
this.isHelpChannel(msg.channel) &&
173-
msg.type === 'CHANNEL_PINNED_MESSAGE'
174-
)
171+
if (isHelpChannel(msg.channel) && msg.type === 'CHANNEL_PINNED_MESSAGE')
175172
msg.delete();
176173
}
177174

178-
private isHelpChannel(
179-
channel: Omit<Channel, 'partial'>,
180-
): channel is TextChannel {
181-
return (
182-
channel instanceof TextChannel &&
183-
channel.parentId == helpCategory &&
184-
channel.id !== howToGetHelpChannel
185-
);
186-
}
187-
188-
private isHelpThread(
189-
channel: Omit<Channel, 'partial'>,
190-
): channel is ThreadChannel & { parent: TextChannel } {
191-
return (
192-
channel instanceof ThreadChannel &&
193-
this.isHelpChannel(channel.parent!)
194-
);
195-
}
196-
197175
@command({
198176
description: 'Help System: Ping the @Helper role from a help thread',
199177
aliases: ['helpers'],
200178
})
201179
async helper(msg: Message) {
202-
if (!this.isHelpThread(msg.channel)) {
180+
if (!isHelpThread(msg.channel)) {
203181
return sendWithMessageOwnership(
204182
msg,
205183
':warning: You may only ping helpers from a help thread',
@@ -249,7 +227,7 @@ export class HelpThreadModule extends Module {
249227

250228
@command({ single: true, description: 'Help System: Rename a help thread' })
251229
async title(msg: Message, title: string) {
252-
if (!this.isHelpThread(msg.channel))
230+
if (!isHelpThread(msg.channel))
253231
return sendWithMessageOwnership(
254232
msg,
255233
':warning: This can only be run in a help thread',
@@ -296,3 +274,19 @@ export class HelpThreadModule extends Module {
296274
msg.channel.send({ embeds: howToGetHelpEmbeds() });
297275
}
298276
}
277+
278+
export function isHelpChannel(
279+
channel: Omit<Channel, 'partial'>,
280+
): channel is TextChannel {
281+
return (
282+
channel instanceof TextChannel &&
283+
channel.parentId == helpCategory &&
284+
channel.id !== howToGetHelpChannel
285+
);
286+
}
287+
288+
export function isHelpThread(
289+
channel: Omit<Channel, 'partial'>,
290+
): channel is ThreadChannel & { parent: TextChannel } {
291+
return channel instanceof ThreadChannel && isHelpChannel(channel.parent!);
292+
}

src/modules/playground.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ import {
2020
truncate,
2121
} from '../util/codeBlocks';
2222
import { LimitedSizeMap } from '../util/limitedSizeMap';
23-
import { addMessageOwnership, sendWithMessageOwnership } from '../util/send';
23+
import {
24+
addMessageOwnership,
25+
getResponseChannel,
26+
sendWithMessageOwnership,
27+
} from '../util/send';
2428
import fetch from 'node-fetch';
29+
import { isHelpChannel } from './helpthread';
2530

2631
const LINK_SHORTENER_ENDPOINT = 'https://tsplay.dev/api/short';
2732
const MAX_EMBED_LENGTH = 512;
@@ -64,13 +69,14 @@ export class PlaygroundModule extends Module {
6469
const exec = PLAYGROUND_REGEX.exec(msg.content);
6570
if (!exec) return;
6671
const embed = createPlaygroundEmbed(msg.author, exec);
67-
if (exec[0] === msg.content) {
72+
if (exec[0] === msg.content && !isHelpChannel(msg.channel)) {
6873
// Message only contained the link
6974
await sendWithMessageOwnership(msg, { embeds: [embed] });
7075
await msg.delete();
7176
} else {
7277
// Message also contained other characters
73-
const botMsg = await msg.channel.send({
78+
const channel = await getResponseChannel(msg);
79+
const botMsg = await channel.send({
7480
embeds: [embed],
7581
content: `${msg.author} Here's a shortened URL of your playground link! You can remove the full link from your message.`,
7682
});

src/util/send.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
PartialMessage,
77
User,
88
} from 'discord.js';
9+
import { isHelpChannel } from '../modules/helpthread';
910
import { LimitedSizeMap } from './limitedSizeMap';
1011

1112
const messageToUserId = new LimitedSizeMap<
@@ -15,12 +16,22 @@ const messageToUserId = new LimitedSizeMap<
1516

1617
export const DELETE_EMOJI = '🗑️';
1718

19+
export async function getResponseChannel(message: Message) {
20+
const channel = message.channel;
21+
if (!isHelpChannel(channel)) return channel;
22+
while (!message.thread) {
23+
message = await message.fetch();
24+
}
25+
return message.thread;
26+
}
27+
1828
export async function sendWithMessageOwnership(
1929
message: Message,
2030
toSend: string | MessagePayload | MessageOptions,
2131
onDelete?: () => void,
2232
) {
23-
const sent = await message.channel.send(toSend);
33+
const channel = await getResponseChannel(message);
34+
const sent = await channel.send(toSend);
2435
await addMessageOwnership(sent, message.author, onDelete);
2536
}
2637

0 commit comments

Comments
 (0)