Skip to content

Commit

Permalink
refactor: Retry role assignment on member join with exponential backoff
Browse files Browse the repository at this point in the history
  • Loading branch information
juddisjudd committed Jun 4, 2024
1 parent 669571e commit 60290bf
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/events/onMemberJoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { GuildMember, TextChannel } from 'discord.js';
import { member_activity_channel } from '../settings.json';
import logger from '../utils/logger';

const MAX_RETRIES = 3;
const RETRY_DELAY = 5 * 60 * 1000;

export const onMemberJoin = async (member: GuildMember) => {
const channel = member.guild.channels.cache.get(member_activity_channel) as TextChannel;
if (!channel) return;
Expand All @@ -17,10 +20,19 @@ export const onMemberJoin = async (member: GuildMember) => {
return;
}

try {
await member.roles.add(role);
logger.info(`Assigned role '${role.name}' to ${member.displayName}`);
} catch (error) {
logger.error(`Could not assign role to ${member.displayName}: ${error}`);
let retries = 0;
while (retries < MAX_RETRIES) {
try {
await member.roles.add(role);
logger.info(`Assigned role '${role.name}' to ${member.displayName}`);
break;
} catch (error) {
logger.error(`Could not assign role to ${member.displayName}: ${error}`);
retries++;
if (retries < MAX_RETRIES) {
logger.info(`Retrying in ${RETRY_DELAY / 1000} seconds...`);
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY));
}
}
}
};
};

0 comments on commit 60290bf

Please sign in to comment.