This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
120 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { | ||
CacheType, | ||
ChatInputCommandInteraction, | ||
Client, | ||
SlashCommandBuilder, | ||
SlashCommandSubcommandBuilder, | ||
} from 'discord.js'; | ||
import { | ||
Option, | ||
OptionValueMap, | ||
SimpleCommand, | ||
SimpleSlashCommandBuilder, | ||
} from './SimpleCommand'; | ||
import { Command } from '../util/types'; | ||
|
||
export class CompoundCommandBuilder { | ||
readonly #handle: SlashCommandBuilder; | ||
|
||
readonly #subcommands = new Map< | ||
string, | ||
SimpleCommand<Option<unknown, boolean>[]> | ||
>(); | ||
|
||
constructor(name: string, description: string) { | ||
this.#handle = new SlashCommandBuilder() | ||
.setName(name) | ||
.setDescription(description); | ||
} | ||
|
||
subcommand(name: string, description: string): SimpleSubcommandBuilder { | ||
const handle = new SlashCommandSubcommandBuilder(); | ||
this.#handle.addSubcommand(handle); | ||
return new SimpleSubcommandBuilder( | ||
name, | ||
description, | ||
handle, | ||
[], | ||
(name, subcommand) => this.#subcommands.set(name, subcommand), | ||
); | ||
} | ||
|
||
build(): CompoundCommand { | ||
return new CompoundCommand(this.#subcommands, this.#handle); | ||
} | ||
} | ||
|
||
export class SimpleSubcommandBuilder< | ||
Options extends Option<unknown, boolean>[] = [], | ||
> extends SimpleSlashCommandBuilder<Options> { | ||
readonly #onBuild: (name: string, subcommand: SimpleCommand<Options>) => void; | ||
|
||
constructor( | ||
name: string, | ||
description: string, | ||
handle: SlashCommandSubcommandBuilder, | ||
options: Options, | ||
onBuild: (name: string, subcommand: SimpleCommand<Options>) => void, | ||
) { | ||
super(name, description, handle, options); | ||
this.#onBuild = onBuild; | ||
} | ||
|
||
build( | ||
action: ( | ||
interaction: ChatInputCommandInteraction<CacheType>, | ||
...options: OptionValueMap<Options> | ||
) => Promise<void>, | ||
): SimpleCommand<Options> { | ||
const subcommand = super.build(action); | ||
this.#onBuild(this.name, subcommand); | ||
return subcommand; | ||
} | ||
} | ||
|
||
export class CompoundCommand implements Command { | ||
readonly #subcommands: Map<string, SimpleCommand<Option<unknown, boolean>[]>>; | ||
|
||
data: SlashCommandBuilder; | ||
|
||
constructor( | ||
subcommands: Map<string, SimpleCommand<Option<unknown, boolean>[]>>, | ||
data: SlashCommandBuilder, | ||
) { | ||
this.#subcommands = subcommands; | ||
this.data = data; | ||
} | ||
|
||
async execute( | ||
interaction: ChatInputCommandInteraction<CacheType>, | ||
client: Client<true>, | ||
): Promise<void> { | ||
const subcommandName = interaction.options.getSubcommand(true); | ||
const subcommand = this.#subcommands.get(subcommandName); | ||
if (subcommand == null) { | ||
await interaction.reply({ | ||
ephemeral: true, | ||
content: 'Invalid subcommand: ' + subcommandName, | ||
}); | ||
return; | ||
} | ||
subcommand.execute(interaction); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { SimpleSlashCommandBuilder } from '../../../common/SimpleCommand'; | ||
import { CompoundCommandBuilder } from '../../../common/CompoundCommand'; | ||
|
||
export default SimpleSlashCommandBuilder.create('date', '時刻を表示').build( | ||
async (i) => { | ||
await i.reply(new Date().toString()); | ||
}, | ||
); | ||
const builder = new CompoundCommandBuilder('date', '時刻の計算と表示'); | ||
|
||
builder.subcommand('now', '現在時刻を表示').build(async (i) => { | ||
await i.reply(new Date().toString()); | ||
}); | ||
|
||
export default builder.build(); |