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

Commit

Permalink
permコマンドを作成
Browse files Browse the repository at this point in the history
  • Loading branch information
takejohn committed May 5, 2024
1 parent 4f4c485 commit 09362c0
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 1 deletion.
57 changes: 57 additions & 0 deletions core/common/SimpleCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import {
APIApplicationCommandOptionChoice,
ApplicationCommandOptionWithChoicesAndAutocompleteMixin,
CacheType,
GuildMember,
Role,
SharedSlashCommandOptions,
SlashCommandSubcommandBuilder,
User,
} from 'discord.js';
import { ChatInputCommandInteraction } from 'discord.js';
import { Command } from '../util/types';
Expand Down Expand Up @@ -50,6 +53,11 @@ interface SimpleStringOptionData<
min_length?: number;
}

type Mentionable = GuildMember | Role | User;

type SimpleMentionableOptionData<Required extends boolean = boolean> =
SimpleCommandOptionData<Mentionable, Required>;

export interface Option<T = unknown, Required extends boolean = boolean> {
/** オプションの名前 */
name: string;
Expand Down Expand Up @@ -157,6 +165,49 @@ class StringOption<
}
}

class MentionableOption<Required extends boolean = boolean>
implements Option<Mentionable, Required>
{
name: string;

required: Required;

constructor(
builder: SharedSlashCommandOptions,
input: SimpleMentionableOptionData<Required>,
) {
const name = input.name;
const description = input.description;
const required = input.required;
this.name = name;
this.required = required;
try {
builder.addMentionableOption((input) => {
return input
.setName(name)
.setDescription(description)
.setRequired(required);
});
} catch (e) {
console.error(e);
}
}

get(
interaction: ChatInputCommandInteraction<CacheType>,
): Value<Mentionable, Required> {
return this.required
? (interaction.options.getMentionable(this.name, true) as Value<
Mentionable,
Required
>)
: (interaction.options.getMentionable(this.name) as Value<
Mentionable,
Required
>);
}
}

/**
* シンプルな SlashCommandBuilder(?)
*/
Expand Down Expand Up @@ -231,6 +282,12 @@ export class SimpleSlashCommandBuilder<
return this.addOption(new StringOption(this.handle, input));
}

addMentionableOption<Required extends boolean = boolean>(
input: SimpleMentionableOptionData<Required>,
) {
return this.addOption(new MentionableOption(this.handle, input));
}

build(
action: (
interaction: ChatInputCommandInteraction,
Expand Down
78 changes: 78 additions & 0 deletions packages/perms/commands/perm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { CompoundCommandBuilder } from 'core';
import { feature as db } from 'db';

interface PermSchema {
guild: string;
name: string;
group: string;
}

const builder = new CompoundCommandBuilder('perm', '権限の設定');

builder
.subcommand('set', '値の更新')
.addStringOption({
name: 'permission',
description: '権限名',
required: true,
})
.addMentionableOption({
name: 'group',
description: '対象のロールまたはユーザー',
required: true,
})
.build(async (interaction, permissionName, group) => {
const connection = db.connection;
const guild = interaction.guild;
if (guild == null) {
interaction.reply({
content: 'このコマンドはサーバー内で使用してください!',
ephemeral: true,
});
return;
}
const collection = connection.collection<PermSchema>('perms');
collection.insertOne({
guild: guild.id,
name: permissionName,
group: group.id,
});
await interaction.reply(
`権限を追加しました!\n権限名: ${permissionName}\nロール/メンバーID: ${group.id}`,
);
});

builder
.subcommand('get', '値の取得')
.addStringOption({
name: 'permission',
description: '権限名',
required: true,
})
.build(async (interaction, permissionName) => {
const connection = db.connection;
const guild = interaction.guild;
if (guild == null) {
interaction.reply({
content: 'このコマンドはサーバー内で使用してください!',
ephemeral: true,
});
return;
}
const collection = connection.collection<PermSchema>('perms');
const result = await collection.findOne({
guild: guild.id,
name: permissionName,
});
if (result != null) {
await interaction.reply(
`権限名: ${permissionName}\nロール/メンバーID: ${result.group}`,
);
} else {
await interaction.reply(
`権限名: ${permissionName}\nその名前の権限はありません!`,
);
}
});

export default builder.build();
11 changes: 10 additions & 1 deletion packages/perms/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { Feature } from 'core';
import { CommandManager, Feature } from 'core';
import { Client } from 'discord.js';
import perm from './commands/perm';
import { feature as db } from 'db';

class PermsFeature extends Feature {
enabled: boolean = true;

name: string = 'perms';

dependencies: Feature[] = [db];

onLoad(client: Client<boolean>): void | PromiseLike<void> {
CommandManager.default.addCommands([perm]);
}
}

export const feature = new PermsFeature();

0 comments on commit 09362c0

Please sign in to comment.