From aa0e2a9a3a07b24361fa6a06153ddfd54f6a826f Mon Sep 17 00:00:00 2001 From: takejohn <105504345+takejohn@users.noreply.github.com> Date: Wed, 8 May 2024 20:59:13 +0900 Subject: [PATCH] =?UTF-8?q?/perm=20remove=E3=82=B5=E3=83=96=E3=82=B3?= =?UTF-8?q?=E3=83=9E=E3=83=B3=E3=83=89=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/perms/PermissionManager.ts | 17 +++++- packages/perms/commands/perm.ts | 93 ++++++++++++++++++++++------- 2 files changed, 87 insertions(+), 23 deletions(-) diff --git a/packages/perms/PermissionManager.ts b/packages/perms/PermissionManager.ts index d025b6c..18ca352 100644 --- a/packages/perms/PermissionManager.ts +++ b/packages/perms/PermissionManager.ts @@ -55,7 +55,14 @@ export class PermissionManager { guild.roles.resolve(group) ?? this.#client.users.resolve(group); const mentions = mentionable != null ? [mentionable] : []; - return new Permission(this.#client, guild, name, mentions); + return new Permission(this.#client, guild, name, mentions, this); + } + + async remove(guild: Guild, name: string): Promise { + const client = this.#client.application.id; + const connection = db.connection; + const collection = connection.collection('perms'); + await collection.deleteOne({ client, guild: guild.id, name }); } } @@ -68,16 +75,20 @@ export class Permission { readonly #mentions: Mentionable[]; + readonly manager: PermissionManager; + constructor( client: Client, guild: Guild, name: string, mentions: Mentionable[], + manager: PermissionManager, ) { this.client = client; this.guild = guild; this.name = name; this.#mentions = mentions; + this.manager = manager; } hasMember(member: GuildMember): boolean { @@ -96,6 +107,10 @@ export class Permission { return false; } + async remove(): Promise { + await this.manager.remove(this.guild, this.name); + } + toString() { return this.#mentions.join(', '); } diff --git a/packages/perms/commands/perm.ts b/packages/perms/commands/perm.ts index 0d1c265..c3cd665 100644 --- a/packages/perms/commands/perm.ts +++ b/packages/perms/commands/perm.ts @@ -2,9 +2,10 @@ import { CompoundCommandBuilder } from 'core'; import { feature as db } from 'db'; import { ApplicationCommandOptionChoiceData, + ChatInputCommandInteraction, PermissionFlagsBits, } from 'discord.js'; -import { PermissionManager } from '../PermissionManager'; +import { Permission, PermissionManager } from '../PermissionManager'; const builder = new CompoundCommandBuilder('perm', '権限の設定'); @@ -14,6 +15,41 @@ export function addChoice(choice: ApplicationCommandOptionChoiceData) { choices.push(choice); } +async function informNotInGuild(interaction: ChatInputCommandInteraction) { + await interaction.reply({ + content: 'このコマンドはサーバー内で使用してください!', + ephemeral: true, + }); + return; +} + +async function checkMemberIsAdministrator( + interaction: ChatInputCommandInteraction<'cached'>, +): Promise { + if (!interaction.member.permissions.has(PermissionFlagsBits.Administrator)) { + await interaction.reply({ + content: '権限がありません!', + ephemeral: true, + }); + return false; + } + return true; +} + +async function getPermissionOrInformNotFound( + interaction: ChatInputCommandInteraction<'cached'>, + permissionName: string, +): Promise { + const permissions = PermissionManager.forClient(interaction.client); + const result = await permissions.get(interaction.guild, permissionName); + if (result == null) { + await interaction.reply( + `権限名: ${permissionName}\nその名前の権限はありません!`, + ); + } + return result; +} + builder .subcommand('set', '値の更新') .addStringOption({ @@ -32,19 +68,10 @@ builder .build(async (interaction, permissionName, group) => { const connection = db.connection; if (!interaction.inCachedGuild()) { - await interaction.reply({ - content: 'このコマンドはサーバー内で使用してください!', - ephemeral: true, - }); + await informNotInGuild(interaction); return; } - if ( - !interaction.member.permissions.has(PermissionFlagsBits.Administrator) - ) { - await interaction.reply({ - content: '権限がありません!', - ephemeral: true, - }); + if (!(await checkMemberIsAdministrator(interaction))) { return; } const permissions = PermissionManager.forClient(interaction.client); @@ -67,21 +94,43 @@ builder .build(async (interaction, permissionName) => { const connection = db.connection; if (!interaction.inCachedGuild()) { - await interaction.reply({ - content: 'このコマンドはサーバー内で使用してください!', - ephemeral: true, - }); + await informNotInGuild(interaction); return; } - const permissions = PermissionManager.forClient(interaction.client); - const result = await permissions.get(interaction.guild, permissionName); - if (result != null) { + const permission = await getPermissionOrInformNotFound( + interaction, + permissionName, + ); + if (permission != null) { await interaction.reply( - `権限名: ${permissionName}\nロール/メンバー: ${result}`, + `権限名: ${permissionName}\nロール/メンバー: ${permission}`, ); - } else { + } + }); + +builder + .subcommand('remove', '値の削除') + .addStringOption({ + name: 'permission', + description: '権限名', + required: true, + async autocomplete(interaction) { + await interaction.respond(choices); + }, + }) + .build(async (interaction, permissionName) => { + if (!interaction.inCachedGuild()) { + await informNotInGuild(interaction); + return; + } + const permission = await getPermissionOrInformNotFound( + interaction, + permissionName, + ); + if (permission != null) { + await permission.remove(); await interaction.reply( - `権限名: ${permissionName}\nその名前の権限はありません!`, + `権限を削除しました\n権限名: ${permissionName}\nロール/メンバー: ${permission}`, ); } });