javascript -discord.js 斜杠命令生成器未注册命令或在不和谐中显示命令

问题描述 投票:0回答:1

我已经有另一个斜杠命令,不使用有效的斜杠命令生成器,所以我知道所有授权范围都很好。当我尝试使用斜杠命令生成器注册命令时,我没有收到错误,但它不会显示在不和谐上。 代码: index.js

const { Client, IntentsBitField, PermissionsBitField, Embed, ButtonBuilder, ButtonStyle} = require('discord.js');
const { EmbedBuilder } = require('discord.js');
const { ActionRowBuilder, Events, Collection, ModalBuilder, TextInputBuilder, TextInputStyle } = require('discord.js');
const fs = require('node:fs');
const path = require('node:path');




const client = new Client({
    intents: [
        IntentsBitField.Flags.Guilds,
        IntentsBitField.Flags.GuildMembers,
        IntentsBitField.Flags.GuildMessages,
        IntentsBitField.Flags.MessageContent,
    ],
});

client.commands = new Collection();

const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
    const commandsPath = path.join(foldersPath, folder);
    const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
    for (const file of commandFiles) {
        const filePath = path.join(commandsPath, file);
        const command = require(filePath);
        // Set a new item in the Collection with the key as the command name and the value as the exported module
        if ('data' in command && 'execute' in command) {
            client.commands.set(command.data.name, command);
        } else {
            console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
        }
    }
}

client.on(Events.InteractionCreate, interaction => {
    console.log(interaction);
});

client.on(Events.InteractionCreate, interaction => {
    if (!interaction.isChatInputCommand()) return;
    console.log(interaction);
});

client.on(Events.InteractionCreate, async interaction => {
    if (!interaction.isChatInputCommand()) return;

    const command = interaction.client.commands.get(interaction.commandName);

    if (!command) {
        console.error(`No command matching ${interaction.commandName} was found.`);
        return;
    }

    try {
        await command.execute(interaction);
    } catch (error) {
        console.error(error);
        if (interaction.replied || interaction.deferred) {
            await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
        } else {
            await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
        }
    }
});
//Code cut off

ticket-setup.js(设置斜杠命令生成器)

const { SlashCommandBuilder, EmbedBuilder, ButtonBuilder, ButtonStyle, ActionRowBuilder, PermissionsBitField, PermissionFlagsBits} = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('ticket-setup')
        .setDescription('Open a Ticket')
        .setDMPermission(false),
    async execute(interaction) {

        const embed = new EmbedBuilder()
            .setTitle('Contact Moderation')
            .setDescription('Create a ticket and contact the moderators with the button below.')
            .setColor('Blue');
        const ticketBtn = new ButtonBuilder()
            .setCustomId('ticketBtn')
            .setLabel('Open Ticket')
            .setEmoji('📤')
            .setStyle(ButtonStyle.Secondary);

         const row = new ActionRowBuilder()
            .addComponents(ticketBtn)

         await interaction.reply({ embeds: [embed], components: [row] });
    }
    
}

我查看了所有文档但无法找到答案。尝试在没有构建器的情况下注册斜杠命令效果很好,但我无法用这个来做到这一点。

编辑 附加文件目录图像以更好地理解代码: directory image

javascript discord discord.js
1个回答
0
投票

您将命令注册为

Application commands
,由于 Discord API 限制,更新可能需要长达一个小时(或更长时间,不确定确切的时间范围)。 但是,
Guild commands
会立即针对指定公会进行更新。 在不更改大部分代码的情况下,您可以通过以下方式将命令注册为
Guild commands

await client.guilds.cache.get(YOUR GUILD ID).commands.set(command.data.name, command);

但根据 Discord 的说法,这并不是首选方式。他们写了自己的教程这里

© www.soinside.com 2019 - 2024. All rights reserved.