refactor(arabot): change removing sus notes to have more checks for types

This commit is contained in:
Anthony Berg 2024-01-13 00:29:57 +00:00
parent 785e844da8
commit c8eb8299dd

View File

@ -28,6 +28,7 @@ import {
User, User,
Guild, Guild,
TextChannel, TextChannel,
GuildMember,
} from 'discord.js'; } from 'discord.js';
import type { Message } from 'discord.js'; import type { Message } from 'discord.js';
import { isMessageInstance } from '@sapphire/discord.js-utilities'; import { isMessageInstance } from '@sapphire/discord.js-utilities';
@ -344,39 +345,30 @@ export class SusCommand extends Subcommand {
return; return;
} }
const userId = note.userId;
const modId = note.modId;
// Get user GuildMembers for user and mod and person who ran command // Get user GuildMembers for user and mod and person who ran command
const member = await guild.members.cache.get(note.userId); const user = this.container.client.users.cache.get(note.userId);
const mod = await guild.members.cache.get(note.modId); const mod = this.container.client.users.cache.get(note.modId);
// TODO fix if user left the server let userDisplay = userId;
if (member === undefined || mod === undefined) { if (user instanceof User) {
await interaction.reply({ userDisplay = user.tag;
content: 'Error fetching users from Discord!',
ephemeral: true,
fetchReply: true,
});
return;
} }
// Get user's name let modDisplay = modId;
let userName = note.userId; if (mod instanceof User) {
if (member !== undefined) { modDisplay = mod.displayName;
userName = member.displayName;
}
// Get mod name
let modName = note.modId;
if (mod !== undefined) {
modName = mod.displayName;
} }
// Create an embed for the note // Create an embed for the note
const noteEmbed = new EmbedBuilder() const noteEmbed = new EmbedBuilder()
.setColor('#ff0000') .setColor('#ff0000')
.setTitle(`Sus note for ${userName}`) .setTitle(`Sus note for ${userDisplay}`)
.setThumbnail(member.displayAvatarURL()) .setThumbnail(user!.displayAvatarURL())
.addFields({ .addFields({
name: `ID: ${noteId} | Moderator: ${modName} | Date: <t:${Math.floor( name: `ID: ${noteId} | Moderator: ${modDisplay} | Date: <t:${Math.floor(
note.time.getTime() / 1000, note.time.getTime() / 1000,
)}>`, )}>`,
value: note.note, value: note.note,
@ -419,19 +411,28 @@ export class SusCommand extends Subcommand {
if (button.customId === `delete${noteId}`) { if (button.customId === `delete${noteId}`) {
await deactivateNote(noteId); await deactivateNote(noteId);
await interaction.editReply({ await interaction.editReply({
content: `${member}'s sus note (ID: ${noteId}) has been successfully removed`, content: `${
user instanceof User ? user : userDisplay
}'s sus note (ID: ${noteId}) has been successfully removed`,
embeds: [], embeds: [],
}); });
// TODO create a new Prisma function to only count and not to get a whole list of sus notes // TODO create a new Prisma function to only count and not to get a whole list of sus notes
// Check how many notes the user has and if 0, then remove sus note // Check how many notes the user has and if 0, then remove sus note
const notes = await findNotes(member.id, true); const notes = await findNotes(userId, true);
// Checks if there are no notes on the user and if there's none, remove the sus role // Checks if there are no notes on the user and if there's none, remove the sus role
if (notes.length === 0) { if (notes.length === 0) {
let member = guild.members.cache.get(userId);
if (!(member instanceof GuildMember)) {
member = await guild.members.fetch(userId).catch(() => undefined);
}
if (member instanceof GuildMember) {
await member.roles.remove(IDs.roles.restrictions.sus); await member.roles.remove(IDs.roles.restrictions.sus);
} }
} }
}
}); });
// Remove the buttons after they have been clicked // Remove the buttons after they have been clicked