Discord Bot Snipe命令“无法读取未定义的属性'get'”

时间:2020-09-22 06:57:27

标签: discord discord.js

我正在尝试为我的discord机器人设计一个阻击命令,所以我去看了有关如何执行此操作的教程,但是无论如何尝试解决此问题,我始终会遇到这个问题。有什么原因会发生这种情况吗?弹出的错误是:“无法读取未定义的属性'get'。任何帮助将不胜感激,谢谢!

const Discord = require("discord.js");
const prefix = '='

module.exports = {
    name: "snipe",
    description: "Recover a deleted message from someone.",
    execute (bot, message, args) {
      const msg = bot.snipes.get(message.channel.id)
      if (!msg) return message.channel.send(`That is not a valid snipe...`);
      const embed = new Discord.MessageEmbed()
        .setAuthor(msg.author.tag, msg.author.displayAvatarURL({ dynamic: true, size: 256 }))
        .setDescription(msg.content)
        .setFooter(msg.date);
      if (msg.image) embed.setImage(msg.image);
      message.channel.send(embed);
    }
};

Edit: bot.snipes is defined here

bot.snipes = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    bot.commands.set(command.name, command);
}

bot.on('ready', () => {
    console.log('THE WORST BOT IN THE WORLD IS READY TO FAIL AGAIN!');
    bot.user.setActivity('WUNNA FLOW', {type: "LISTENING"})
    
});

bot.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === 'anmar') {
        bot.commands.get('anmar').execute(message, args);
    } else if (command === 'mar') {
        bot.commands.get('anmar').execute(message, args);
    } else if (command === 'fishe') {
        bot.commands.get('fishe').execute(message, args);
    } else if (command === 'ran') {
        bot.commands.get('ran').execute(message, args);
    } else if (command === 'help') {
        bot.commands.get('help').execute(message, args);
    } else if (command === 'snipe') {
        bot.commands.get('snipe').execute(message, args);
    }

});

bot.on("messageDelete", message => {
    bot.snipes.set(message.channel.id, message);
});

bot.login (botconfig.token);

1 个答案:

答案 0 :(得分:0)

问题在于您何时拨打execute

bot.commands.get('snipe').execute(message, args);

在这里,当message函数需要3个参数时,您将bot而不是execute作为第一个参数传递:botmessage和{ {1}}。

改为使用此:

args

或者,您可以重构命令以从bot.commands.get('snipe').execute(bot, message, args); 获取bot

message
execute (message, args) {
  const bot = message.client;
  // rest of code...
}