-
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?
Conversation
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; | ||
}; | ||
|
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.
Let me know if I can improve this function in any way.
let flag = 0; | ||
|
||
messages.forEach((message) => { | ||
if (message.content.includes("This question has an answer!")) { | ||
flag++; | ||
} | ||
}); | ||
|
||
return flag > 0; |
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)
let flag = 0; | |
messages.forEach((message) => { | |
if (message.content.includes("This question has an answer!")) { | |
flag++; | |
} | |
}); | |
return flag > 0; | |
return messages.some((message) => | |
message.content.includes("This question has an answer!") | |
); |
@@ -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 comment
The 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, thread.messages.fetch()
? (I don't have a dev computer handy or I'd check, sorry)
const threadMessages = fetchMessagesByUser( | ||
thread as ThreadChannel, | ||
reactibot, | ||
); | ||
|
||
if (checkIfBotReplied(threadMessages)) { |
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.
A couple notes:
fetchMessagesByUser
is a relatively expensive call, I think we should move it further down in the logic so it's only run when absolutely necessary -- we have less expensive calls that can bail out earlier- I'd prefer to avoid
as
, as that bails out of type checking. There's athread.isThread()
assertion that perserves type safety. There's an implied "return early if it's not a thread" here, maybe we could make that more explicit and move this code further down to remove the need to cast
Check who the current reaction is from and reply only when the thread author reacted. Closes #173