Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
/perm removeサブコマンド作成
Browse files Browse the repository at this point in the history
  • Loading branch information
takejohn committed May 8, 2024
1 parent 97e150d commit aa0e2a9
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 23 deletions.
17 changes: 16 additions & 1 deletion packages/perms/PermissionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const client = this.#client.application.id;
const connection = db.connection;
const collection = connection.collection<PermSchema>('perms');
await collection.deleteOne({ client, guild: guild.id, name });
}
}

Expand All @@ -68,16 +75,20 @@ export class Permission {

readonly #mentions: Mentionable[];

readonly manager: PermissionManager;

constructor(
client: Client<true>,
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 {
Expand All @@ -96,6 +107,10 @@ export class Permission {
return false;
}

async remove(): Promise<void> {
await this.manager.remove(this.guild, this.name);
}

toString() {
return this.#mentions.join(', ');
}
Expand Down
93 changes: 71 additions & 22 deletions packages/perms/commands/perm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', '権限の設定');

Expand All @@ -14,6 +15,41 @@ export function addChoice(choice: ApplicationCommandOptionChoiceData<string>) {
choices.push(choice);
}

async function informNotInGuild(interaction: ChatInputCommandInteraction) {
await interaction.reply({
content: 'このコマンドはサーバー内で使用してください!',
ephemeral: true,
});
return;
}

async function checkMemberIsAdministrator(
interaction: ChatInputCommandInteraction<'cached'>,
): Promise<boolean> {
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<Permission | null> {
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({
Expand All @@ -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);
Expand All @@ -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}`,
);
}
});
Expand Down

0 comments on commit aa0e2a9

Please sign in to comment.