1 | const log = logger.withScope('discord:handleMentions')
|
2 |
|
3 | import { DMChannel, TextChannel } from 'discord.js'
|
4 |
|
5 | export default function handleMentions (message: string, channel: (TextChannel | DMChannel)) {
|
6 |
|
7 | const massMentions = ['@everyone', '@here']
|
8 | if (massMentions.some(massMention => message.includes(massMention)) && !config.discord.massMentions) {
|
9 | massMentions.forEach(massMention => { message = message.replace(new RegExp(massMention, 'g'), `\`${massMention}\``) })
|
10 | }
|
11 |
|
12 | if (!channel || channel instanceof DMChannel) return message
|
13 |
|
14 | if (config.discord.roleMentions) {
|
15 | for (let role of channel.guild.roles.array()) {
|
16 | message = replaceCaseInsensitive(message, `@${role.name}`, role.toString())
|
17 | }
|
18 | }
|
19 |
|
20 | if (config.discord.userMentions) {
|
21 | for (let member of channel.members.array()) {
|
22 | message = replaceCaseInsensitive(message, `@${member.user.username}`, member.toString())
|
23 | message = replaceCaseInsensitive(message, `@${member.nickname}`, member.toString())
|
24 | }
|
25 | }
|
26 |
|
27 | log.trace('message', message)
|
28 | return message
|
29 | }
|
30 | function replaceCaseInsensitive (str: string, searchString: string, replaceString: string) {
|
31 | const index = str.toLowerCase().indexOf(searchString.toLowerCase())
|
32 | if (index === -1) return str
|
33 | return str.slice(0, index) + replaceString + str.slice(index + searchString.length, str.length)
|
34 | }
|