Merge pull request #40 from smyalygames/main

feat(arabot): add functions to remove one or multiple sus notes
This commit is contained in:
Anthony 2022-07-28 04:59:05 +01:00 committed by GitHub
commit 26f3858759
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,7 +18,7 @@
*/ */
import { Command, RegisterBehavior } from '@sapphire/framework'; import { Command, RegisterBehavior } from '@sapphire/framework';
import { Message, MessageEmbed } from 'discord.js'; import { MessageEmbed } from 'discord.js';
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
import { addExistingUser, userExists } from '../../utils/dbExistingUser'; import { addExistingUser, userExists } from '../../utils/dbExistingUser';
@ -65,13 +65,51 @@ async function findNote(userId: string, active: boolean) {
return getNote; return getNote;
} }
async function deactivateNote(noteId: number) {
// Initialise the database connection
const prisma = new PrismaClient();
// Query to deactivate the specific sus note
await prisma.sus.update({
where: {
id: noteId,
},
data: {
active: false,
},
});
// Close the database connection
await prisma.$disconnect();
}
async function deactivateAllNotes(userId: string) {
// Initialise the database connection
const prisma = new PrismaClient();
// Query to deactivate the specific user's sus notes
await prisma.sus.updateMany({
where: {
userId: {
contains: userId,
},
},
data: {
active: false,
},
});
// Close the database connection
await prisma.$disconnect();
}
// Main command // Main command
export class SusCommand extends Command { export class SusCommand extends Command {
public constructor(context: Command.Context) { public constructor(context: Command.Context) {
super(context, { super(context, {
name: 'sus', name: 'sus',
description: 'Notes about users that are sus', description: 'Notes about users that are sus',
preconditions: [['ModOnly', 'VerifierOnly']], preconditions: [['VerifierOnly', 'ModOnly']],
}); });
} }
@ -81,7 +119,7 @@ export class SusCommand extends Command {
(builder) => builder (builder) => builder
.setName(this.name) .setName(this.name)
.setDescription(this.description) .setDescription(this.description)
// Subcommand to add a sus note // Subcommand to add a sus note
.addSubcommand((command) => command.setName('add') .addSubcommand((command) => command.setName('add')
.setDescription('Add a sus note about a user') .setDescription('Add a sus note about a user')
.addUserOption((option) => option.setName('user') .addUserOption((option) => option.setName('user')
@ -90,11 +128,23 @@ export class SusCommand extends Command {
.addStringOption((option) => option.setName('note') .addStringOption((option) => option.setName('note')
.setDescription('Note about the user') .setDescription('Note about the user')
.setRequired(true))) .setRequired(true)))
// Subcommand to list sus notes // Subcommand to list sus notes
.addSubcommand((command) => command.setName('view') .addSubcommand((command) => command.setName('view')
.setDescription('View a sus note for a user') .setDescription('View a sus note for a user')
.addUserOption((option) => option.setName('user') .addUserOption((option) => option.setName('user')
.setDescription('User to add the note') .setDescription('User to view the note of')
.setRequired(true)))
// Subcommand to remove a specific sus note
.addSubcommand((command) => command.setName('remove')
.setDescription('Remove a specific sus note')
.addIntegerOption((option) => option.setName('id')
.setDescription('Sus note ID')
.setRequired(true)))
// Subcommand to remove all sus notes
.addSubcommand((command) => command.setName('removeAll')
.setDescription('Remove all sus notes from a user')
.addUserOption((option) => option.setName('user')
.setDescription('User to remove the note from')
.setRequired(true))), .setRequired(true))),
{ {
behaviorWhenNotIdentical: RegisterBehavior.Overwrite, behaviorWhenNotIdentical: RegisterBehavior.Overwrite,
@ -114,6 +164,12 @@ export class SusCommand extends Command {
case 'view': { case 'view': {
return await this.listNote(interaction); return await this.listNote(interaction);
} }
case 'remove': {
return await this.removeNote(interaction);
}
case 'removeAll': {
return await this.removeAllNotes(interaction);
}
} }
// If subcommand is invalid // If subcommand is invalid
@ -176,7 +232,6 @@ export class SusCommand extends Command {
ephemeral: true, ephemeral: true,
fetchReply: true, fetchReply: true,
}); });
return;
} }
public async listNote(interaction: Command.ChatInputInteraction) { public async listNote(interaction: Command.ChatInputInteraction) {
@ -239,7 +294,11 @@ export class SusCommand extends Command {
.setColor('#0099ff') .setColor('#0099ff')
.setTitle(`Sus notes for ${user.username}`) .setTitle(`Sus notes for ${user.username}`)
.setThumbnail(user.avatarURL()!) .setThumbnail(user.avatarURL()!)
.addField(`Moderator: ${mod} Date: ${notes[notes.length - 1].time}`, notes[notes.length - 1].note); // TODO add a way to display more than 1 sus note
.addField(
`Sus ID: ${notes[notes.length - 1].id} | Moderator: ${mod} Date: ${notes[notes.length - 1].time}`,
notes[notes.length - 1].note,
);
// Sends the notes to the user // Sends the notes to the user
await interaction.reply({ await interaction.reply({
@ -249,25 +308,73 @@ export class SusCommand extends Command {
}); });
} }
public async messageRun(message: Message) { public async removeNote(interaction: Command.ChatInputInteraction) {
const msg = await message.channel.send('Ping?'); // Get the arguments
let noteId = interaction.options.getInteger('id');
const content = `Pong from JavaScript! Bot Latency ${Math.round(this.container.client.ws.ping)}ms. API Latency ${msg.createdTimestamp - message.createdTimestamp}ms.`; // Checks if all the variables are of the right type
if (noteId === null) {
// Checks if the user exists await interaction.reply({
if (message.member === null) { content: 'Error fetching id from Discord!',
await message.channel.send('Member not found'); ephemeral: true,
fetchReply: true,
});
return; return;
} }
// Checks if the user is on the database // Remove possibility of null from variables
if (!await userExists(message.member)) { noteId = noteId!;
// If they are not on the database, add them to the database
await addExistingUser(message.member); // TODO fetch the note and get mod input if they want to remove that note
// Remove the sus notes from the database
await deactivateNote(noteId);
await interaction.reply({
content: `Sus note ID ${noteId} has been removed successfully`,
ephemeral: true,
fetchReply: true,
});
}
public async removeAllNotes(interaction: Command.ChatInputInteraction) {
// Get the arguments
let user = interaction.options.getUser('user');
// Checks if all the variables are of the right type
if (user === null) {
await interaction.reply({
content: 'Error fetching user!',
ephemeral: true,
fetchReply: true,
});
return;
} }
await addToDatabase(message.member.id, message.member.id, 'This is a note :D'); // Remove possibility of null from variables
user = user!;
return msg.edit(content); // Check if the user had sus notes before trying to remove them
// Gets the sus notes from the database
const notes = await findNote(user.id, true);
// Checks if there are no notes on the user
if (notes.length === 0) {
await interaction.reply({
content: `${user} had no notes!`,
ephemeral: true,
fetchReply: true,
});
return;
}
// TODO display all notes from user and get mod input if they want to remove all those notes
// Remove the sus notes from the database
await deactivateAllNotes(user.id);
await interaction.reply({
content: `Sus notes have been removed for ${user} successfully`,
ephemeral: true,
fetchReply: true,
});
} }
} }