【问题标题】:Discord bot snipe command "Cannot read property 'get' of undefined"Discord bot 狙击命令“无法读取未定义的属性 'get'”
【发布时间】:2021-01-08 06:31:30
【问题描述】:

我正在尝试为我的不和谐机器人设计一个狙击命令,所以我去看了一个关于如何做到这一点的教程,但我总是偶然发现这个问题,无论我尝试做什么来解决它。有什么理由会发生这种情况吗?弹出的错误是:“无法读取未定义的属性'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);

【问题讨论】:

  • 嗨,欢迎来到 Stack Overvfow! bot.snipes 好像是undefined,请问您能否提供定义bot.snipes 的代码?
  • @cherryblossom 我已对其进行了编辑,以表明 bot.snipes 已在我的主机器人文件中定义。
  • 你是如何运行命令的?确保将bot 作为第一个参数传递给execute
  • @cherryblossom 我对其进行了编辑以展示我是如何运行它们的,这不是一种可靠的运行方式,但我计划很快对其进行修改,我会立即解决这个问题!跨度>

标签: discord discord.js


【解决方案1】:

问题是当你打电话给execute:

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

在这里,当您的 execute 函数需要 3 个参数时,您传递 message 而不是 bot 作为第一个参数:botmessageargs

改用这个:

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

或者,您可以重构命令以从 message 获取 bot

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

【讨论】:

    猜你喜欢
    • 2021-08-29
    • 2020-06-20
    • 1970-01-01
    • 2021-03-31
    • 2020-10-15
    • 2021-09-14
    • 2021-02-01
    • 2021-02-22
    • 1970-01-01
    相关资源
    最近更新 更多