-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix "accept answer" double-sending in some cases #180
base: main
Are you sure you want to change the base?
Changes from all commits
bfeb69a
08c3341
cbcad00
a3f3404
5169549
c6d2232
436c9f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string, Message<boolean>>, | ||
): boolean => { | ||
let flag = 0; | ||
|
||
messages.forEach((message) => { | ||
if (message.content.includes("This question has an answer!")) { | ||
flag++; | ||
} | ||
}); | ||
|
||
return flag > 0; | ||
}; | ||
|
||
Comment on lines
+19
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me know if I can improve this function in any way. |
||
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)) { | ||
Comment on lines
+74
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A couple notes:
|
||
return; | ||
} | ||
|
||
const starter = thread.isThread() | ||
? await thread.fetchStarterMessage() | ||
: undefined; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This relies on the messages being cached, which won't be true for threads that were created before the bot restarted. Is it feasible to make this something like, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about: (prettier might complain, my dev laptop is out of commission atm)