Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ export const ongoingEmptyTimeout = parseInt(process.env.ONGOING_EMPTY_TIMEOUT!);

export const TS_BLUE = '#007ACC';
export const GREEN = '#77b155';
// Picked from Discord's "hourglass" emoji (in ⌛ | Occupied Help Channels)
export const HOURGLASS_ORANGE = '#ffa647';
80 changes: 59 additions & 21 deletions src/modules/helpchan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import {
Guild,
TextChannel,
GuildMember,
User,
} from 'discord.js';
import { HelpUser } from '../entities/HelpUser';
import {
categories,
TS_BLUE,
GREEN,
HOURGLASS_ORANGE,
askCooldownRoleId,
channelNames,
dormantChannelTimeout,
Expand All @@ -39,6 +41,19 @@ This channel will be dedicated to answering your question only. Others will try
For more tips, check out StackOverflow's guide on **[asking good questions](https://stackoverflow.com/help/how-to-ask)**.
`;

const occupiedMessage = (tag: string) => `
**This channel is claimed by @${tag}**

This channel is dedicated to answering their question only (and any of their follow-up questions). Others will try to answer and help solve the issue.

**To @${tag}, keep in mind:**
• It's always ok to just ask your question. You don't need permission.
• Explain what you expect to happen and what actually happens.
• Include a code sample and error message, if you got any.

For more tips, check out StackOverflow's guide on **[asking good questions](https://stackoverflow.com/help/how-to-ask)**.
`;

const DORMANT_MESSAGE = `
This help channel has been marked as **dormant**, and has been moved into the **Help: Dormant** category at the bottom of the channel list. It is no longer possible to send messages in this channel until it becomes available again.

Expand All @@ -62,6 +77,20 @@ export class HelpChanModule extends Module {
} hours of inactivity or when you send !close.`,
);

OCCUPIED_EMBED_BASE = new MessageEmbed()
.setTitle('⌛ Occupied Help Channel')
.setColor(HOURGLASS_ORANGE);

occupiedEmbed(asker: User) {
return new MessageEmbed(this.OCCUPIED_EMBED_BASE)
.setDescription(occupiedMessage(asker.tag))
.setFooter(
`Closes after ${
dormantChannelTimeout / 60 / 60 / 1000
} hours of inactivity or when ${asker.username} sends !close.`,
);
}

DORMANT_EMBED = new MessageEmbed()
.setColor(TS_BLUE)
.setDescription(DORMANT_MESSAGE);
Expand Down Expand Up @@ -117,9 +146,8 @@ export class HelpChanModule extends Module {
const embed = messages.first()?.embeds[0];

return (
embed &&
embed.description?.trim() ===
this.AVAILABLE_EMBED.description?.trim()
embed?.title &&
embed.title.trim() === this.OCCUPIED_EMBED_BASE.title?.trim()
);
}

Expand Down Expand Up @@ -176,6 +204,9 @@ export class HelpChanModule extends Module {

this.busyChannels.add(msg.channel.id);

let embed = this.occupiedEmbed(msg.author);

await this.updateStatusEmbed(msg.channel, embed);
await msg.pin();
await this.addCooldown(msg.member, msg.channel);
await this.moveChannel(msg.channel, categories.ongoing);
Expand Down Expand Up @@ -244,24 +275,7 @@ export class HelpChanModule extends Module {
);
if (dormant && dormant instanceof TextChannel) {
await this.moveChannel(dormant, categories.ask);

let lastMessage = dormant.messages.cache
.array()
.reverse()
.find(m => m.author.id === this.client.user?.id);

if (!lastMessage)
lastMessage = (await dormant.messages.fetch({ limit: 5 }))
.array()
.find(m => m.author.id === this.client.user?.id);

if (lastMessage) {
// If there is a last message (the dormant message) by the bot, just edit it
await lastMessage.edit(this.AVAILABLE_EMBED);
} else {
// Otherwise, just send a new message
await dormant.send(this.AVAILABLE_EMBED);
}
await this.updateStatusEmbed(dormant, this.AVAILABLE_EMBED);
} else {
const chan = await guild.channels.create(
this.getChannelName(guild),
Expand Down Expand Up @@ -321,6 +335,27 @@ export class HelpChanModule extends Module {
}
}

private async updateStatusEmbed(channel: TextChannel, embed: MessageEmbed) {
let lastMessage = channel.messages.cache
.array()
.reverse()
.find(m => m.author.id === this.client.user?.id);

if (!lastMessage)
lastMessage = (await channel.messages.fetch({ limit: 5 }))
.array()
.reverse()
.find(m => m.author.id === this.client.user?.id);

if (lastMessage) {
// If there is a last message (the status message) by the bot, edit it
await lastMessage.edit(embed);
} else {
// Otherwise, just send a new message
await channel.send(embed);
}
}

private async addCooldown(member: GuildMember, channel: TextChannel) {
await member.roles.add(askCooldownRoleId);
const helpUser = new HelpUser();
Expand Down Expand Up @@ -415,7 +450,10 @@ export class HelpChanModule extends Module {
.setAuthor(member.displayName, member.user.displayAvatarURL())
.setDescription(msgContent),
);

await toPin.pin();
const occupied = this.occupiedEmbed(msg.author);
await this.updateStatusEmbed(claimedChannel, occupied);
await this.addCooldown(member, claimedChannel);
await this.moveChannel(claimedChannel, categories.ongoing);
await claimedChannel.send(
Expand Down