Skip to content

Commit 135673a

Browse files
authored
Merge pull request #179 from tjjfvi/thread-fixes
Misc thread fixes
2 parents 2763340 + 3a6cea5 commit 135673a

File tree

4 files changed

+51
-39
lines changed

4 files changed

+51
-39
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/codeBlocks.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ const CODEBLOCK_REGEX = /```(?:ts|typescript)?\n([\s\S]+)```/;
77
export const PLAYGROUND_REGEX = /https?:\/\/(?:www\.)?(?:typescriptlang|staging-typescript)\.org\/(?:play|dev\/bug-workbench)(?:\/index\.html)?\/?(\??(?:\w+=[^\s#&]*)?(?:\&\w+=[^\s#&]*)*)#code\/([\w\-%+_]+={0,4})/;
88

99
export async function findCode(message: Message, ignoreLinks = false) {
10-
const codeInMessage = findCodeInMessage(message, ignoreLinks);
10+
const codeInMessage = await findCodeInMessage(message, ignoreLinks);
1111
if (codeInMessage) return codeInMessage;
1212
const referencedMessage = await getReferencedMessage(message);
1313
if (referencedMessage) {
14-
const codeInReferencedMessage = findCodeInMessage(
14+
const codeInReferencedMessage = await findCodeInMessage(
1515
referencedMessage,
1616
ignoreLinks,
1717
);
@@ -20,7 +20,7 @@ export async function findCode(message: Message, ignoreLinks = false) {
2020
const msgs = await message.channel.messages.fetch({ limit: 10 });
2121

2222
for (const msg of msgs.values()) {
23-
const code = findCodeInMessage(msg, ignoreLinks);
23+
const code = await findCodeInMessage(msg, ignoreLinks);
2424
if (code) return code;
2525
}
2626
}
@@ -29,10 +29,11 @@ export async function findCode(message: Message, ignoreLinks = false) {
2929
// 1: Normal code block annotated with ts from a non-bot
3030
// 2: Link to TS playground. This can be either from a bot or a normal user
3131
// since we shorten playground links on their own and delete the message.
32-
function findCodeInMessage(
33-
{ author, content, embeds }: Message,
34-
ignoreLinks = false,
35-
) {
32+
async function findCodeInMessage(msg: Message, ignoreLinks = false) {
33+
if (msg.type === 'THREAD_STARTER_MESSAGE') {
34+
msg = await msg.fetchReference();
35+
}
36+
const { author, content, embeds } = msg;
3637
if (!author.bot) {
3738
const match = content.match(CODEBLOCK_REGEX);
3839
if (match && match[1].length) {

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)