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
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/features/autothread.ts
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";
Expand All @@ -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 +22 to +30
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!")
);

};

Comment on lines +19 to +32
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.

const autoThread: ChannelHandlers = {
handleMessage: async ({ msg: maybeMessage }) => {
const msg = maybeMessage.partial
Expand Down Expand Up @@ -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
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

return;
}

const starter = thread.isThread()
? await thread.fetchStarterMessage()
: undefined;
Expand Down