問題描述
我用一個簡單的ping"命令創建了一個小命令處理程序.命令.但是,嘗試訪問 message.channel
顯示未定義.這是為什么呢?
I created a small command handler with a simple "ping" command. However, trying to access message.channel
shows up undefined. Why is this?
./index.js
//Discord, fs, prefix, client etc. declarations
client.commands = new Collection()
const files = fs.readdirSync("./commands").filter(file => file.endsWith(".js")
for (const file of files) {
const command = require(`./commands/${file}`)
client.commands.set(command.name, command)
}
client.on("messageCreate", message => {
const [command, ...args] = message.content.slice(prefix.length).split(/ +/g)
if (client.commands.has(command) {
client.commands.get(command).execute(message, client, args)
}
})
client.login(/*token*/)
./commands/ping.js
module.exports = {
name: "ping",
description: "Make me say pong",
async execute(client, message, args) {
message.channel.send("Pong!")
}
}
推薦答案
雖然 Message.channel
可能有效,但 message
不是 Message<的實例/代碼>.它實際上是
Client
的一個實例.參數的順序總是很重要,將它們放在錯誤的順序可能會引發 TypeErrors.這里有1個簡單的解決方案
While Message.channel
may be valid, message
is not an instance of Message
. It is actually an instance of Client
. The order of the arguments always matter, and putting them in the wrong order can throw TypeErrors. There is 1 simple solution here
按正確的順序排列參數!您可以更改執行或聲明
Make the arguments in the right order! You can either change the execution, or the declaration
client.commands.get(command).execute(client, message, args)
這真的很常見,而且發生的不僅僅是向頻道發送消息
This is really common and happens for more than sending messages to channels
這篇關于當不應該在 discord.js 命令處理程序中未定義時,無法讀取未定義的屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!