Sync Slash Commands with one Guild

Hello, I have a test server which i use to test my bots, I am new to sapphire, and by reading the docs, I couldn't find a way to make the app sync the commands to a single guild/server. I do not want to sync with all servers as I noticed that is slow and takes a few minutes, whereas when i sync with one server (my server) it's almost immediate. Can you do that in sapphire? Please help
5 Replies
b1nzee
b1nzee15mo ago
You can specify an array of guildIds in the command registry
public override registerApplicationCommands(
registry: ApplicationCommandRegistry
) {
registry.registerChatInputCommand(
(command) => {
command.setName(this.name).setDescription(this.description);
},
{
idHints: [],
guildIds: ["ID_HERE"],
}
);
}
public override registerApplicationCommands(
registry: ApplicationCommandRegistry
) {
registry.registerChatInputCommand(
(command) => {
command.setName(this.name).setDescription(this.description);
},
{
idHints: [],
guildIds: ["ID_HERE"],
}
);
}
Disuqi
Disuqi15mo ago
So i have to do it for every command? Is there a way to apply it globally @b1nzee
Favna
Favna15mo ago
There isn't but you can just define a constant somewhere and import that in your commands. Or alternatively you can leverage class extension and supplement it that way. Never forget you got all the power of modules, object oriented programming and class inheritance available still.
chillihero
chillihero15mo ago
public override registerApplicationCommands(
registry: ApplicationCommandRegistry
) {
const commandGuilds = process.env.NODE_ENV !== 'production' ?["ID_HERE"] : []; registry.registerChatInputCommand(
(command) => {
command.setName(this.name).setDescription(this.description);
},
{
idHints: [],
guildIds: commandGuilds,
}
);
}
public override registerApplicationCommands(
registry: ApplicationCommandRegistry
) {
const commandGuilds = process.env.NODE_ENV !== 'production' ?["ID_HERE"] : []; registry.registerChatInputCommand(
(command) => {
command.setName(this.name).setDescription(this.description);
},
{
idHints: [],
guildIds: commandGuilds,
}
);
}
Would solve the problem of changing guildCommands for prod & dev environment. (if you use two different ones) (not sure if i read that requirement from your message correctly) nvm it was not
Favna
Favna15mo ago
This is how I tackle that for @Dragonite. Like I keep saying to so many people, never forget you have modules and you can split your code across files.
// src/lib/util/utils.ts
import { envParseArray } from '@skyra/env-utilities';

export function getGuildIds(): string[] {
return envParseArray('COMMAND_GUILD_IDS', []);
}
// src/lib/util/utils.ts
import { envParseArray } from '@skyra/env-utilities';

export function getGuildIds(): string[] {
return envParseArray('COMMAND_GUILD_IDS', []);
}
// src/lib/extensions/DragoniteCommand.ts
import { Command } from '@sapphire/framework';
import { PermissionFlagsBits, PermissionsBitField } from 'discord.js';

export abstract class DragoniteCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
const resolvedPermissions = new PermissionsBitField(options.requiredClientPermissions).add(PermissionFlagsBits.EmbedLinks);

super(context, {
requiredClientPermissions: resolvedPermissions,
...options
});
}
}
// src/lib/extensions/DragoniteCommand.ts
import { Command } from '@sapphire/framework';
import { PermissionFlagsBits, PermissionsBitField } from 'discord.js';

export abstract class DragoniteCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
const resolvedPermissions = new PermissionsBitField(options.requiredClientPermissions).add(PermissionFlagsBits.EmbedLinks);

super(context, {
requiredClientPermissions: resolvedPermissions,
...options
});
}
}
DragoniteCommand exists for the sole purpose of assigning default required permissions to all commands.
// src/commands/Pokemon/*.ts
import { DragoniteCommand } from '#lib/extensions/DragoniteCommand';
import { getGuildIds } from '#utils/utils';
import { ApplyOptions } from '@sapphire/decorators';
import type { ChatInputCommand } from '@sapphire/framework';

@ApplyOptions<ChatInputCommand.Options>({
description: 'Gets PokéDex entries for the chosen Pokémon.'
})
export class SlashCommand extends DragoniteCommand {
public override registerApplicationCommands(registry: ChatInputCommand.Registry) {
registry.registerChatInputCommand(
(builder) =>
builder //
.setName(this.name)
.setDescription(this.description),
{ guildIds: getGuildIds() }
);
}
// src/commands/Pokemon/*.ts
import { DragoniteCommand } from '#lib/extensions/DragoniteCommand';
import { getGuildIds } from '#utils/utils';
import { ApplyOptions } from '@sapphire/decorators';
import type { ChatInputCommand } from '@sapphire/framework';

@ApplyOptions<ChatInputCommand.Options>({
description: 'Gets PokéDex entries for the chosen Pokémon.'
})
export class SlashCommand extends DragoniteCommand {
public override registerApplicationCommands(registry: ChatInputCommand.Registry) {
registry.registerChatInputCommand(
(builder) =>
builder //
.setName(this.name)
.setDescription(this.description),
{ guildIds: getGuildIds() }
);
}
Want results from more Discord servers?
Add your server