【发布时间】:2021-02-27 10:40:24
【问题描述】:
所以我尝试使用命令处理程序在我的机器人中创建 Discord.js 狙击命令,一切正常,on messageDelete 事件也正常工作,但是当我删除用户消息并运行 !snipe 时,我收到错误消息:
Cannot read property 'get' of undefined。
这是我的机器人文件:
机器人文件
const { Client, Message, Collection, Discord } = require('discord.js'); const mongoose = require('mongoose'); const config = require('./config.json'); const client = require('./dashboard/modules/auth-client');
const bot = new Client();
bot.snipes = new Collection();
bot.login(config.bot.token);
mongoose.connect(config.mongoURI, { useNewUrlParser: true, useUnifiedTopology: true }, (error) => error
? console.log('Failed to connect to database')
: console.log('Connected to database'));
module.exports = bot;
require('./handlers/event-handler'); require('./dashboard/server');
MessageDelete 事件
const Event = require("./event");
const { MessageEmbed } = require('discord.js')
const { bot } = require("../../bot.js")
module.exports = class extends Event {
on = "messageDelete";
async invoke(msg) {
if (msg.author.bot) return;
const snipes = msg.client.snipes.get(msg.channel.id) || [];
snipes.unshift({
content: msg.content,
author: msg.author,
image: msg.attachments.first() ? msg.attachments.first().proxyURL : null,
date: new Date().toLocaleString("en-GB", {
dataStyle: "full",
timeStyle: "short",
}),
});
snipes.splice(10);
msg.client.snipes.set(msg.channel.id, snipes);
let embed = new MessageEmbed()
.setTitle(`New message deleted!`)
.setDescription(
`**The user ${msg.author.tag} has deleted a message in <#${msg.channel.id}>**`
)
.addField(`Content`, msg.content, true)
.setColor(`RED`);
let channel = msg.guild.channels.cache.find(
(ch) => ch.name === "bot-logs"
);
if (!channel) return;
channel.send(embed);
} catch(e) { }
};
狙击命令
const { MessageEmbed } = require('discord.js');
const bot = require('../bot.js');
module.exports = class {
name = 'snipe';
category = 'General';
async execute(bot, msg, args) {
const snipes = bot.snipes.get(msg.channel.id) || [];
const snipedmsg = snipes[args[0] - 1 || 0];
if (!snipedmsg) return msg.channel.send("Not a valid snipe!");
const Embed = new MessageEmbed()
.setAuthor(snipedmsg.author.tag, snipedmsg.author.displayAvatarURL({ dynamic: true, size: 256 }))
.setDescription(snipedmsg.content)
.setFooter(`Date: ${snipedmsg.date} | ${args[0] || 1}/${snipes.length}`)
if (snipedmsg.attachment) Embed.setImage(snipedmsg.attachment);
msg.channel.send(Embed);
}
}
【问题讨论】:
-
你已经设置了
bot.snipes = new Collection();,但是你通过msg.client.snipes阅读了它。
标签: javascript node.js discord discord.js