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.
Merge pull request #95 from TeamSekai/feature/date-and-calculation
- Loading branch information
Showing
23 changed files
with
535 additions
and
135 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,124 @@ | ||
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<Option<unknown, boolean>[]>, | ||
) => void; | ||
|
||
constructor( | ||
name: string, | ||
description: string, | ||
handle: SlashCommandSubcommandBuilder, | ||
options: Options, | ||
onBuild: ( | ||
name: string, | ||
subcommand: SimpleCommand<Option<unknown, boolean>[]>, | ||
) => void, | ||
) { | ||
super(name, description, handle, options); | ||
this.#onBuild = onBuild; | ||
} | ||
|
||
protected override newInstance<O extends Option<unknown, boolean>[]>( | ||
name: string, | ||
description: string, | ||
handle: SlashCommandSubcommandBuilder, | ||
options: O, | ||
): SimpleSlashCommandBuilder<O> { | ||
return new SimpleSubcommandBuilder( | ||
name, | ||
description, | ||
handle, | ||
options, | ||
this.#onBuild, | ||
); | ||
} | ||
|
||
override build( | ||
action: ( | ||
interaction: ChatInputCommandInteraction<CacheType>, | ||
...options: OptionValueMap<Options> | ||
) => Promise<void>, | ||
): SimpleCommand<Options> { | ||
const subcommand = super.build(action); | ||
this.#onBuild(this.name, subcommand as any); | ||
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
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
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
Oops, something went wrong.