命令处理程序正在加载文件,但未运行它们

时间:2020-09-27 11:46:22

标签: discord.js

你好(绝对不是专家)

使用discord.js设置命令处理程序,我知道文件正在加载,但是当我运行它们时,它们不起作用(运行)。

我的索引文件的开始

const { prefix, token }  = require("./settings/config.json");
const Discord = require("discord.js");
const fs = require("fs");
const bot = new Discord.Client();
bot.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
  const command = require(`./commands/${file}`);
  commandFiles.run(bot, message, args);
  console.log(`${file} loaded!`);
  bot.commands.set(command.name, command);
}

commands文件夹中的一个文件(已加载但未运行):

const Discord = require("discord.js");
const client = new Discord.Client();
let servericon = ('https://i.imgur.com/foKcByT.png');

module.exports.run = async (bot, message, args) => {
  // No Perms Embed
  const noPermsErrEmbed = new Discord.MessageEmbed()
  .setColor('FF6961')
  .setTitle("**error!**")
  .setDescription("This command can only be used by staff!")
  .setTimestamp()
  .setFooter(message.author.tag + " | Peace Keeper", message.author.displayAvatarURL({dynamic: true, size: 1024}))
  if(!message.member.hasPermission("MANAGE_MESSAGES")) return message.reply(noPermsErrEmbed).then(msg => msg.delete(5000));

  let ancArgs = args.slice(0).join(" ").split('|');
  
  if (args.length >= 3) {
    message.delete().then(() => {
      const ancEmbed = new Discord.MessageEmbed()
      .setColor('#ABDFF2')
      .setTitle("** " + ancArgs[0] + " **")
      .setDescription(ancArgs[1])
      .setTimestamp()
      .setThumbnail(servericon)
      .setFooter(message.author.tag + " | Peace Keeper", message.author.displayAvatarURL({dynamic: true, size: 1024}))
      message.channel.send(ancEmbed);
    })
  } else {
    message.delete().catch();
    const ancErrEmbed = new Discord.MessageEmbed()
      .setColor('FF6961')
      .setTitle("**error!**")
      .setDescription("use the correct format: !special-anc <title> | <message>")
      .setTimestamp()
      .setFooter(message.author.tag + " | Peace Keeper", message.author.displayAvatarURL({dynamic: true, size: 1024}))
      message.reply(ancErrEmbed).then(msg => msg.delete({timeout: 10000}));
  }
}

module.exports.help = {
  name: "special-anc"
}

1 个答案:

答案 0 :(得分:0)

它不会运行命令,因为您使用了错误的变量来调用运行部件:

   commandFiles.run(bot, message, args);

应该是:

   command.run(bot, message, args);

commandFiles只是文件列表,因此与您的命令没有直接关系。

但是,您的代码有问题。 这是您的module.exports对象(在command变量中):

{
   run: [AsyncFunction],
   help: { name: String }
}

在使用此行bot.commands.set(command.name, command);时,由于没有name属性,该行将不起作用,应为help.name