【问题标题】:Discord.js Snipe CommandDiscord.js 狙击命令
【发布时间】: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


【解决方案1】:

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

可能用掉&lt;db&gt;.get

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

【讨论】:

    【解决方案2】:

    您可以根据需要使用此代码:

    const Discord = require("discord.js")
    const db = require("quick.db")
    
    module.exports = {
      name: "snipe",
      aliases: ["ms", "messagesnipe"],
      category: "info",
      usage: "(prefix)snipe",
      description: "Get last message which is deleted with message Author and Image(If any)",
      run:async (client, message, args) => {
        
        const msg = client.snipes.get(message.channel.id)
        if(!msg) return message.channel.send("There's nothing to snipe!")
        const embed = new Discord.MessageEmbed()
        .setAuthor(msg.author)
        .setDescription(msg.content)
        if(msg.image)embed
        .setImage(msg.image)
        .setColor("00FFFF")
        .setTimestamp();
        
        message.channel.send(embed)
               
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-30
      • 1970-01-01
      • 2021-10-26
      • 2021-07-29
      • 2020-11-23
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 2021-04-25
      相关资源
      最近更新 更多