Sapphire - Imagine a framework
Sapphire - Imagine a framework

sapphire-support

Root Question Message

Chillihero
Chillihero1/14/2023
Best Approach on separating Commands from their "run" file to a separate Folder

For context, I am super new to discordjs and this framework so apologizes if this question sounds odd.
I really dislike the approach that all examples in the example repository and docs are giving of having the command definition (if you call it like this) and the execution code of the command in the same file.
What is the best way to separate them both? Or is it even possible?

Solution Message

Favna
Favna1/14/2023
import { myRunFunction } from '../lib/commands/my-command';
import { Command } from '@sapphire/framework';

export class MyCommand extends Command {
  public override async chatInputRun(interaction: Command.ChatInputInteraction) {
    return myRunFunction(interaction);
  }
}

Other than that, it's not possible. Sapphire aims to be Object Oriented which means that code is encapsulated and highly coupled to the origin object, the command class. It sounds like you're rather looking for something following a more FP style which Sapphire doesn't offer.

In fact I'd argue that modern JS in general, while allowing FP, isn't the best language for actual FP because there is a large focus on classes.
Chillihero
Chillihero1/14/2023
I see, what does FP stand for?
Favna
Favna1/14/2023
functional programming
Favna
Favna1/14/2023
it's the total opposite of object oriented
Chillihero
Chillihero1/14/2023
ahh yes, I see.
Chillihero
Chillihero1/14/2023
Thank you
Favna
Favna1/14/2023
OOP combines data and its associated behavior into an object. This helps software programmers to easier understand how the program works, although the code is much longer than in FP. FP, on the other hand, clearly distinguishes data and behavior, keeping them separately.
Favna
Favna1/14/2023
that pretty much describes what you were asking
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy