Aranya Maji
Aranya Maji

Reputation: 1

Discord.js Snipe Command

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:

Bot File

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 event

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

Snipe command

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

Upvotes: 0

Views: 8256

Answers (2)

NoxItB
NoxItB

Reputation: 11

You can use this code if you want:

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

Upvotes: 1

Jeffplays2005
Jeffplays2005

Reputation: 320

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

Possible use away <db>.get.

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

Upvotes: 0

Related Questions