TypeError:bot.commands.get不是函数

时间:2018-05-08 19:28:41

标签: javascript node.js discord.js

我该如何解决? 当我启动机器人时,在控制台中我看到:

TypeError: bot.commands.get is not a function (183:34)

*require('dotenv').config()
const cfg = require('./config.js');
const Discord = require("discord.js");
const fs = require("fs");
const bot = new Discord.Client();
bot.commands = new Discord.Collection();
let xp = require("./xp.json");
let purple = cfg.config.PURPLE;
let cooldown = new Set();
let cdseconds = 5;

exports.config = () => {
    return bot.config;
}

bot.config = {
  TOKEN: process.env.BOT_TOKEN,
  TRN_APIKEY: process.env.TRN_APIKEY,
  YOUTUBE_APIKEY: process.env.YOUTUBE_APIKEY,
  OWNER_ID: cfg.config.OWNER_ID,
  PREFIX: cfg.config.PREFIX,
  IGNORE_CHANNELS: cfg.config.IGNORE_CHANNELS,
  WELCOME_MESSAGE_CHANNEL: cfg.config.WELCOME_MESSAGE_CHANNEL
};



  let messageArray = message.content.split(" ");
  let cmd = messageArray[0];
  let args = messageArray.slice(1);

  let commandfile = bot.commands.get(cmd.slice(bot.config.PREFIX.length));
  if(commandfile) commandfile.run(bot,message,args);

  setTimeout(() => {
    cooldown.delete(message.author.id)
  }, cdseconds * 1000)

});

bot.login(bot.config.TOKEN);

1 个答案:

答案 0 :(得分:0)

如果您正在尝试创建命令处理程序,请使用An Idiot's Guide to making a Discord.js bot

中的命令处理程序
if(message.content.includes(config.prefix){

const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
var params = msg.content.substring(msgcmd.length + 2);


if(command.length > 0){
try {
console.log(message.author.username + "#" + message.author.discriminator + " (" + message.author.id + ") tried to use the command " + command + " in " + message.guild.name)
let commandFile = require(`./commands/${command}.js`);
commandFile.run(client, message, args, params, config, fs);
} catch (err) {

console.log(err)
}}}

刚拿出

let messageArray = message.content.split(" ");
let cmd = messageArray[0];
let args = messageArray.slice(1);

let commandfile = bot.commands.get(cmd.slice(bot.config.PREFIX.length));
if(commandfile) commandfile.run(bot,message,args);

我认为你的错误与messageArray是一个对象有关,你不能运行一个对象,除非你通过一个函数运行它。

例如......

var i ;
i.run();

我不是这样的功能。

function imgurLinks (message) {
 return message.match(/https?:\/\/www\.tenor\.co\/[^\s]+/g)
}

这将通过像imgurlinks(message.content)这样的函数运行成员的消息。