From 7af32a69bef959e46e7435cb76c731f143b7e9ef Mon Sep 17 00:00:00 2001 From: Matheus Albino Date: Wed, 23 Oct 2024 17:31:12 -0300 Subject: [PATCH] Bot now replies to the author's message instead of sending DMs --- src/functions/attemptReplyAuthorMessage.ts | 32 ++++++++++++++++++++++ src/functions/attemptSendUserMessage.ts | 18 ------------ src/functions/handleMessageReposts.ts | 9 +++--- src/utils/message.ts | 4 +++ 4 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 src/functions/attemptReplyAuthorMessage.ts delete mode 100644 src/functions/attemptSendUserMessage.ts diff --git a/src/functions/attemptReplyAuthorMessage.ts b/src/functions/attemptReplyAuthorMessage.ts new file mode 100644 index 0000000..4a29088 --- /dev/null +++ b/src/functions/attemptReplyAuthorMessage.ts @@ -0,0 +1,32 @@ +import { TextChannel, type Message } from "discord.js"; +import { underline } from "../utils/message.ts"; + +export default function attemptReplyAuthorMessage( + authorMessage: Message, + url: string, + originalMessages: Message[], +): void { + if (!(authorMessage.channel instanceof TextChannel)) return; + if (!authorMessage.guild) return; + + let replyMessage = `Hey! [${underline("O link que voce mandou")}](<${url}>) ja foi mandado no passado! Voce eh um repostado!`; + + for (let index = 0; index < originalMessages.length; index++) { + const msg = originalMessages[index]; + if (!msg) return; + + const { id, guildId, channelId, author } = msg; + if (!author.globalName) return; + + const messageUrl = `https://discord.com/channels/${guildId}/${channelId}/${id}`; + replyMessage += `\n- Mensagem original ${++index} (por ${underline(author.globalName)}): ${messageUrl}`; + } + + authorMessage + .reply({ + content: replyMessage, + options: { ephemeral: true }, + }) + .then() + .catch((_) => null); +} diff --git a/src/functions/attemptSendUserMessage.ts b/src/functions/attemptSendUserMessage.ts deleted file mode 100644 index 20cda2f..0000000 --- a/src/functions/attemptSendUserMessage.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { TextChannel, type Message } from "discord.js"; - -export default function attemptSendAuthorMessage( - authorMessage: Message, - url: string, -): void { - if (!(authorMessage.channel instanceof TextChannel)) return; - if (!authorMessage.guild) return; - - authorMessage.author - .send( - `Hey! [__O link que voce mandou__](<${url}>) no canal "_${authorMessage.channel.name}_" do servidor "_${authorMessage.guild.name}_" ja foi mandado no passado! -A mensagem que voce mandou foi apagada para evitar que os chatos comentem "repost". -De nada!`, - ) - .then() - .catch((_) => null); -} diff --git a/src/functions/handleMessageReposts.ts b/src/functions/handleMessageReposts.ts index 3e85055..2e4b665 100644 --- a/src/functions/handleMessageReposts.ts +++ b/src/functions/handleMessageReposts.ts @@ -3,7 +3,7 @@ import { clearUrl } from "../utils/url.ts"; import { transformMessageArrayInMap } from "../utils/message.ts"; import updateMessagesArray from "./updateMessagesArray.ts"; -import attemptSendAuthorMessage from "./attemptSendUserMessage.ts"; +import attemptReplyAuthorMessage from "./attemptReplyAuthorMessage.ts"; export default function handleMessageReposts( messagesArray: Message[], @@ -15,14 +15,13 @@ export default function handleMessageReposts( if (!newMessageClearedUrl) return messagesArray; - const urlMessagesMatchArray = urlMessagesMap.get(newMessageClearedUrl); + const originalMessages = urlMessagesMap.get(newMessageClearedUrl); - if (!urlMessagesMatchArray) { + if (!originalMessages) { return updateMessagesArray(messagesArray, newMessage); } - newMessage.delete(); - attemptSendAuthorMessage(newMessage, newMessageClearedUrl); + attemptReplyAuthorMessage(newMessage, newMessageClearedUrl, originalMessages); return messagesArray; } diff --git a/src/utils/message.ts b/src/utils/message.ts index 8166bb1..503969a 100644 --- a/src/utils/message.ts +++ b/src/utils/message.ts @@ -23,3 +23,7 @@ export function transformMessageArrayInMap( return urlMessagesMap; } + +export function underline(message: string): string { + return `__${message}__`; +}