Skip to content

Commit f828a7f

Browse files
authored
Merge pull request #117 from smichel17/update-embed
Update help channel embed when channel is occupied
2 parents eba49cf + 995ce85 commit f828a7f

File tree

3 files changed

+95
-27
lines changed

3 files changed

+95
-27
lines changed

src/db.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,26 @@ let db: Connection | undefined;
88
export async function getDB() {
99
if (db) return db;
1010

11+
// Require ssl in production
12+
const extraOpts =
13+
process.env.NODE_ENV === 'production'
14+
? {
15+
ssl: true,
16+
extra: {
17+
ssl: {
18+
rejectUnauthorized: false,
19+
},
20+
},
21+
}
22+
: {};
23+
1124
db = await createConnection({
1225
type: 'postgres',
1326
url: dbUrl,
1427
synchronize: true,
1528
logging: false,
1629
entities: [RepUser, RepGive, HelpUser],
17-
ssl: true,
18-
extra: {
19-
ssl: {
20-
rejectUnauthorized: false,
21-
},
22-
},
30+
...extraOpts,
2331
});
2432
console.log('Connected to DB');
2533
return db;

src/env.ts

+2
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ export const ongoingEmptyTimeout = parseInt(process.env.ONGOING_EMPTY_TIMEOUT!);
3838

3939
export const TS_BLUE = '#007ACC';
4040
export const GREEN = '#77b155';
41+
// Picked from Discord's "hourglass" emoji (in ⌛ | Occupied Help Channels)
42+
export const HOURGLASS_ORANGE = '#ffa647';

src/modules/helpchan.ts

+79-21
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@ import {
1212
Guild,
1313
TextChannel,
1414
GuildMember,
15+
User,
1516
} from 'discord.js';
1617
import { HelpUser } from '../entities/HelpUser';
1718
import {
1819
categories,
1920
TS_BLUE,
2021
GREEN,
22+
HOURGLASS_ORANGE,
2123
askCooldownRoleId,
2224
channelNames,
2325
dormantChannelTimeout,
2426
dormantChannelLoop,
2527
askHelpChannelId,
2628
ongoingEmptyTimeout,
29+
trustedRoleId,
2730
} from '../env';
2831
import { isTrustedMember } from '../util/inhibitors';
2932

@@ -39,6 +42,26 @@ This channel will be dedicated to answering your question only. Others will try
3942
For more tips, check out StackOverflow's guide on **[asking good questions](https://stackoverflow.com/help/how-to-ask)**.
4043
`;
4144

45+
// The "empty" line has a braille pattern blank unicode character, in order to
46+
// achieve a leading newline, since normally whitespace is stripped. This is a
47+
// hack, but it works even on a system without the fonts to display Discord
48+
// emoji, so it should work everywhere. https://www.compart.com/en/unicode/U+2800
49+
const occupiedMessage = (asker: User) => `
50+
51+
**This channel is claimed by ${asker}.**
52+
It is dedicated to answering their questions only. More info: <#${askHelpChannelId}>
53+
54+
**${asker} You'll get better and faster answers if you:**
55+
• Describe the context. What are you trying to accomplish?
56+
• Include any error messages, and the code that produce them (5-15 lines).
57+
• Use code blocks, not screenshots. Start with ${'```ts'} for syntax highlighting.
58+
• Also reproduce the issue in the **[TypeScript Playground](https://www.typescriptlang.org/play)**, if possible.
59+
60+
Usually someone will try to answer and help solve the issue within a few hours. If not, and you have followed the bullets above, you may ping the <@&${trustedRoleId}> role.
61+
62+
For more tips, check out StackOverflow's guide on **[asking good questions](https://stackoverflow.com/help/how-to-ask)**.
63+
`;
64+
4265
const DORMANT_MESSAGE = `
4366
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.
4467
@@ -62,7 +85,22 @@ export class HelpChanModule extends Module {
6285
} hours of inactivity or when you send !close.`,
6386
);
6487

88+
OCCUPIED_EMBED_BASE = new MessageEmbed()
89+
.setTitle('⌛ Occupied Help Channel')
90+
.setColor(HOURGLASS_ORANGE);
91+
92+
occupiedEmbed(asker: User) {
93+
return new MessageEmbed(this.OCCUPIED_EMBED_BASE)
94+
.setDescription(occupiedMessage(asker))
95+
.setFooter(
96+
`Closes after ${
97+
dormantChannelTimeout / 60 / 60 / 1000
98+
} hours of inactivity or when ${asker.username} sends !close.`,
99+
);
100+
}
101+
65102
DORMANT_EMBED = new MessageEmbed()
103+
.setTitle('💤 Dormant Help Channel')
66104
.setColor(TS_BLUE)
67105
.setDescription(DORMANT_MESSAGE);
68106

@@ -117,9 +155,8 @@ export class HelpChanModule extends Module {
117155
const embed = messages.first()?.embeds[0];
118156

119157
return (
120-
embed &&
121-
embed.description?.trim() ===
122-
this.AVAILABLE_EMBED.description?.trim()
158+
embed?.title &&
159+
embed.title.trim() === this.OCCUPIED_EMBED_BASE.title?.trim()
123160
);
124161
}
125162

@@ -176,6 +213,9 @@ export class HelpChanModule extends Module {
176213

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

216+
let embed = this.occupiedEmbed(msg.author);
217+
218+
await this.updateStatusEmbed(msg.channel, embed);
179219
await msg.pin();
180220
await this.addCooldown(msg.member, msg.channel);
181221
await this.moveChannel(msg.channel, categories.ongoing);
@@ -244,24 +284,7 @@ export class HelpChanModule extends Module {
244284
);
245285
if (dormant && dormant instanceof TextChannel) {
246286
await this.moveChannel(dormant, categories.ask);
247-
248-
let lastMessage = dormant.messages.cache
249-
.array()
250-
.reverse()
251-
.find(m => m.author.id === this.client.user?.id);
252-
253-
if (!lastMessage)
254-
lastMessage = (await dormant.messages.fetch({ limit: 5 }))
255-
.array()
256-
.find(m => m.author.id === this.client.user?.id);
257-
258-
if (lastMessage) {
259-
// If there is a last message (the dormant message) by the bot, just edit it
260-
await lastMessage.edit(this.AVAILABLE_EMBED);
261-
} else {
262-
// Otherwise, just send a new message
263-
await dormant.send(this.AVAILABLE_EMBED);
264-
}
287+
await this.updateStatusEmbed(dormant, this.AVAILABLE_EMBED);
265288
} else {
266289
const chan = await guild.channels.create(
267290
this.getChannelName(guild),
@@ -321,6 +344,38 @@ export class HelpChanModule extends Module {
321344
}
322345
}
323346

347+
private async updateStatusEmbed(channel: TextChannel, embed: MessageEmbed) {
348+
const isStatusEmbed = (embed: MessageEmbed) =>
349+
[
350+
this.AVAILABLE_EMBED.title,
351+
this.OCCUPIED_EMBED_BASE.title,
352+
this.DORMANT_EMBED.title,
353+
].includes(embed.title);
354+
355+
// The message cache does not have a stable order (at least with respect
356+
// to creation date), so sorting is needed to find the latest embed.
357+
let lastMessage = channel.messages.cache
358+
.array()
359+
.filter(m => m.author.id === this.client.user?.id)
360+
.sort((m1, m2) => m2.createdTimestamp - m1.createdTimestamp)
361+
.find(m => m.embeds.some(isStatusEmbed));
362+
363+
if (!lastMessage)
364+
// Fetch has a stable order, with recent messages first
365+
lastMessage = (await channel.messages.fetch({ limit: 5 }))
366+
.array()
367+
.filter(m => m.author.id === this.client.user?.id)
368+
.find(m => m.embeds.some(isStatusEmbed));
369+
370+
if (lastMessage) {
371+
// If there is a last message (the status message) by the bot, edit it
372+
await lastMessage.edit(embed);
373+
} else {
374+
// Otherwise, just send a new message
375+
await channel.send(embed);
376+
}
377+
}
378+
324379
private async addCooldown(member: GuildMember, channel: TextChannel) {
325380
await member.roles.add(askCooldownRoleId);
326381
const helpUser = new HelpUser();
@@ -415,7 +470,10 @@ export class HelpChanModule extends Module {
415470
.setAuthor(member.displayName, member.user.displayAvatarURL())
416471
.setDescription(msgContent),
417472
);
473+
418474
await toPin.pin();
475+
const occupied = this.occupiedEmbed(member.user);
476+
await this.updateStatusEmbed(claimedChannel, occupied);
419477
await this.addCooldown(member, claimedChannel);
420478
await this.moveChannel(claimedChannel, categories.ongoing);
421479
await claimedChannel.send(

0 commit comments

Comments
 (0)