【发布时间】:2021-12-31 11:20:00
【问题描述】:
我正在调整我的命令处理程序以使用斜杠并且它有效,但现在我遇到了其他问题
1 - 当我尝试运行 /ping 命令时,出现“交互失败”并且控制台上没有错误
2 - 我放了一些 console.log() 来查看它是否提供了正确的值,但是一些变量给出了 undefined
这是我的命令处理程序 (index.js):
const fs = require('fs');
const pComandos = fs.readdirSync('Comandos');
client.commands = new Discord.Collection();
client.description = new Discord.Collection();
for (const folder of pComandos) {
const file = fs.readdirSync('Comandos/' + folder);
for (const comando of file) {
let cmd = require(`./Comandos/${folder}/${comando}`);
commandsArray = [];
console.log(chalk.green(`[ C ] O comando ` + chalk.bold(comando) + ` foi carregado!`))
client.commands.set(cmd.help.name, cmd)
if (cmd.help.description) {
cmd.desc = true
cmd.help.description.forEach(desc => client.commands.set(desc, cmd))
}
client.commands.set(cmd)
commandsArray.push({
name: cmd.help.name,
description: cmd.help.description.join("")
})
if (cmd.init) cmd.init(client)
client.on("ready", async() => {
const server = await client.guilds.cache.get("892210305558532116")
server.commands.set(commandsArray)
})
}
}
交互事件(interactionCreate.js):
const { Interaction, CommandInteraction } = require("discord.js");
const fs = require("fs");
exports.run = (client, interaction) => {
if (interaction.isCommand()) {
const pComandos = fs.readdirSync('./Comandos');
for (const folder of pComandos) {
let file = fs.readdirSync("./Comandos/" + folder);
console.log(file) // [ 'ping.js' ]
for (const comando of file) {
let cmd = require(`../../Comandos/${folder}/${comando}`);
console.log(cmd)
/*
{
run: [AsyncFunction (anonymous)],
help: { name: 'ping', description: [ 'Pong!' ] },
desc: true
}
*/
let command =
client.commands.get(cmd) ||
client.commands.get(client.description.get(cmd));
console.log(command) // undefined
console.log(client.commands.get(cmd)) // undefined
if (command) {
command.run(client, interaction);
}
}
}
}
};
ping 命令(ping.js):
const discord = require("discord.js");
exports.run = async (client, interaction) => {
interaction.deferUpdate();
var latencia = Date.now() - interaction.createdTimestamp
interaction.reply({content: `<:emoji_3:892431734203891722> | Ping atual do client: ${client.ws.ping} (${latencia}).\n???? | Timezone: ${process.env.TZ}`});
};
exports.help = {
name: "ping",
description: ["Pong!"]
};
这是我的文件组织:https://prnt.sc/20ddh4e 和控制台(Console.log值在interactionCreate.js代码中):https://prnt.sc/20ddnlv
【问题讨论】:
标签: javascript node.js discord discord.js