【发布时间】: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