无法读取未定义的属性'nsfw'-discord.js

时间:2019-03-21 16:37:00

标签: javascript discord.js commando

我目前正在开发使用'discord.js','discord.js-commando'和'snekfetch'的discord机器人。我正在尝试创建一个函数,如果公会成员键入“!meme”,则不和谐的bot将从r / dankmemes抓取一个随机帖子,然后使用richEmbed将其发送到相应的频道。但是,在测试该功能时,会出现以下错误消息:

TypeError: Cannot read property 'nsfw' of undefined

TypeError: Cannot read property 'send' of undefined

我已经尝试解决问题4天了,对于导致此问题的原因我一无所知。根据discord.js文档,这应该可以正常工作。我已经在下面附加了命令模块:

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

1 个答案:

答案 0 :(得分:0)

根据文档中的the examplerun的回调为run(message, args),但是您将其定义为run(client, message, args),因此message.channel是未定义的,因为尝试在错误的对象上访问它。

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