Skip to content

Commit

Permalink
Improvements to logs, fix crash in resume review, experience polish (#…
Browse files Browse the repository at this point in the history
…384)

* Ensure we never try to send too large a log

* Link back to resume when logging feedback

* Fix links in recurring messages

* Exclude message author from emoji reaction count, fixes #358

* Add exponential backoff retry helper

* Add durability for initial messages fetch, fixes #382
  • Loading branch information
vcarl authored Jun 11, 2024
1 parent cb57bd9 commit 5612f62
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/features/emojiMod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ const emojiMod: ChannelHandlers = {
reactor,
message: fullMessage,
reaction: fullReaction,
usersWhoReacted: usersWhoReacted.filter((x): x is GuildMember =>
Boolean(x),
usersWhoReacted: usersWhoReacted.filter(
(x): x is GuildMember => Boolean(x) && authorMember.id !== reactor.id,
),
});
},
Expand Down
2 changes: 1 addition & 1 deletion src/features/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const channelLog =

if (channel?.type == ChannelType.GuildText) {
channel.send({
content: `[${type}] ${text}`,
content: `[${type}] ${text}`.slice(0, 2000),
allowedMentions: { users: [] },
});
}
Expand Down
12 changes: 9 additions & 3 deletions src/features/resume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
findResumeAttachment,
REVIEW_COMMAND,
} from "./resume-review";
import { constructDiscordLink } from "../helpers/discord";
import { retry } from "./retry";

const openai = new OpenAI({
apiKey: openAiKey,
Expand Down Expand Up @@ -74,7 +76,8 @@ export const resumeResources = async (bot: Client) => {
deferred.edit("Looking for a resume…");
const messages = await interaction.channel.messages.fetch();

const firstMessage = await interaction.channel.fetchStarterMessage();
const { fetchStarterMessage } = interaction.channel;
const firstMessage = await retry(() => fetchStarterMessage(), 5, 10);
if (!firstMessage) {
await interaction.reply({
ephemeral: true,
Expand Down Expand Up @@ -162,8 +165,11 @@ export const resumeResources = async (bot: Client) => {
console.log({ content });
const trimmed =
content.at(0)?.slice(0, 2000) ?? "Oops! Something went wrong.";
logger.log("[RESUME]", `Feedback given:`);
logger.log("[RESUME]", trimmed);
logger.log(
"RESUME",
`Feedback given: ${constructDiscordLink(firstMessage)}`,
);
logger.log("RESUME", trimmed);
deferred.edit({
content: trimmed,
});
Expand Down
49 changes: 49 additions & 0 deletions src/features/retry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* This is a chatGPT generated function, don't blame me. I did verify it works:
```ts
console.log(retryCount, {
backoff: delay * Math.pow(factor, retries - retryCount),
retryCount,
});
```
as the first line of the `attempt` function produced:
```
> 5 { backoff: 20, retryCount: 5 }
> 4 { backoff: 40, retryCount: 4 }
> 3 { backoff: 80, retryCount: 3 }
> 2 { backoff: 160, retryCount: 2 }
> 1 { backoff: 320, retryCount: 1 }
> 0 { backoff: 640, retryCount: 0 }
```
* Retries a promise-returning function with exponential backoff.
*
* @template T
* @param {() => Promise<T>} fn - The function returning a promise to be retried.
* @param {number} [retries=3] - The number of retry attempts.
* @param {number} [delayMs=1000] - The initial delay in milliseconds before retrying.
* @returns {Promise<T>} A promise that resolves with the result of the function or rejects after all retries have been exhausted.
*/
export function retry<T>(
fn: () => Promise<T>,
retries = 3,
delayMs = 1000,
): Promise<T> {
return new Promise((resolve, reject) => {
const attempt = (retryCount: number) => {
fn()
.then(resolve)
.catch((error) => {
if (retryCount <= 0) {
reject(error);
} else {
setTimeout(() => {
attempt(retryCount - 1);
}, delayMs * Math.pow(2, retries - retryCount));
}
});
};

attempt(retries);
});
}
18 changes: 9 additions & 9 deletions src/features/scheduled-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,17 @@ How do I ask a good question <https://stackoverflow.com/help/how-to-ask>
message: {
content: `Check our the other channels too! This is our highest-traffic channel, which may mean your question gets missed as other discussions happen.
#help-js For questions about pure Javscript problems.
#help-styling For questions about CSS or other visual problems.
#help-backend For questions about issues with your server code.
#code-review Get deeper review of a snippet of code.
#jobs-advice If you have a question about your job or career, ask it in here.
#general-tech Discussion of non-JS code, or that new laptop you're deciding on.
#tooling for questions about building, linting, generating, or otherwise processing your code.
<#565213527673929729> For questions about pure Javscript problems.
<#105765765117935616> For questions about CSS or other visual problems.
<#145170347921113088> For questions about issues with your server code.
<#105765859191975936> Get deeper review of a snippet of code.
<#287623405946011648> If you have a question about your job or career, ask it in here.
<#547620660482932737> Discussion of non-JS code, or that new laptop you're deciding on.
<#108428584783220736> for questions about building, linting, generating, or otherwise processing your code.
Looking for work? Trying to hire? Check out #job-board, or <https://reactiflux.com/jobs>
Looking for work? Trying to hire? Check out <#103882387330457600>, or <https://reactiflux.com/jobs>
Has someone been really helpful? Shoutout who and what in #thanks! We keep an eye in there as one way to find new MVPs. Give us all the reactions in there too!
Has someone been really helpful? Shoutout who and what in <#798567961468076072>! We keep an eye in there as one way to find new MVPs. Give us all the reactions in there too!
Please remember our Code of Conduct: <https://reactiflux.com/conduct>
and our guidelines for promotion: <https://reactiflux.com/promotion>
Expand Down

0 comments on commit 5612f62

Please sign in to comment.