diff --git a/src/commands/database.ts b/src/commands/database.ts new file mode 100644 index 0000000..ebf8e76 --- /dev/null +++ b/src/commands/database.ts @@ -0,0 +1,48 @@ +import { ApplicationIntegrationType, ChatInputCommandInteraction, Client, InteractionContextType, MessageFlags, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js'; +import { Database } from '../shared/Database'; +import { getIdFromInput } from '../utils'; + +export default { + name: 'database', + data: new SlashCommandBuilder() + .setName('database') + .setDescription('Database access.') + .setIntegrationTypes(ApplicationIntegrationType.GuildInstall) + .setContexts(InteractionContextType.Guild) + .setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild) + .addSubcommandGroup(subcommandGroup => + subcommandGroup + .setName('messages') + .setDescription('Access to messages in the database.') + .addSubcommand(subcommand => + subcommand + .setName('purge') + .setDescription("Purge a user's messages from the database.") + .addStringOption(option => + option + .setName('user') + .setDescription('The discord user id, or mention, of the user.') + .setRequired(true) + ) + ) + ), + handler: async function (client: Client, interaction: ChatInputCommandInteraction) { + await interaction.deferReply({ flags: [MessageFlags.Ephemeral] }); + + const group = interaction.options.getSubcommandGroup(true); + + const subcommand = interaction.options.getSubcommand(true); + + if (group == 'messages') { + if (subcommand == 'purge') { + const discordUserInput = interaction.options.getString('discord-user', true); + + const idToUse = getIdFromInput(discordUserInput); + + await Database.purgeMessagesFrom(idToUse); + + return interaction.editReply('Messages purged.'); + } + } + } +}; \ No newline at end of file diff --git a/src/shared/Database.ts b/src/shared/Database.ts index 1a9b3f5..d7f2c4b 100644 --- a/src/shared/Database.ts +++ b/src/shared/Database.ts @@ -173,7 +173,7 @@ export class Database { } static async purgeMessagesFrom(id: string) { - await Database.db.run('DELETE from messages WHERE '); + await Database.db.run('DELETE from messages WHERE author_id_hash = ?', Encrypter.hash(id)); } //#endregion