From 1ea0154c9fe5efb8b1e902d666233a814d084a6d Mon Sep 17 00:00:00 2001 From: tippfehlr Date: Fri, 30 Aug 2024 16:31:16 +0200 Subject: [PATCH] feat: add /reset command Resets the whole bot incl. activityStats for a guild Closes: #89 --- locales/cs.json | 10 +++- locales/de.json | 10 +++- locales/en-US.json | 4 ++ locales/fr.json | 10 +++- locales/nl.json | 10 +++- locales/pt-BR.json | 10 +++- locales/ru.json | 10 +++- locales/uk.json | 10 +++- src/modules/bot.ready.ts | 2 + src/modules/commands/reset.ts | 86 +++++++++++++++++++++++++++++++++++ 10 files changed, 155 insertions(+), 7 deletions(-) create mode 100644 src/modules/commands/reset.ts diff --git a/locales/cs.json b/locales/cs.json index cccf287..4e7c190 100644 --- a/locales/cs.json +++ b/locales/cs.json @@ -104,7 +104,8 @@ "description": "re-check all users/roles", "success": "checked {{{usersChecked}}} users, added {{{added}}} and removed {{{removed}}} roles", "in-progress": "IN PROGRESS: already checked {{{usersChecked}}}/{{{totalUsersToCheck}}} users, added {{{added}}} and removed {{{removed}}} roles.", - "askForManageRolesPermission": "I don’t have the `MANAGE_ROLES` permission but need it to assign roles.\n Please add the permission to the “Activity Roles” role. Otherwise the bot will leave the guild automatically." + "askForManageRolesPermission": "I don’t have the `MANAGE_ROLES` permission but need it to assign roles.\n Please add the permission to the “Activity Roles” role. Otherwise the bot will leave the guild automatically.", + "alreadyRunning": "A `/checkroles` request is already running for this guild." }, "Cancelled": "Cancelled", "deleteActivityRole": { @@ -118,5 +119,12 @@ }, "deleteActivityRoles": { "deleteAllConfirmation": "Are you sure you want to delete all activity roles?" + }, + "presenceUpdate": { + "roleHigherThanBotRole": "I don’t have permission to manage %s because it is higher than my highest role.\nPlease move one of my roles above all Activity Roles." + }, + "reset": { + "description": "resets the bot for this guild", + "confirmationPrompt": ":warning: You are about to reset the whole bot including all your activity roles. Roles will not be removed from members, and the bot will not remove them even if you create a new Activity Role.\nThis is normally not necessary! Are you _**absolutely**_ sure?" } } diff --git a/locales/de.json b/locales/de.json index 191fc5b..7d729f7 100644 --- a/locales/de.json +++ b/locales/de.json @@ -104,7 +104,8 @@ "description": "re-check all users/roles", "success": "checked {{{usersChecked}}} users, added {{{added}}} and removed {{{removed}}} roles", "in-progress": "IN PROGRESS: already checked {{{usersChecked}}}/{{{totalUsersToCheck}}} users, added {{{added}}} and removed {{{removed}}} roles.", - "askForManageRolesPermission": "I don’t have the `MANAGE_ROLES` permission but need it to assign roles.\n Please add the permission to the “Activity Roles” role. Otherwise the bot will leave the guild automatically." + "askForManageRolesPermission": "I don’t have the `MANAGE_ROLES` permission but need it to assign roles.\n Please add the permission to the “Activity Roles” role. Otherwise the bot will leave the guild automatically.", + "alreadyRunning": "A `/checkroles` request is already running for this guild." }, "Cancelled": "Cancelled", "deleteActivityRole": { @@ -118,5 +119,12 @@ }, "deleteActivityRoles": { "deleteAllConfirmation": "Are you sure you want to delete all activity roles?" + }, + "presenceUpdate": { + "roleHigherThanBotRole": "I don’t have permission to manage %s because it is higher than my highest role.\nPlease move one of my roles above all Activity Roles." + }, + "reset": { + "description": "resets the bot for this guild", + "confirmationPrompt": ":warning: You are about to reset the whole bot including all your activity roles. Roles will not be removed from members, and the bot will not remove them even if you create a new Activity Role.\nThis is normally not necessary! Are you _**absolutely**_ sure?" } } diff --git a/locales/en-US.json b/locales/en-US.json index d036def..56b6eb2 100644 --- a/locales/en-US.json +++ b/locales/en-US.json @@ -122,5 +122,9 @@ }, "presenceUpdate": { "roleHigherThanBotRole": "I don’t have permission to manage %s because it is higher than my highest role.\nPlease move one of my roles above all Activity Roles." + }, + "reset": { + "description": "resets the bot for this guild", + "confirmationPrompt": ":warning: You are about to reset the whole bot including all your activity roles. Roles will not be removed from members, and the bot will not remove them even if you create a new Activity Role.\nThis is normally not necessary! Are you _**absolutely**_ sure?" } } diff --git a/locales/fr.json b/locales/fr.json index dfdd58f..fd9a5a0 100644 --- a/locales/fr.json +++ b/locales/fr.json @@ -104,7 +104,8 @@ "description": "re-vérifie tous les utilisateurs/rôles", "success": "vérifié {{{usersChecked}}} utilisateurs, ajouté {{{added}}} et retiré {{{removed}}} rôles", "in-progress": "EN COURS : a déjà vérifié {{{usersChecked}}}/{{{totalUsersToCheck}}} utilisateurs, a ajouté {{{added}}} et a supprimé {{{removed}}} rôles.", - "askForManageRolesPermission": "Je n'ai pas la permission `GERER_LES_RÔLES` mais j'en ai besoin pour assigner des rôles.\n Veuillez ajouter la permission au rôle \"Activity Roles\". Sinon, le robot quittera automatiquement le serveur." + "askForManageRolesPermission": "Je n'ai pas la permission `GERER_LES_RÔLES` mais j'en ai besoin pour assigner des rôles.\n Veuillez ajouter la permission au rôle \"Activity Roles\". Sinon, le robot quittera automatiquement le serveur.", + "alreadyRunning": "A `/checkroles` request is already running for this guild." }, "Cancelled": "Annulé", "deleteActivityRole": { @@ -118,5 +119,12 @@ }, "deleteActivityRoles": { "deleteAllConfirmation": "Êtes-vous sûr de vouloir supprimer tous les rôles d'activité ?" + }, + "presenceUpdate": { + "roleHigherThanBotRole": "I don’t have permission to manage %s because it is higher than my highest role.\nPlease move one of my roles above all Activity Roles." + }, + "reset": { + "description": "resets the bot for this guild", + "confirmationPrompt": ":warning: You are about to reset the whole bot including all your activity roles. Roles will not be removed from members, and the bot will not remove them even if you create a new Activity Role.\nThis is normally not necessary! Are you _**absolutely**_ sure?" } } diff --git a/locales/nl.json b/locales/nl.json index 8bade1b..f910351 100644 --- a/locales/nl.json +++ b/locales/nl.json @@ -104,7 +104,8 @@ "description": "re-check all users/roles", "success": "checked {{{usersChecked}}} users, added {{{added}}} and removed {{{removed}}} roles", "in-progress": "IN PROGRESS: already checked {{{usersChecked}}}/{{{totalUsersToCheck}}} users, added {{{added}}} and removed {{{removed}}} roles.", - "askForManageRolesPermission": "I don’t have the `MANAGE_ROLES` permission but need it to assign roles.\n Please add the permission to the “Activity Roles” role. Otherwise the bot will leave the guild automatically." + "askForManageRolesPermission": "I don’t have the `MANAGE_ROLES` permission but need it to assign roles.\n Please add the permission to the “Activity Roles” role. Otherwise the bot will leave the guild automatically.", + "alreadyRunning": "A `/checkroles` request is already running for this guild." }, "Cancelled": "Cancelled", "deleteActivityRole": { @@ -118,5 +119,12 @@ }, "deleteActivityRoles": { "deleteAllConfirmation": "Are you sure you want to delete all activity roles?" + }, + "presenceUpdate": { + "roleHigherThanBotRole": "I don’t have permission to manage %s because it is higher than my highest role.\nPlease move one of my roles above all Activity Roles." + }, + "reset": { + "description": "resets the bot for this guild", + "confirmationPrompt": ":warning: You are about to reset the whole bot including all your activity roles. Roles will not be removed from members, and the bot will not remove them even if you create a new Activity Role.\nThis is normally not necessary! Are you _**absolutely**_ sure?" } } diff --git a/locales/pt-BR.json b/locales/pt-BR.json index a2fc046..f964f9d 100644 --- a/locales/pt-BR.json +++ b/locales/pt-BR.json @@ -104,7 +104,8 @@ "description": "re-verificar todos os usuários/cargos", "success": "verificou {{{usersChecked}}} usuários, adicionou {{{added}}} e removeu cargos {{{removed}}}", "in-progress": "EM PROGRESS: já verificou {{{usersChecked}}}/{{{totalUsersToCheck}}} usuários, adicionou {{{added}}} e removeu {{{removed}}} cargos.", - "askForManageRolesPermission": "I don’t have the `MANAGE_ROLES` permission but need it to assign roles.\n Please add the permission to the “Activity Roles” role. Otherwise the bot will leave the guild automatically." + "askForManageRolesPermission": "I don’t have the `MANAGE_ROLES` permission but need it to assign roles.\n Please add the permission to the “Activity Roles” role. Otherwise the bot will leave the guild automatically.", + "alreadyRunning": "A `/checkroles` request is already running for this guild." }, "Cancelled": "Cancelado", "deleteActivityRole": { @@ -118,5 +119,12 @@ }, "deleteActivityRoles": { "deleteAllConfirmation": "Tem certeza que deseja excluir todos os cargos de atividade?" + }, + "presenceUpdate": { + "roleHigherThanBotRole": "I don’t have permission to manage %s because it is higher than my highest role.\nPlease move one of my roles above all Activity Roles." + }, + "reset": { + "description": "resets the bot for this guild", + "confirmationPrompt": ":warning: You are about to reset the whole bot including all your activity roles. Roles will not be removed from members, and the bot will not remove them even if you create a new Activity Role.\nThis is normally not necessary! Are you _**absolutely**_ sure?" } } diff --git a/locales/ru.json b/locales/ru.json index 26f0733..0819c49 100644 --- a/locales/ru.json +++ b/locales/ru.json @@ -104,7 +104,8 @@ "description": "re-check all users/roles", "success": "checked {{{usersChecked}}} users, added {{{added}}} and removed {{{removed}}} roles", "in-progress": "IN PROGRESS: already checked {{{usersChecked}}}/{{{totalUsersToCheck}}} users, added {{{added}}} and removed {{{removed}}} roles.", - "askForManageRolesPermission": "I don’t have the `MANAGE_ROLES` permission but need it to assign roles.\n Please add the permission to the “Activity Roles” role. Otherwise the bot will leave the guild automatically." + "askForManageRolesPermission": "I don’t have the `MANAGE_ROLES` permission but need it to assign roles.\n Please add the permission to the “Activity Roles” role. Otherwise the bot will leave the guild automatically.", + "alreadyRunning": "A `/checkroles` request is already running for this guild." }, "Cancelled": "Cancelled", "deleteActivityRole": { @@ -118,5 +119,12 @@ }, "deleteActivityRoles": { "deleteAllConfirmation": "Are you sure you want to delete all activity roles?" + }, + "presenceUpdate": { + "roleHigherThanBotRole": "I don’t have permission to manage %s because it is higher than my highest role.\nPlease move one of my roles above all Activity Roles." + }, + "reset": { + "description": "resets the bot for this guild", + "confirmationPrompt": ":warning: You are about to reset the whole bot including all your activity roles. Roles will not be removed from members, and the bot will not remove them even if you create a new Activity Role.\nThis is normally not necessary! Are you _**absolutely**_ sure?" } } diff --git a/locales/uk.json b/locales/uk.json index 759bc0a..f41b5c7 100644 --- a/locales/uk.json +++ b/locales/uk.json @@ -104,7 +104,8 @@ "description": "re-check all users/roles", "success": "checked {{{usersChecked}}} users, added {{{added}}} and removed {{{removed}}} roles", "in-progress": "IN PROGRESS: already checked {{{usersChecked}}}/{{{totalUsersToCheck}}} users, added {{{added}}} and removed {{{removed}}} roles.", - "askForManageRolesPermission": "I don’t have the `MANAGE_ROLES` permission but need it to assign roles.\n Please add the permission to the “Activity Roles” role. Otherwise the bot will leave the guild automatically." + "askForManageRolesPermission": "I don’t have the `MANAGE_ROLES` permission but need it to assign roles.\n Please add the permission to the “Activity Roles” role. Otherwise the bot will leave the guild automatically.", + "alreadyRunning": "A `/checkroles` request is already running for this guild." }, "Cancelled": "Cancelled", "deleteActivityRole": { @@ -118,5 +119,12 @@ }, "deleteActivityRoles": { "deleteAllConfirmation": "Are you sure you want to delete all activity roles?" + }, + "presenceUpdate": { + "roleHigherThanBotRole": "I don’t have permission to manage %s because it is higher than my highest role.\nPlease move one of my roles above all Activity Roles." + }, + "reset": { + "description": "resets the bot for this guild", + "confirmationPrompt": ":warning: You are about to reset the whole bot including all your activity roles. Roles will not be removed from members, and the bot will not remove them even if you create a new Activity Role.\nThis is normally not necessary! Are you _**absolutely**_ sure?" } } diff --git a/src/modules/bot.ready.ts b/src/modules/bot.ready.ts index b71b7a5..126ca0c 100644 --- a/src/modules/bot.ready.ts +++ b/src/modules/bot.ready.ts @@ -17,6 +17,7 @@ import _export from './commands/export'; import help from './commands/help'; import listRoles from './commands/listRoles'; import requireRole from './commands/requireRole'; +import reset from './commands/reset'; import setStatusRole from './commands/setStatusRole'; import stats from './commands/stats'; import toggleAutoRole from './commands/toggleAutoRole'; @@ -35,6 +36,7 @@ export function initClientReady() { .addCommand(help) .addCommand(listRoles) .addCommand(requireRole) + .addCommand(reset) .addCommand(setStatusRole) .addCommand(stats) .addCommand(toggleAutoRole); diff --git a/src/modules/commands/reset.ts b/src/modules/commands/reset.ts new file mode 100644 index 0000000..aa711f1 --- /dev/null +++ b/src/modules/commands/reset.ts @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +import { + ActionRowBuilder, + ButtonBuilder, + ButtonInteraction, + ButtonStyle, + ComponentType, + PermissionsBitField, + SlashCommandBuilder, +} from 'discord.js'; + +import { Command } from '../commandHandler'; +import { __, discordTranslations, log } from '../messages'; +import { db, getLang } from '../db'; + +export default { + data: new SlashCommandBuilder() + .setName('reset') + .setDescription(__({ phrase: 'reset->description', locale: 'en-US' })) + .setDescriptionLocalizations(discordTranslations('reset->description')) + .setDefaultMemberPermissions(PermissionsBitField.Flags.ManageRoles), + + execute: async interaction => { + const locale = getLang(interaction); + await interaction.reply({ + content: __({ phrase: 'reset->confirmationPrompt', locale }), + components: [ + new ActionRowBuilder().addComponents( + new ButtonBuilder() + .setCustomId('reset:confirm') + .setLabel(__({ phrase: 'Yes', locale })) + .setStyle(ButtonStyle.Danger), + new ButtonBuilder() + .setCustomId('reset:cancel') + .setLabel(__({ phrase: 'No', locale })) + .setStyle(ButtonStyle.Secondary), + ), + ], + ephemeral: true, + }); + + interaction.channel + ?.createMessageComponentCollector({ + componentType: ComponentType.Button, + filter: (btnInt: ButtonInteraction) => interaction.user.id === btnInt.user.id, + max: 1, + time: 3 * 60 * 1000, + }) + .on('collect', async (int: ButtonInteraction) => { + switch (int.customId) { + case 'reset:confirm': + await int.deferUpdate(); + await db + .deleteFrom('activityRoles') + .where('guildID', '=', interaction.guildId) + .execute(); + await db + .deleteFrom('activityStats') + .where('guildID', '=', interaction.guildId) + .execute(); + await db.deleteFrom('guilds').where('guildID', '=', interaction.guildId).execute(); + await db.deleteFrom('statusRoles').where('guildID', '=', interaction.guildId).execute(); + await db + .deleteFrom('activeTemporaryRoles') + .where('guildID', '=', interaction.guildId) + .execute(); + await db + .deleteFrom('activeTemporaryRolesHashed') + .where('guildID', '=', interaction.guildId) + .execute(); + await int.editReply({ + content: __({ phrase: 'Success!', locale }), + components: [], + }); + break; + case 'reset:cancel': + int.update({ + content: __({ phrase: 'Cancelled', locale }), + components: [], + }); + break; + } + }); + }, +} as Command;