Hey all, I’m not really sure if this is a “Sapphire support” type of question more than a TypeScript one (I’m new to TS in general)
But is there any way to invoke a commands messageRun or the designated method from say another command class (something like Java reflection/does sapphire have a command handler to grab the appropriate method?) or is the best way to extract the code to a new file then just call that from both commands?
Basically I have a command which is:
$account link <region> <account name>
But I also have another command which is:
$verify <region> <account name> or $verify <account name>
Is there any way for me to just have the verify commands call the: $account link chat input method
Or is the best way to just have like a Verification.ts and have the command logic in there and call that method from every command.
Honestly just trying to minimize ts files if possible
Solution
You can leverage
this.store
this.store
, just make sure you pas all the right arguments:
import { Args, Command, type MessageCommand } from '@sapphire/framework';import type { Message } from 'discord.js';export class MyCommand extends Command { public constructor(context: Command.Context) { super(context, { description: 'blub blub' }); } public override async messageRun(message: Message, args: Args, context: MessageCommand.RunContext) { const otherCommandMessageRun = this.store.get('other-command')?.messageRun; if (otherCommandMessageRun) otherCommandMessageRun(message, args, context); }}
import { Args, Command, type MessageCommand } from '@sapphire/framework';import type { Message } from 'discord.js';export class MyCommand extends Command { public constructor(context: Command.Context) { super(context, { description: 'blub blub' }); } public override async messageRun(message: Message, args: Args, context: MessageCommand.RunContext) { const otherCommandMessageRun = this.store.get('other-command')?.messageRun; if (otherCommandMessageRun) otherCommandMessageRun(message, args, context); }}