[英]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]});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.