【问题标题】:Command handler loading the files however not running them命令处理程序加载文件但不运行它们
【发布时间】:2021-01-13 04:54:57
【问题描述】:

嗨(绝对不是专家)

使用 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);
}

命令文件夹中的一个文件(加载但不运行):

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"
}

【问题讨论】:

  • Discord.js 版本 12
  • 我忘了说错误信息对不起:commandFiles.run(bot, message, args); ^ ReferenceError: message is not defined at Object.&lt;anonymous&gt; (C:\Users\amral\Google Drive\Jordan-Discord-Bot\index.js:10:25)

标签: discord.js


【解决方案1】:

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

   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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 2021-06-06
    • 1970-01-01
    • 2021-07-15
    • 2022-01-14
    相关资源
    最近更新 更多