Discord.js Snipe命令 [英] Discord.js Snipe Command

查看:38
本文介绍了Discord.js Snipe命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试使用命令处理程序在我的机器人中执行Discord.js snipe命令,并且一切正常,on messageDelete事件也正常工作,但是当我删除用户消息并运行!snipe时,出现错误:无法读取未定义的属性'get'.这是我的机器人文件:

So I am trying to make a Discord.js snipe command in my bot with command handlers and everything works fine, the on messageDelete event works fine too but when I delete a user message and run !snipe , I get the error: Cannot read property 'get' of undefined. Here are my bot files:

启动文件

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);
    }
  }

推荐答案

错误是否提到错误发生的特定行?

Does the error mention a specific line on where the error occurs?

可能会占用< db> .get .

还有另一件事是您在消息删除事件中使用 message.client.snipes.get 的位置.可能替换为 bot.snipes .

And also another thing is where your using message.client.snipes.get in the message delete event. Possibly replace with bot.snipes.

这篇关于Discord.js Snipe命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆