我正在使用 discord.js v13,我剛剛開始撰寫代碼。嘗試運行“beep”命令時,它在聊天中沒有回應,控制臺中沒有錯誤,也沒有使機器人崩潰,只是沒有任何反應。
這是我的 index.js:
const config = require("./config.json")
const fs = require("fs")
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
//////////////////////////////////////////////////////////////////////////////////////////
client.on("ready", () => {
console.log(`[カノンの準備ができました!]`)
client.user.setActivity({ type: "LISTENING", name: `Love Spiral Tower` })
})
//////////////////////////////////////////////////////////////////////////////////////////
client.config = require("./config.json")
client.commands = new Discord.Collection()
client.aliases = new Discord.Collection()
fs.readdir("./コマンド/", (err, files) => {
if (err) return console.log("このコマンドの処理中にエラーが発生しました")
const jsFiles = files.filter(f => f.split(".").pop() === "js")
if (jsFiles.length <= 0) return console.log("このコマンドの処理中にエラーが発生しました")
jsFiles.forEach(file => {
const cmd = require(`./コマンド/${file}`)
console.log(`ファイルが見つかりました!: ${file}`)
client.commands.set(cmd.name, cmd)
if (cmd.aliases) cmd.aliases.forEach(alias => client.aliases.set(alias, cmd.name))
})
})
client.on("message", async message => {
const prefix = config.prefix
if (!message.content.startsWith(prefix)) return
const args = message.content.slice(prefix.length).trim().split(/ /g)
const command = args.shift().toLowerCase()
const cmd = client.commands.get(command) || client.commands.get(client.aliases.get(command))
if (!cmd) return
try {
cmd.run(client, message, args)
} catch (e) {
console.error(e)
}
})
client.login(config.token);
我的命令“嗶”:
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
module.exports = {
name: "beep",
alias: ["b"],
execute (client, message, args){
message.channel.send("Boop!")
}
}
請幫助。
uj5u.com熱心網友回復:
您的機器人未檢測到訊息。你需要GUILD_MESSAGES意圖
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES
]
})
另一件事是,你應該不為每個命令創建一個新的客戶端。這只會創建許多不必要的客戶端,而您可以只使用接收事件的客戶端。
uj5u.com熱心網友回復:
索引.js
將 client.on("message", async message =>
僅用于discord.js V12和,也許是客戶不會回答。
你應該使用messageCreate,而不是message如果你使用的V13。
例子:
client.on("messageCreate", async message => {
const prefix = config.prefix
if (!message.content.startsWith(prefix)) return
const args = message.content.slice(prefix.length).trim().split(/ /g)
const command = args.shift().toLowerCase()
const cmd = client.commands.get(command) || client.commands.get(client.aliases.get(command))
if (!cmd) return
try {
cmd.run(client, message, args)
} catch (e) {
console.error(e)
}
})
命令
你有沒有嘗試使用async run代替execute?
如果沒有,請在我的帖子底部使用此代碼或使用 async run(client, message, args)
const Discord = require('discord.js');
module.exports = {
name: "beep",
alias: ["b"],
async run(client, message, args){
message.channel.send("Boop!")
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/363720.html
