Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

definoob
Copy link
Contributor

@definoob definoob commented Feb 3, 2022

Check who the current reaction is from and reply only when the thread author reacted. Closes #173

src/features/autothread.ts Outdated Show resolved Hide resolved
Comment on lines +19 to +32
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;
};

Copy link
Contributor Author

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.

@definoob definoob requested a review from vcarl February 3, 2022 16:31
Comment on lines +22 to +30
let flag = 0;

messages.forEach((message) => {
if (message.content.includes("This question has an answer!")) {
flag++;
}
});

return flag > 0;
Copy link
Member

@vcarl vcarl Feb 3, 2022

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)

Suggested change
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);
Copy link
Member

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)

Comment on lines +74 to +79
const threadMessages = fetchMessagesByUser(
thread as ThreadChannel,
reactibot,
);

if (checkIfBotReplied(threadMessages)) {
Copy link
Member

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 a thread.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

@vcarl vcarl changed the title Issue 173 Fix "accept answer" double-sending in some cases Feb 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Thread channels are double-sending "this has an answer" messages
2 participants