From bfeb69a1c8c555b571de07f91f5684913bfd0af6 Mon Sep 17 00:00:00 2001 From: definoob Date: Thu, 27 Jan 2022 09:48:43 +0800 Subject: [PATCH 1/6] https://github.com/reactiflux/reactibot/issues/160 --- src/features/commands.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/features/commands.ts b/src/features/commands.ts index 42fedcbc..6e7c0dbf 100644 --- a/src/features/commands.ts +++ b/src/features/commands.ts @@ -652,6 +652,9 @@ Instead: guildTextChannel.guild.roles.everyone, { SEND_MESSAGES: false, + CREATE_PUBLIC_THREADS: false, + CREATE_PRIVATE_THREADS: false, + SEND_MESSAGES_IN_THREADS: false, }, ); guildTextChannel.send("This channel has been locked by a moderator"); From cbcad00b317e4ea0b842c832459f46ebbd8cdf8f Mon Sep 17 00:00:00 2001 From: definoob Date: Thu, 3 Feb 2022 11:26:15 +0800 Subject: [PATCH 2/6] Issue 173 --- src/features/autothread.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/features/autothread.ts b/src/features/autothread.ts index a96c2742..4cbf914a 100644 --- a/src/features/autothread.ts +++ b/src/features/autothread.ts @@ -49,7 +49,7 @@ const autoThread: ChannelHandlers = { await sleep(30); message.delete(); }, - handleReaction: async ({ reaction }) => { + handleReaction: async ({ reaction, user }) => { if (!CHECKS.includes(reaction.emoji.toString())) { return; } @@ -59,7 +59,7 @@ const autoThread: ChannelHandlers = { ? await thread.fetchStarterMessage() : undefined; - if (!starter || !guild) { + if (!starter || !guild || user.id !== starter.author.id) { return; } From a3f3404ccfc1eb612c5413a37c72576c88fa20ac Mon Sep 17 00:00:00 2001 From: definoob Date: Thu, 3 Feb 2022 20:40:10 +0800 Subject: [PATCH 3/6] revert unnecessary changes --- src/features/autothread.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/features/autothread.ts b/src/features/autothread.ts index 4cbf914a..a96c2742 100644 --- a/src/features/autothread.ts +++ b/src/features/autothread.ts @@ -49,7 +49,7 @@ const autoThread: ChannelHandlers = { await sleep(30); message.delete(); }, - handleReaction: async ({ reaction, user }) => { + handleReaction: async ({ reaction }) => { if (!CHECKS.includes(reaction.emoji.toString())) { return; } @@ -59,7 +59,7 @@ const autoThread: ChannelHandlers = { ? await thread.fetchStarterMessage() : undefined; - if (!starter || !guild || user.id !== starter.author.id) { + if (!starter || !guild) { return; } From 51695494309542fe35285148ca3e72b1fa29263d Mon Sep 17 00:00:00 2001 From: definoob Date: Fri, 4 Feb 2022 00:27:02 +0800 Subject: [PATCH 4/6] get uesr-sent messages from thread --- src/helpers/discord.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/helpers/discord.ts b/src/helpers/discord.ts index d9c73218..8bad0217 100644 --- a/src/helpers/discord.ts +++ b/src/helpers/discord.ts @@ -5,6 +5,7 @@ import { Guild, MessageReaction, PartialMessageReaction, + ThreadChannel, } from "discord.js"; const staffRoles = ["mvp", "moderator", "admin", "admins"]; @@ -41,3 +42,6 @@ export const fetchReactionMembers = ( .then((users) => Promise.all(users.map((user) => guild.members.fetch(user.id))), ); + +export const fetchMessagesByUser = (thread: ThreadChannel, userID: string) => + thread.messages.cache.filter((message) => message.author.id === userID); From c6d22328d24d62c7e02eeb2efaa62bfda9223327 Mon Sep 17 00:00:00 2001 From: definoob Date: Fri, 4 Feb 2022 00:27:23 +0800 Subject: [PATCH 5/6] add reactibot ID --- src/constants.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/constants.ts b/src/constants.ts index 1a35fbc1..e3a24827 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,4 +1,5 @@ export const modRoleId = "&102870499406647296"; +export const reactibot = "644375510725689369"; export const enum CHANNELS { "helpReact" = "103696749012467712", From 436c9f10abceea1295df41acde54aa15f10ae3a9 Mon Sep 17 00:00:00 2001 From: definoob Date: Fri, 4 Feb 2022 00:29:40 +0800 Subject: [PATCH 6/6] check if bot replied already --- src/features/autothread.ts | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/features/autothread.ts b/src/features/autothread.ts index a96c2742..c901a204 100644 --- a/src/features/autothread.ts +++ b/src/features/autothread.ts @@ -1,11 +1,12 @@ import { differenceInHours, format } from "date-fns"; -import { Client } from "discord.js"; -import { CHANNELS } from "../constants"; +import { Client, Collection, ThreadChannel, Message } from "discord.js"; +import { CHANNELS, reactibot } from "../constants"; import { constructDiscordLink, fetchReactionMembers, isHelpful, isStaff, + fetchMessagesByUser, } from "../helpers/discord"; import { sleep } from "../helpers/misc"; import { ChannelHandlers } from "../types"; @@ -15,6 +16,20 @@ const CHECKS = ["☑️", "✔️", "✅"]; const IDLE_TIMEOUT = 12; const STAFF_ACCEPT_THRESHOLD = 2; +const checkIfBotReplied = ( + messages: Collection>, +): boolean => { + let flag = 0; + + messages.forEach((message) => { + if (message.content.includes("This question has an answer!")) { + flag++; + } + }); + + return flag > 0; +}; + const autoThread: ChannelHandlers = { handleMessage: async ({ msg: maybeMessage }) => { const msg = maybeMessage.partial @@ -55,6 +70,16 @@ const autoThread: ChannelHandlers = { } const { channel: thread, author, guild } = await reaction.message.fetch(); + + const threadMessages = fetchMessagesByUser( + thread as ThreadChannel, + reactibot, + ); + + if (checkIfBotReplied(threadMessages)) { + return; + } + const starter = thread.isThread() ? await thread.fetchStarterMessage() : undefined;