From 210f89ec4eb0e51e518dc2d8fe6a62e52036b55d Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Sun, 4 Apr 2021 03:23:26 +0000 Subject: [PATCH] fix: channel mentions includes all valid mentions --- src/structures/message.ts | 58 ++++++++++++++++++++++----------------- src/util/constants.ts | 1 + 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/structures/message.ts b/src/structures/message.ts index 4cce057fe..526fb6fe5 100644 --- a/src/structures/message.ts +++ b/src/structures/message.ts @@ -9,6 +9,7 @@ import { removeAllReactions } from "../helpers/messages/remove_all_reactions.ts" import { removeReaction } from "../helpers/messages/remove_reaction.ts"; import { removeReactionEmoji } from "../helpers/messages/remove_reaction_emoji.ts"; import { sendMessage } from "../helpers/messages/send_message.ts"; +import { CHANNEL_MENTION_REGEX } from "../util/constants"; import { createNewProp } from "../util/utils.ts"; const baseMessage: Partial = { @@ -29,8 +30,9 @@ const baseMessage: Partial = { return this.member?.guilds.get(this.guildId); }, get link() { - return `https://discord.com/channels/${this.guildId || - "@me"}/${this.channelId}/${this.id}`; + return `https://discord.com/channels/${this.guildId || "@me"}/${ + this.channelId + }/${this.id}`; }, get mentionedRoles() { return this.mentionRoleIds?.map((id) => this.guild?.roles.get(id)) || []; @@ -44,12 +46,7 @@ const baseMessage: Partial = { // METHODS delete(reason, delayMilliseconds) { - return deleteMessage( - this.channelId!, - this.id!, - reason, - delayMilliseconds, - ); + return deleteMessage(this.channelId!, this.id!, reason, delayMilliseconds); }, edit(content) { return editMessage(this as Message, content); @@ -64,19 +61,20 @@ const baseMessage: Partial = { return addReactions(this.channelId!, this.id!, reactions, ordered); }, reply(content) { - const contentWithMention = typeof content === "string" - ? { - content, - mentions: { repliedUser: true }, - replyMessageId: this.id, - failReplyIfNotExists: false, - } - : { - ...content, - mentions: { ...(content.mentions || {}), repliedUser: true }, - replyMessageId: this.id, - failReplyIfNotExists: content.failReplyIfNotExists === true, - }; + const contentWithMention = + typeof content === "string" + ? { + content, + mentions: { repliedUser: true }, + replyMessageId: this.id, + failReplyIfNotExists: false, + } + : { + ...content, + mentions: { ...(content.mentions || {}), repliedUser: true }, + replyMessageId: this.id, + failReplyIfNotExists: content.failReplyIfNotExists === true, + }; if (this.guildId) return sendMessage(this.channelId!, contentWithMention); return sendDirectMessage(this.author!.id, contentWithMention); @@ -134,8 +132,8 @@ export async function createMessageStruct(data: MessageCreateOptions) { } // Discord doesnt give guild id for getMessage() so this will fill it in - const guildIdFinal = guildId || - (await cacheHandlers.get("channels", channelId))?.guildId || ""; + const guildIdFinal = + guildId || (await cacheHandlers.get("channels", channelId))?.guildId || ""; const message = Object.create(baseMessage, { ...restProps, @@ -146,12 +144,22 @@ export async function createMessageStruct(data: MessageCreateOptions) { mentions: createNewProp(data.mentions.map((m) => m.id)), mentionsEveryone: createNewProp(mentionsEveryone), mentionRoleIds: createNewProp(mentionRoleIds), - mentionChannelIds: createNewProp(mentionChannelIds.map((m) => m.id)), + mentionChannelIds: createNewProp( + [ + // Keep any ids that discord sends + ...mentionChannelIds, + // Add any other ids that can be validated in a channel mention format + ...(rest.content.match(CHANNEL_MENTION_REGEX) || []).map((text) => + // converts the <#123> into 123 + text.substring(2, text.length - 1) + ), + ].map((m) => m.id) + ), webhookId: createNewProp(webhookId), messageReference: createNewProp(messageReference), timestamp: createNewProp(Date.parse(data.timestamp)), editedTimestamp: createNewProp( - editedTimestamp ? Date.parse(editedTimestamp) : undefined, + editedTimestamp ? Date.parse(editedTimestamp) : undefined ), }); diff --git a/src/util/constants.ts b/src/util/constants.ts index 2d9e6d92c..d8701e5c1 100644 --- a/src/util/constants.ts +++ b/src/util/constants.ts @@ -177,3 +177,4 @@ export const endpoints = { }; export const SLASH_COMMANDS_NAME_REGEX = /^[\w-]{1,32}$/; +export const CHANNEL_MENTION_REGEX = /<#[0-9]+>/g; \ No newline at end of file