From ea58ecc2616ce805ccd8185e6fe07e33c325acdf Mon Sep 17 00:00:00 2001 From: smyalygames Date: Sat, 18 Feb 2023 03:08:38 +0000 Subject: [PATCH] fix(arabot): fixed issues with using GuildMember instead of User leading to undefined instances --- src/commands/mod/restriction/restrict.ts | 12 ++++++++---- src/commands/mod/restriction/unrestrict.ts | 22 ++++++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/commands/mod/restriction/restrict.ts b/src/commands/mod/restriction/restrict.ts index 50f9873..cfc3915 100644 --- a/src/commands/mod/restriction/restrict.ts +++ b/src/commands/mod/restriction/restrict.ts @@ -61,7 +61,11 @@ export async function restrictRun( let user = guild.client.users.cache.get(userId); if (user === undefined) { - user = await guild.client.users.fetch(userId) as User; + user = await guild.client.users.fetch(userId); + if (user === undefined) { + info.message = 'Error fetching user'; + return info; + } } // Gets mod's GuildMember @@ -227,7 +231,7 @@ export async function restrictRun( // Restrict the user on the database await restrict(userId, modId, reason, section); - info.message = `Restricted ${member}`; + info.message = `Restricted ${user}`; info.success = true; // Log the ban @@ -239,7 +243,7 @@ export async function restrictRun( .fetch(IDs.channels.logs.restricted) as TextChannel | undefined; if (logChannel === undefined) { container.logger.error('Restrict Error: Could not fetch log channel'); - info.message = `Restricted ${member} but could not find the log channel. This has been logged to the database.`; + info.message = `Restricted ${user} but could not find the log channel. This has been logged to the database.`; return info; } } @@ -248,7 +252,7 @@ export async function restrictRun( .setColor('#FF6700') .setAuthor({ name: `Restricted ${user.tag}`, iconURL: `${user.avatarURL()}` }) .addFields( - { name: 'User', value: `${member}`, inline: true }, + { name: 'User', value: `${user}`, inline: true }, { name: 'Moderator', value: `${mod}`, inline: true }, { name: 'Reason', value: reason }, ) diff --git a/src/commands/mod/restriction/unrestrict.ts b/src/commands/mod/restriction/unrestrict.ts index 48656f0..42c3d86 100644 --- a/src/commands/mod/restriction/unrestrict.ts +++ b/src/commands/mod/restriction/unrestrict.ts @@ -121,6 +121,16 @@ export class UnRestrictCommand extends Command { success: false, }; + let user = guild.client.users.cache.get(userId); + + if (user === undefined) { + user = await guild.client.users.fetch(userId); + if (user === undefined) { + info.message = 'Error fetching user'; + return info; + } + } + // Gets mod's GuildMember const mod = guild.members.cache.get(modId); @@ -157,7 +167,7 @@ export class UnRestrictCommand extends Command { // Checks if the user is not restricted if (!member.roles.cache.hasAny(...restrictRoles)) { - info.message = `${member} is not restricted!`; + info.message = `${user} is not restricted!`; return info; } @@ -219,24 +229,24 @@ export class UnRestrictCommand extends Command { .fetch(IDs.channels.logs.restricted) as TextChannel | undefined; if (logChannel === undefined) { this.container.logger.error('Restrict Error: Could not fetch log channel'); - info.message = `Unrestricted ${member} but could not find the log channel. This has been logged to the database.`; + info.message = `Unrestricted ${user} but could not find the log channel. This has been logged to the database.`; return info; } } const message = new EmbedBuilder() .setColor('#28A745') - .setAuthor({ name: `Unrestricted ${member.user.tag}`, iconURL: `${member.user.avatarURL()}` }) + .setAuthor({ name: `Unrestricted ${user.tag}`, iconURL: `${user.avatarURL()}` }) .addFields( - { name: 'User', value: `${member}`, inline: true }, + { name: 'User', value: `${user}`, inline: true }, { name: 'Moderator', value: `${mod}`, inline: true }, ) .setTimestamp() - .setFooter({ text: `ID: ${member.id}` }); + .setFooter({ text: `ID: ${userId}` }); await logChannel.send({ embeds: [message] }); - info.message = `Unrestricted ${member}`; + info.message = `Unrestricted ${user}`; return info; } }