refactor(arabot): change variable names for users and remove some checks

This commit is contained in:
smyalygames 2023-03-04 00:03:27 +00:00
parent ed693f9597
commit 1870f75517

View File

@ -114,69 +114,64 @@ export class SusCommand extends Subcommand {
// Subcommand to add sus note // Subcommand to add sus note
public async addNote(interaction: Subcommand.ChatInputCommandInteraction) { public async addNote(interaction: Subcommand.ChatInputCommandInteraction) {
// Get the arguments // Get the arguments
const user = interaction.options.getUser('user'); const user = interaction.options.getUser('user', true);
const note = interaction.options.getString('note'); const note = interaction.options.getString('note', true);
const mod = interaction.user;
const { guild } = interaction; const { guild } = interaction;
// Checks if all the variables are of the right type // Checks if all the variables are of the right type
if (user === null || interaction.member === null || note === null || guild === null) { if (guild === null) {
await interaction.reply({ await interaction.reply({
content: 'Error fetching user!', content: 'Error fetching guild!',
ephemeral: true, ephemeral: true,
fetchReply: true,
}); });
return; return;
} }
const mod = interaction.member.user;
// Add the data to the database // Add the data to the database
// Check if the user exists on the database // Check if the user exists on the database
const userGuildMember = guild.members.cache.get(user.id); const member = guild.members.cache.get(user.id);
const modGuildMember = guild.members.cache.get(mod.id); const modMember = guild.members.cache.get(mod.id);
// TODO potentially add a method to add user by Snowflake
if (userGuildMember === undefined || modGuildMember === undefined) { if (member === undefined || modMember === undefined) {
await interaction.reply({ await interaction.reply({
content: 'Error fetching users!', content: 'Error fetching users!',
ephemeral: true, ephemeral: true,
fetchReply: true,
}); });
return; return;
} }
// Check if user and mod are on the database // Check if user and mod are on the database
if (!await userExists(userGuildMember.id)) { if (!await userExists(member.id)) {
await addExistingUser(userGuildMember); await addExistingUser(member);
} }
if (!await userExists(modGuildMember.id)) { if (!await userExists(modMember.id)) {
await addExistingUser(modGuildMember); await addExistingUser(modMember);
} }
await addToDatabase(user.id, mod.id, note); await addToDatabase(user.id, mod.id, note);
// Give the user the sus role they don't already have the sus note // Give the user the sus role they don't already have the sus note
if (!userGuildMember.roles.cache.has(IDs.roles.restrictions.sus)) { if (!member.roles.cache.has(IDs.roles.restrictions.sus)) {
await userGuildMember.roles.add(IDs.roles.restrictions.sus); await member.roles.add(IDs.roles.restrictions.sus);
} }
await interaction.reply({ await interaction.reply({
content: `${user} note: ${note}`, content: `${user} note: ${note}`,
ephemeral: true, ephemeral: true,
fetchReply: true,
}); });
} }
public async listNote(interaction: Subcommand.ChatInputCommandInteraction) { public async listNote(interaction: Subcommand.ChatInputCommandInteraction) {
// Get the arguments // Get the arguments
const user = interaction.options.getUser('user'); const user = interaction.options.getUser('user', true);
const { guild } = interaction; const { guild } = interaction;
// Checks if all the variables are of the right type // Checks if all the variables are of the right type
if (user === null || guild == null) { if (guild == null) {
await interaction.reply({ await interaction.reply({
content: 'Error fetching user!', content: 'Error fetching guild!',
ephemeral: true, ephemeral: true,
fetchReply: true,
}); });
return; return;
} }
@ -212,11 +207,12 @@ export class SusCommand extends Subcommand {
// Add up to 10 of the latest sus notes to the embed // Add up to 10 of the latest sus notes to the embed
for (let i = notes.length > 10 ? notes.length - 10 : 0; i < notes.length; i += 1) { for (let i = notes.length > 10 ? notes.length - 10 : 0; i < notes.length; i += 1) {
// Get mod name // Get mod name
const modGuildMember = guild.members.cache.get(notes[i].modId);
let mod = notes[i].modId; let mod = notes[i].modId;
if (modGuildMember !== undefined) { const modMember = guild.members.cache.get(mod);
mod = modGuildMember!.displayName; if (modMember !== undefined) {
mod = modMember.displayName;
} }
// Add sus note to embed // Add sus note to embed
noteEmbed.addFields({ noteEmbed.addFields({
name: `Sus ID: ${notes[i].id} | Moderator: ${mod} | Date: <t:${Math.floor(notes[i].time.getTime() / 1000)}>`, name: `Sus ID: ${notes[i].id} | Moderator: ${mod} | Date: <t:${Math.floor(notes[i].time.getTime() / 1000)}>`,
@ -234,13 +230,13 @@ export class SusCommand extends Subcommand {
public async removeNote(interaction: Subcommand.ChatInputCommandInteraction) { public async removeNote(interaction: Subcommand.ChatInputCommandInteraction) {
// Get the arguments // Get the arguments
const noteId = interaction.options.getInteger('id'); const noteId = interaction.options.getInteger('id', true);
const { guild, channel } = interaction; const { guild, channel } = interaction;
// Checks if all the variables are of the right type // Checks if all the variables are of the right type
if (noteId === null || guild === null || channel === null || interaction.member === null) { if (guild === null || channel === null) {
await interaction.reply({ await interaction.reply({
content: 'Error fetching id from Discord!', content: 'Error fetching guild or channel!',
ephemeral: true, ephemeral: true,
fetchReply: true, fetchReply: true,
}); });
@ -261,11 +257,11 @@ export class SusCommand extends Subcommand {
} }
// 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 user = await guild.members.cache.get(note.userId); const member = await guild.members.cache.get(note.userId);
const mod = await guild.members.cache.get(note.modId); const mod = await guild.members.cache.get(note.modId);
// TODO fix if user left the server // TODO fix if user left the server
if (user === undefined || mod === undefined) { if (member === undefined || mod === undefined) {
await interaction.reply({ await interaction.reply({
content: 'Error fetching users from Discord!', content: 'Error fetching users from Discord!',
ephemeral: true, ephemeral: true,
@ -276,8 +272,8 @@ export class SusCommand extends Subcommand {
// Get user's name // Get user's name
let userName = note.userId; let userName = note.userId;
if (user !== undefined) { if (member !== undefined) {
userName = user.displayName; userName = member.displayName;
} }
// Get mod name // Get mod name
@ -290,7 +286,7 @@ export class SusCommand extends Subcommand {
const noteEmbed = new EmbedBuilder() const noteEmbed = new EmbedBuilder()
.setColor('#ff0000') .setColor('#ff0000')
.setTitle(`Sus note for ${userName}`) .setTitle(`Sus note for ${userName}`)
.setThumbnail(user.avatarURL()!) // TODO avatar does not show when run .setThumbnail(member.displayAvatarURL())
.addFields({ .addFields({
name: `ID: ${noteId} | Moderator: ${modName} | Date: <t:${Math.floor(note.time.getTime() / 1000)}>`, name: `ID: ${noteId} | Moderator: ${modName} | Date: <t:${Math.floor(note.time.getTime() / 1000)}>`,
value: note.note, value: note.note,
@ -334,17 +330,17 @@ 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: `${user}'s sus note (ID: ${noteId}) has been successfully removed`, content: `${member}'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(user.id, true); const notes = await findNotes(member.id, 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) {
await user.roles.remove(IDs.roles.restrictions.sus); await member.roles.remove(IDs.roles.restrictions.sus);
} }
} }
}); });
@ -359,23 +355,23 @@ export class SusCommand extends Subcommand {
public async removeAllNotes(interaction: Subcommand.ChatInputCommandInteraction) { public async removeAllNotes(interaction: Subcommand.ChatInputCommandInteraction) {
// Get the arguments // Get the arguments
const user = interaction.options.getUser('user'); const user = interaction.options.getUser('user', true);
const { guild, channel } = interaction; const { guild, channel } = interaction;
// Checks if all the variables are of the right type // Checks if all the variables are of the right type
if (user === null || guild === null || channel === null) { if (guild === null || channel === null) {
await interaction.reply({ await interaction.reply({
content: 'Error fetching user!', content: 'Error fetching guild or channel!',
ephemeral: true, ephemeral: true,
fetchReply: true, fetchReply: true,
}); });
return; return;
} }
const userGuildMember = guild.members.cache.get(user.id); const member = guild.members.cache.get(user.id);
// Checks if managed to find GuildMember for the user // Checks if managed to find GuildMember for the user
if (userGuildMember === undefined) { if (member === undefined) {
await interaction.reply({ await interaction.reply({
content: 'Error fetching user!', content: 'Error fetching user!',
ephemeral: true, ephemeral: true,
@ -407,8 +403,8 @@ export class SusCommand extends Subcommand {
// Add up to 10 of the latest sus notes to the embed // Add up to 10 of the latest sus notes to the embed
for (let i = notes.length > 10 ? notes.length - 10 : 0; i < notes.length; i += 1) { for (let i = notes.length > 10 ? notes.length - 10 : 0; i < notes.length; i += 1) {
// Get mod name // Get mod name
const modGuildMember = guild.members.cache.get(notes[i].modId);
let mod = notes[i].modId; let mod = notes[i].modId;
const modGuildMember = guild.members.cache.get(mod);
if (modGuildMember !== undefined) { if (modGuildMember !== undefined) {
mod = modGuildMember.displayName; mod = modGuildMember.displayName;
} }
@ -458,7 +454,7 @@ export class SusCommand extends Subcommand {
// Remove sus note from database // Remove sus note from database
await deactivateAllNotes(user.id); await deactivateAllNotes(user.id);
await interaction.editReply({ await interaction.editReply({
content: `Removed all of ${userGuildMember}'s sus notes successfully`, content: `Removed all of ${member}'s sus notes successfully`,
embeds: [], embeds: [],
}); });
} }
@ -472,7 +468,7 @@ export class SusCommand extends Subcommand {
}); });
// Remove sus role from the user // Remove sus role from the user
await userGuildMember.roles.remove(IDs.roles.restrictions.sus); await member.roles.remove(IDs.roles.restrictions.sus);
} }
// Non Application Command method of adding a sus note // Non Application Command method of adding a sus note