Skip to content

Commit 01e36d8

Browse files
committed
allow !title to be run multiple times, with 5 min cooldown
1 parent 07d2f55 commit 01e36d8

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/entities/HelpThread.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ export class HelpThread extends BaseEntity {
1111
// When @helper was last pinged
1212
@Column({ nullable: true })
1313
helperTimestamp?: string;
14+
15+
// When the title was last set
16+
@Column({ nullable: true })
17+
titleSetTimestamp?: string;
1418
}

src/modules/helpthread.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ ${owner} This thread is for your question; when it's resolved, please type \`!cl
8282
See <#${howToGetHelpChannel}> for info on how to get better help.
8383
`;
8484

85+
// The rate limit for thread naming is 2 time / 10 mins, tracked per thread
86+
const titleSetCooldown = 5 * 60 * 1000;
87+
8588
export class HelpThreadModule extends Module {
8689
@listener({ event: 'messageCreate' })
8790
async onNewQuestion(msg: Message) {
@@ -256,13 +259,33 @@ export class HelpThreadModule extends Module {
256259
);
257260
if (!title)
258261
return sendWithMessageOwnership(msg, ':warning: Missing title');
259-
let username = msg.member?.nickname ?? msg.author.username;
260-
if (msg.channel.name !== username)
262+
const thread = msg.channel;
263+
const threadData = (await HelpThread.findOne(thread.id))!;
264+
if (
265+
msg.author.id !== threadData.ownerId &&
266+
!msg.member!.roles.cache.has(trustedRoleId)
267+
)
268+
return sendWithMessageOwnership(
269+
msg,
270+
':warning: Only the asker and helpers can set the title',
271+
);
272+
const titleSetAllowedAfter =
273+
+(threadData.titleSetTimestamp ?? 0) + titleSetCooldown;
274+
if (threadData.titleSetTimestamp && Date.now() < titleSetAllowedAfter)
261275
return sendWithMessageOwnership(
262276
msg,
263-
':warning: Already set thread name',
277+
`:warning: You can set the title again <t:${Math.ceil(
278+
titleSetAllowedAfter / 1000,
279+
)}:R>`,
264280
);
265-
msg.channel.setName(`${username} - ${title}`);
281+
const owner = await msg.guild!.members.fetch(threadData.ownerId);
282+
const username = owner.nickname ?? owner.user.username;
283+
await Promise.all([
284+
HelpThread.update(thread.id, {
285+
titleSetTimestamp: Date.now() + '',
286+
}),
287+
msg.channel.setName(`${username} - ${title}`),
288+
]);
266289
}
267290

268291
@command()

0 commit comments

Comments
 (0)