我无法发送带有斜杠命令的嵌入 (discord.js)

I can't send an embed with a slash command (discord.js)

所以,每当我尝试在 discord.js 上发送带有斜杠命令的嵌入时,都会抛出错误。 这是我要发送的“help.js”文件。

const { SlashCommandBuilder } = require('@discordjs/builders');

const { MessageEmbed } = require('discord.js');

const helpEmbed = {
  "type": "rich",
  "title": `Need Help?`,
  "description": `Here you go.`,
  "color": 0x00fff0
}

module.exports = {
    data: new SlashCommandBuilder()
        .setName('help')
        .setDescription('Prints a Help Message'),
    async execute(interaction) {
        await channel.send({embeds: [helpEmbed]});
    },
};

错误:

ReferenceError: channel is not defined
    at Object.execute (C:\Users\user\Desktop\my-bot\bot-file\commands\help.js:37:3)
    at Client.<anonymous> (C:\Users\user\Desktop\my-bot\bot-file\index.js:31:17)
    at Client.emit (node:events:527:28)
    at InteractionCreateAction.handle (C:\Users\user\Desktop\my-bot\bot-file\node_modules\discord.js\src\client\actions\InteractionCreate.js:83:12)
    at Object.module.exports [as INTERACTION_CREATE] (C:\Users\user\Desktop\my-bot\bot-file\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
    at WebSocketManager.handlePacket (C:\Users\user\Desktop\my-bot\bot-file\node_modules\discord.js\src\client\websocket\WebSocketManager.js:351:31)
    at WebSocketShard.onPacket (C:\Users\user\Desktop\my-bot\bot-file\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\user\Desktop\my-bot\bot-file\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (C:\Users\user\Desktop\my-bot\bot-file\node_modules\ws\lib\event-target.js:199:18)
at WebSocket.emit (node:events:527:28)

您收到此错误是因为变量 channel 在您使用之前尚未定义。相反,如果您想将帮助嵌入发送到用户使用斜线命令的频道,或者您可以选择使用 ID 或频道名称获取频道,然后发送它,您可以使用 interaction.channel.send()

第一个选项:(如果你想将嵌入发送到用户使用斜杠命令的频道)

async execute(interaction) {
    await interaction.channel.send({embeds: [helpEmbed]});
}

第二个选项:(如果您想通过频道 ID 或名称查找频道)

async execute(interaction) {
    const channel = interaction.guild.channels.cache.get('channelid')
    // Or
    const channel = interaction.guild.channels.cache.find(ch => ch.name === 'channelName')
    await channel.send({embeds: [helpEmbed]});
}