Allow phrases to be purged

This commit is contained in:
Tarrgon
2025-05-29 10:28:23 -04:00
parent f7723e8aad
commit b5e631a965
2 changed files with 27 additions and 5 deletions
+23 -5
View File
@@ -1,4 +1,4 @@
import { ApplicationIntegrationType, AutocompleteInteraction, ChatInputCommandInteraction, Client, InteractionContextType, MessageFlags, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js'; import { ApplicationIntegrationType, AutocompleteInteraction, ChatInputCommandInteraction, Client, InteractionContextType, MessageFlags, PermissionFlagsBits, SlashCommandBuilder, User } from 'discord.js';
import { Database } from '../shared/Database'; import { Database } from '../shared/Database';
import { getE621User } from '../utils'; import { getE621User } from '../utils';
import { config } from '../config'; import { config } from '../config';
@@ -8,7 +8,7 @@ const MIN_PHRASE_LENGTH = 1;
const MAX_PHRASE_LENGTH = 512; const MAX_PHRASE_LENGTH = 512;
type SubcommandGroup = 'admin' | 'personal'; type SubcommandGroup = 'admin' | 'personal';
type Subcommand = 'add' | 'remove' | 'list' | 'dump'; type Subcommand = 'add' | 'remove' | 'list' | 'dump' | 'purge';
export default { export default {
name: 'phrases', name: 'phrases',
@@ -92,13 +92,21 @@ export default {
subcommand subcommand
.setName('dump') .setName('dump')
.setDescription('List all notification phrases.') .setDescription('List all notification phrases.')
)
.addSubcommand(subcommand =>
subcommand
.setName('purge')
.setDescription("Purge a user's phrases.")
.addUserOption(option =>
option
.setName('user')
.setDescription('The user to purge the phrases of.')
.setRequired(true)
)
), ),
handler: async function (client: Client, interaction: ChatInputCommandInteraction) { handler: async function (client: Client, interaction: ChatInputCommandInteraction) {
const subcommandGroup: SubcommandGroup | null = interaction.options.getSubcommandGroup() as SubcommandGroup; const subcommandGroup: SubcommandGroup | null = interaction.options.getSubcommandGroup() as SubcommandGroup;
const subcommand: Subcommand | null = interaction.options.getSubcommand() as Subcommand; const subcommand: Subcommand | null = interaction.options.getSubcommand() as Subcommand;
if (subcommand == 'dump') {
return dumpPhrases(interaction);
}
switch (subcommand) { switch (subcommand) {
case 'add': case 'add':
@@ -107,6 +115,10 @@ export default {
return removePhrase(interaction, interaction.options.getNumber('phrase', true), subcommandGroup!); return removePhrase(interaction, interaction.options.getNumber('phrase', true), subcommandGroup!);
case 'list': case 'list':
return listPhrases(interaction, subcommandGroup!); return listPhrases(interaction, subcommandGroup!);
case 'dump':
return dumpPhrases(interaction);
case 'purge':
return purgePhrases(interaction, interaction.options.getUser('user', true));
} }
}, },
autoComplete: async function (client: Client, interaction: AutocompleteInteraction) { autoComplete: async function (client: Client, interaction: AutocompleteInteraction) {
@@ -128,6 +140,12 @@ export default {
} }
}; };
async function purgePhrases(interaction: ChatInputCommandInteraction, user: User) {
const count = await Database.removeAllTicketPhrasesFor(user.id);
interaction.reply(`${count} phrases purged.`);
}
async function dumpPhrases(interaction: ChatInputCommandInteraction) { async function dumpPhrases(interaction: ChatInputCommandInteraction) {
let content = ''; let content = '';
+4
View File
@@ -245,6 +245,10 @@ export class Database {
await Database.db.run('DELETE from ticket_phrases WHERE id = ?', id); await Database.db.run('DELETE from ticket_phrases WHERE id = ?', id);
} }
static async removeAllTicketPhrasesFor(id: string): Promise<number> {
return (await Database.db.run('DELETE from ticket_phrases WHERE user_id = ?', id)).changes!;
}
static async getTicketPhrasesFor(userId: string): Promise<TicketPhrase[]> { static async getTicketPhrasesFor(userId: string): Promise<TicketPhrase[]> {
return await Database.db.all<TicketPhrase[]>('SELECT * from ticket_phrases WHERE user_id = ?', userId); return await Database.db.all<TicketPhrase[]>('SELECT * from ticket_phrases WHERE user_id = ?', userId);
} }