Invoking Command method from other command

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, just make sure you pas all the right arguments: ```ts import { Args, Command, type MessageCommand } from '@sapphire/framework'; import type { Message } from 'discord.js';...
Jump to solution
5 Replies
Solution
Favna
Favna7mo ago
You can leverage 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);
}
}
Baylem
Baylem7mo ago
Thanks, this is perfectly what I need
Baylem
Baylem7mo ago
Okay, well one further question, can this be done with subcommands?
Favna
Favna7mo ago
yes - but 1. If you want the full flow including parsing and preconditions you'll have to make sure the args have all the proper data of subcommand group/name etc 2. If you just want to call your own subcommand method you'll have to do some type casting because TS won't know what methods are on the class, then pass the parameters
Baylem
Baylem7mo ago
Ah okay