Pass data from Precondition to ChatInputCommand

Hi all. I am using Sapphire to handle my commands, and I have a precondition that checks if a guild has config related to it in the DB, and creates it if not, and I was wondering if there was any way for me to hold that data in the interaction/context. I tried to attach it to context
public override async chatInputRun(
interaction: CommandInteraction,
_command: ChatInputCommand,
__context: PreconditionContext__,)
public override async chatInputRun(
interaction: CommandInteraction,
_command: ChatInputCommand,
__context: PreconditionContext__,)
and then retrieve it in the
public async ticketsCategory(interaction: ChatInputCommand.Interaction, context: ChatInputCommand.RunContext)
public async ticketsCategory(interaction: ChatInputCommand.Interaction, context: ChatInputCommand.RunContext)
(I am using subcommands in a group, hence it's not called that. It does execute right). Logging the context returns
{
commandId: "1404874117412294828",
commandName: "settings",
}
{
commandId: "1404874117412294828",
commandName: "settings",
}
not sure if it's possible to do what I wanted. Thanks
Solution:
there is no built in way to pass data, but you can use anything that's in JavaScript by default such as using a keyed map, object, array, database, etc
Jump to solution
2 Replies
The Untraceable
The UntraceableOP2w ago
// preconditions/EnsureGuildConfig.ts
export class EnsureGuildConfigPrecondition extends Precondition {
public override async chatInputRun(
interaction: CommandInteraction,
_command: ChatInputCommand,
context: PreconditionContext,
) {

if (!interaction.guildId || !interaction.guild) {
this.container.logger.error("This command can only be used in a guild.");
return this.error({
message: "This command can only be used in a guild.",
});
}

this.container.logger.debug("Fetching guild data...");
const guildData = await this.container.db
.collection<GuildSettings>("settings")
.findOneAndUpdate(
{ guildId: interaction.guildId },
{ $setOnInsert: createGuildConfig(interaction.guild) },
{ upsert: true, returnDocument: "after" },
);

this.container.logger.debug("Guild data fetched:", guildData);
context.settings = guildData;

return this.ok();
}
}
// preconditions/EnsureGuildConfig.ts
export class EnsureGuildConfigPrecondition extends Precondition {
public override async chatInputRun(
interaction: CommandInteraction,
_command: ChatInputCommand,
context: PreconditionContext,
) {

if (!interaction.guildId || !interaction.guild) {
this.container.logger.error("This command can only be used in a guild.");
return this.error({
message: "This command can only be used in a guild.",
});
}

this.container.logger.debug("Fetching guild data...");
const guildData = await this.container.db
.collection<GuildSettings>("settings")
.findOneAndUpdate(
{ guildId: interaction.guildId },
{ $setOnInsert: createGuildConfig(interaction.guild) },
{ upsert: true, returnDocument: "after" },
);

this.container.logger.debug("Guild data fetched:", guildData);
context.settings = guildData;

return this.ok();
}
}
That's the precondition's code
Solution
Favna
Favna2w ago
there is no built in way to pass data, but you can use anything that's in JavaScript by default such as using a keyed map, object, array, database, etc

Did you find this page helpful?