diff --git a/src/commands/notes.ts b/src/commands/notes.ts index 57a4e09..0ace945 100644 --- a/src/commands/notes.ts +++ b/src/commands/notes.ts @@ -30,6 +30,30 @@ export default { .setRequired(true) ) ) + .addSubcommand(subcommand => + subcommand + .setName('edit') + .setDescription('Edit notes on a user.') + .addStringOption(option => + option + .setName('user') + .setDescription('The discord user mention, or ID, to edit the notes of.') + .setRequired(true) + ) + .addIntegerOption(option => + option + .setName('note') + .setDescription('The note to edit.') + .setRequired(true) + .setAutocomplete(true) + ) + .addStringOption(option => + option + .setName('new-reason') + .setDescription('The new reason for the note.') + .setRequired(true) + ) + ) .addSubcommand(subcommand => subcommand .setName('remove') @@ -125,6 +149,42 @@ export default { await Database.removeNote(noteId); interaction.editReply('Removed note.'); + } else if (subcommand == 'edit') { + const noteId = interaction.options.getInteger('note', true); + + const notes = await Database.getNotes(idToUse); + const note = notes.find(n => n.id == noteId); + + if (!note) return interaction.editReply('Note not found.'); + + const reason = interaction.options.getString('new-reason', true); + + logCustomEvent(interaction.guild!, { + title: 'Note Edited', + description: null, + color: 0x00FF00, + timestamp: new Date(), + fields: [ + { + name: 'User', + value: `<@${interaction.user.id}>\n${interaction.user.username}`, + inline: true + }, + { + name: 'Old reason', + value: note.reason + }, + { + name: 'New reason', + value: reason, + inline: true + } + ] + }); + + await Database.editNote(noteId, note.reason, reason, interaction.user.id); + + interaction.editReply(`Note on <@${idToUse}> edited.\n\nNew reason:\n${reason}`); } else if (subcommand == 'list') { const noteMessage = await getNoteMessage(idToUse, 1); diff --git a/src/shared/Database.ts b/src/shared/Database.ts index 9bf159d..5c7679f 100644 --- a/src/shared/Database.ts +++ b/src/shared/Database.ts @@ -64,6 +64,15 @@ const DB_SCHEMA = ` timestamp datetime NOT NULL DEFAULT (datetime('now', 'localtime')) ); + CREATE TABLE IF NOT EXISTS note_edits ( + id INTEGER PRIMARY KEY, + note_id INTEGER, + mod_id TEXT, + previous_reason TEXT, + timestamp datetime NOT NULL DEFAULT (datetime('now', 'localtime')), + FOREIGN KEY(note_id) REFERENCES notes(id) + ); + CREATE TABLE IF NOT EXISTS bans ( id INTEGER PRIMARY KEY, user_id TEXT, @@ -302,6 +311,11 @@ export class Database { await Database.db.run('INSERT INTO notes(user_id, reason, mod_id) VALUES (?, ?, ?)', userId, reason, modId); } + static async editNote(id: number, oldReason: string, newReason: string, modId: string) { + await Database.db.run('UPDATE notes SET reason = ?, mod_id = ? WHERE id = ?', newReason, modId, id); + await Database.db.run('INSERT INTO note_edits(note_id, mod_id, previous_reason) VALUES (?, ?, ?)', id, modId, oldReason); + } + static async removeNote(id: number): Promise { const res = await Database.db.run('DELETE from notes WHERE id = ?', id);