Skip to content

Commit 7d47cdb

Browse files
authored
Merge pull request #78 from Gerrit0/maybe-fix-cooldowns
fix: Cooldowns
2 parents f91948e + 46b29e0 commit 7d47cdb

File tree

1 file changed

+75
-27
lines changed

1 file changed

+75
-27
lines changed

src/modules/helpchan.ts

+75-27
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ import {
44
Module,
55
listener,
66
CommonInhibitors,
7+
optional,
78
} from 'cookiecord';
8-
import { Message, MessageEmbed, Guild, TextChannel } from 'discord.js';
9+
import {
10+
Message,
11+
MessageEmbed,
12+
Guild,
13+
TextChannel,
14+
GuildMember,
15+
} from 'discord.js';
916
import { HelpUser } from '../entities/HelpUser';
1017
import {
1118
categories,
@@ -134,21 +141,27 @@ export class HelpChanModule extends Module {
134141
this.busyChannels.has(msg.channel.id)
135142
)
136143
return;
137-
const pinned = (await msg.channel.messages.fetchPinned()).first();
138-
if (
139-
pinned && // If there's no pinned message, let anyone close the channel.
140-
pinned.author.id !== msg.author.id &&
141-
!msg.member?.hasPermission('MANAGE_MESSAGES')
142-
)
143-
return await msg.channel.send(
144-
':warning: you have to be the asker to close the channel.',
145-
);
146-
if (msg.channel.parentID !== categories.ongoing)
144+
145+
if (msg.channel.parentID !== categories.ongoing) {
147146
return await msg.channel.send(
148147
':warning: you can only run this in ongoing help channels.',
149148
);
149+
}
150+
151+
const owner = await HelpUser.findOne({
152+
where: { channelId: msg.channel.id },
153+
});
150154

151-
await this.markChannelAsDormant(msg.channel, pinned);
155+
if (
156+
(owner && owner.userId === msg.author.id) ||
157+
msg.member?.hasPermission('MANAGE_MESSAGES')
158+
) {
159+
await this.markChannelAsDormant(msg.channel);
160+
} else {
161+
return await msg.channel.send(
162+
':warning: you have to be the asker to close the channel.',
163+
);
164+
}
152165
}
153166

154167
async ensureAskChannels(guild: Guild) {
@@ -193,23 +206,20 @@ export class HelpChanModule extends Module {
193206
}
194207
}
195208

196-
private async markChannelAsDormant(channel: TextChannel, pinned?: Message) {
197-
if (!pinned) pinned = (await channel.messages.fetchPinned()).first();
198-
209+
private async markChannelAsDormant(channel: TextChannel) {
199210
this.busyChannels.add(channel.id);
200-
await pinned?.unpin();
201-
if (pinned) {
202-
await pinned?.member?.roles.remove(askCooldownRoleId);
203-
} else {
204-
const helpUser = await HelpUser.findOne({
205-
where: { channelId: channel.id },
211+
212+
const pinned = await channel.messages.fetchPinned();
213+
await Promise.all(pinned.map(msg => msg.unpin()));
214+
215+
const helpUser = await HelpUser.findOne({
216+
where: { channelId: channel.id },
217+
});
218+
if (helpUser) {
219+
const member = await channel.guild.members.fetch({
220+
user: helpUser.userId,
206221
});
207-
if (helpUser) {
208-
const member = await channel.guild.members.fetch({
209-
user: helpUser.userId,
210-
});
211-
await member?.roles.remove(askCooldownRoleId);
212-
}
222+
await member?.roles.remove(askCooldownRoleId);
213223
}
214224
await HelpUser.delete({ channelId: channel.id });
215225

@@ -239,6 +249,44 @@ export class HelpChanModule extends Module {
239249
}
240250
}
241251

252+
@command()
253+
async cooldown(msg: Message, @optional user?: GuildMember) {
254+
console.log('Cooldown', msg.content);
255+
if (!msg.guild) return;
256+
257+
const guildTarget = await msg.guild.members.fetch(user ?? msg.author);
258+
259+
if (!guildTarget) return;
260+
261+
if (!guildTarget.roles.cache.has(askCooldownRoleId)) {
262+
await msg.channel.send(
263+
`${guildTarget.displayName} doesn't have a cooldown.`,
264+
);
265+
return;
266+
}
267+
268+
const helpUser = await HelpUser.findOne({
269+
where: { userId: guildTarget.id },
270+
});
271+
272+
if (helpUser) {
273+
const channel = msg.guild.channels.resolve(helpUser.channelId);
274+
// If we don't have a channel, just remove the cooldown. This should
275+
// only happen if someone deletes a help channel.
276+
if (channel) {
277+
await msg.channel.send(
278+
`${guildTarget.displayName} has an active help channel: ${channel.name}`,
279+
);
280+
return;
281+
}
282+
}
283+
284+
await guildTarget.roles.remove(askCooldownRoleId);
285+
await msg.channel.send(
286+
`Removed ${guildTarget.displayName}'s cooldown.`,
287+
);
288+
}
289+
242290
// Commands to fix race conditions
243291
@command({
244292
inhibitors: [CommonInhibitors.hasGuildPermission('MANAGE_MESSAGES')],

0 commit comments

Comments
 (0)