Search code examples
javascriptdiscord.jscommando

Cannot read property 'nsfw' of undefined - discord.js


I'm currently working on a discord bot that makes use of 'discord.js', 'discord.js-commando' and 'snekfetch'. I'm trying to create a function in which if a guild member were to type "!meme", the discord bot will grab a random post from r/dankmemes and send it to the respective channel using richEmbed. however, upon testing the function the following error messages appear:

TypeError: Cannot read property 'nsfw' of undefined

TypeError: Cannot read property 'send' of undefined

I've been trying to resolve the issue for 4 days and i'm utterly clueless as to what is causing this issue. According to the discord.js documentation this should work absolutely fine. I've attached the command module below:

const Commando = require('discord.js-commando');
const Discord = require('discord.js');
const snekfetch = require('snekfetch');

class MemesRssCommand extends Commando.Command
{
    constructor(client)
    {
        super(client,{
            name: 'meme',
            group: 'simple',
            memberName: 'meme',
            description: 'Takes a random meme from r/dankmemes'
        });
    }

    async run(client, message, args) {
        try {
            const { body } = await snekfetch
                .get('https://www.reddit.com/r/dankmemes.json?sort=top&t=week')
                .query({ limit: 800 });
            const allowed = message.channel.nsfw ? body.data.children : body.data.children.filter(post => !post.data.over_18);
            if (!allowed.length) return message.channel.send('Our farmers were unable to locate any ripe memes! Try again later (You shouldnt see this message. If you are reading this, then reddit is probably offline. If reddit is online and you still get this message, contact @𝕏𝕪𝕝𝕠𝕟𝕠𝕝𝕪#1612');
            const randomnumber = Math.floor(Math.random() * allowed.length)
            const embed = new Discord.RichEmbed()
            .setColor(0x00A2E8)
            .setTitle(allowed[randomnumber].data.title)
            .setDescription("Posted by: " + allowed[randomnumber].data.author)
            .setImage(allowed[randomnumber].data.url)
            .addField("Other info:", "Up votes: " + allowed[randomnumber].data.ups + " / Comments: " + allowed[randomnumber].data.num_comments)
            .setFooter("Posted by: " + allowed[randomnumber].data.author + " | Memes provided by https://www.reddit.com/r/dankmemes")
            message.channel.send(embed)
        } catch (err) {
            return console.log(err);
        }
    }

}

module.exports = MemesRssCommand

Solution

  • According to the example in documentation the callback for run is run(message, args) but you are defining it as run(client, message, args), thus the message.channel is undefined since you are trying to access it on the wrong object.

        async run(message, args) {
            const member = args.member;
            const channel = message.channel
            // ....
        }