Refactoring
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
import { TextChannel, type Message } from "discord.js";
|
||||
|
||||
export default function attemptSendAuthorMessage(message: Message): void {
|
||||
if (!(message.channel instanceof TextChannel)) return;
|
||||
if (!message.guild) return;
|
||||
|
||||
message.author
|
||||
.send(
|
||||
`Hey! [__O link que voce mandou__](<${message.content}>) no canal "_${message.channel.name}_" do servidor "_${message.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);
|
||||
}
|
||||
19
src/bot.ts
19
src/bot.ts
@@ -1,10 +1,8 @@
|
||||
import { Client, Events, GatewayIntentBits } from "discord.js";
|
||||
import type { Message } from "discord.js";
|
||||
import { readyHandler } from "./ready.ts";
|
||||
import feedMessagesArray from "./feedMessagesArray.ts";
|
||||
import updateMessagesArray from "./updateMessagesArray.ts";
|
||||
import checkForReposts from "./checkForReposts.ts";
|
||||
import attemptSendAuthorMessage from "./attemptSendUserMessage.ts";
|
||||
import { readyHandler } from "./functions/ready.ts";
|
||||
import feedMessagesArray from "./functions/feedMessagesArray.ts";
|
||||
import handleMessageReposts from "./functions/handleMessageReposts.ts";
|
||||
|
||||
const client = new Client({
|
||||
intents: [
|
||||
@@ -22,16 +20,7 @@ client.once(Events.ClientReady, async (client) => {
|
||||
});
|
||||
|
||||
client.on(Events.MessageCreate, async (message) => {
|
||||
console.log(message);
|
||||
const originalMessages = checkForReposts(messagesArr, message);
|
||||
console.log(originalMessages);
|
||||
|
||||
if (originalMessages.length >= 1) {
|
||||
message.delete();
|
||||
attemptSendAuthorMessage(message);
|
||||
} else {
|
||||
messagesArr = updateMessagesArray(messagesArr, message);
|
||||
}
|
||||
messagesArr = handleMessageReposts(messagesArr, message);
|
||||
});
|
||||
|
||||
// // TODO, updateMessagesArray on message deletion
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
import type { Message } from "discord.js";
|
||||
import { clearUrl } from "./utils/url.ts";
|
||||
import { transformMessageArrayInMap } from "./utils/message.ts";
|
||||
|
||||
export default function checkForReposts(
|
||||
messagesArray: Message[],
|
||||
newMessage: Message,
|
||||
): Message[] {
|
||||
const urlMessagesMap = transformMessageArrayInMap(messagesArray);
|
||||
|
||||
const newMessageClearedUrl = clearUrl(newMessage.content);
|
||||
|
||||
if (!newMessageClearedUrl) return [];
|
||||
|
||||
const urlMessagesMatchArray = urlMessagesMap.get(newMessageClearedUrl);
|
||||
|
||||
if (!urlMessagesMatchArray) return [];
|
||||
|
||||
if (urlMessagesMatchArray.length > 1) return urlMessagesMatchArray;
|
||||
|
||||
return [];
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
import type { Message } from "discord.js";
|
||||
import type MessageInfoWithReposts from "./interfaces/MessageInfoWithReposts.ts";
|
||||
import { transformMessageArrayInMap } from "./utils/message.ts";
|
||||
|
||||
export default function checkMessagesArrayForReposts(
|
||||
messagesArray: Message[],
|
||||
): Map<string, MessageInfoWithReposts> {
|
||||
const urlMessagesMap = transformMessageArrayInMap(messagesArray);
|
||||
|
||||
const messageInfoWithRepostsMap = new Map<string, MessageInfoWithReposts>();
|
||||
|
||||
for (const [url, messagesMentioningUrlArray] of urlMessagesMap) {
|
||||
if (messagesMentioningUrlArray.length === 1) continue;
|
||||
|
||||
const originalMessage = messagesMentioningUrlArray.reduce((prev, curr) =>
|
||||
prev.createdTimestamp < curr.createdTimestamp ? prev : curr,
|
||||
);
|
||||
|
||||
const messageInfoWithRepostsMapPosition =
|
||||
messageInfoWithRepostsMap.get(url);
|
||||
|
||||
if (!messageInfoWithRepostsMapPosition) {
|
||||
messageInfoWithRepostsMap.set(url, {
|
||||
originalMessage,
|
||||
repetitions: messagesMentioningUrlArray.filter(
|
||||
(elm) => elm !== originalMessage,
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return messageInfoWithRepostsMap;
|
||||
}
|
||||
15
src/functions/attemptSendUserMessage.ts
Normal file
15
src/functions/attemptSendUserMessage.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { TextChannel, type Message } from "discord.js";
|
||||
|
||||
export default function attemptSendAuthorMessage(authorMessage: Message): void {
|
||||
if (!(authorMessage.channel instanceof TextChannel)) return;
|
||||
if (!authorMessage.guild) return;
|
||||
|
||||
authorMessage.author
|
||||
.send(
|
||||
`Hey! [__O link que voce mandou__](<${authorMessage.content}>) 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);
|
||||
}
|
||||
33
src/functions/checkMessagesArrayReposts.ts
Normal file
33
src/functions/checkMessagesArrayReposts.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import type { Message } from "discord.js";
|
||||
import type MessageInfoWithReposts from "../interfaces/MessageInfoWithReposts.ts";
|
||||
import { transformMessageArrayInMap } from "../utils/message.ts";
|
||||
|
||||
export default function checkMessagesArrayForReposts(
|
||||
messagesArray: Message[],
|
||||
): Map<string, MessageInfoWithReposts> {
|
||||
const urlMessagesMap = transformMessageArrayInMap(messagesArray);
|
||||
|
||||
const messageInfoWithRepostsMap = new Map<string, MessageInfoWithReposts>();
|
||||
|
||||
for (const [url, messagesMentioningUrlArray] of urlMessagesMap) {
|
||||
if (messagesMentioningUrlArray.length === 1) continue;
|
||||
|
||||
const originalMessage = messagesMentioningUrlArray.reduce((prev, curr) =>
|
||||
prev.createdTimestamp < curr.createdTimestamp ? prev : curr,
|
||||
);
|
||||
|
||||
const messageInfoWithRepostsMapPosition =
|
||||
messageInfoWithRepostsMap.get(url);
|
||||
|
||||
if (!messageInfoWithRepostsMapPosition) {
|
||||
messageInfoWithRepostsMap.set(url, {
|
||||
originalMessage,
|
||||
repetitions: messagesMentioningUrlArray.filter(
|
||||
(elm) => elm !== originalMessage,
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return messageInfoWithRepostsMap;
|
||||
}
|
||||
30
src/functions/handleMessageReposts.ts
Normal file
30
src/functions/handleMessageReposts.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import type { Message } from "discord.js";
|
||||
import { clearUrl } from "../utils/url.ts";
|
||||
import { transformMessageArrayInMap } from "../utils/message.ts";
|
||||
|
||||
import updateMessagesArray from "./updateMessagesArray.ts";
|
||||
import attemptSendAuthorMessage from "./attemptSendUserMessage.ts";
|
||||
|
||||
export default function handleMessageReposts(
|
||||
messagesArray: Message[],
|
||||
newMessage: Message,
|
||||
): Message[] {
|
||||
const urlMessagesMap = transformMessageArrayInMap(messagesArray);
|
||||
|
||||
const newMessageClearedUrl = clearUrl(newMessage.content);
|
||||
|
||||
if (!newMessageClearedUrl) return messagesArray;
|
||||
|
||||
const urlMessagesMatchArray = urlMessagesMap.get(newMessageClearedUrl);
|
||||
|
||||
if (!urlMessagesMatchArray) return messagesArray;
|
||||
|
||||
if (urlMessagesMatchArray.length <= 1) {
|
||||
return updateMessagesArray(messagesArray, newMessage);
|
||||
}
|
||||
|
||||
newMessage.delete();
|
||||
attemptSendAuthorMessage(newMessage);
|
||||
|
||||
return messagesArray;
|
||||
}
|
||||
Reference in New Issue
Block a user