提取文字少于1024个字符的reddit帖子

时间:2019-05-02 14:49:29

标签: javascript discord.js

`我正在尝试将reddit帖子提取到不和谐的bot上,但是只希望帖子文本<1024个字符的帖子,因为那是RichEmbed中.addField的限制。我将如何去做?

目前,我有:

     const allowed = message.channel.nsfw ? body.data.children : 
     body.data.children.filter(post => !post.data.over_18);

     const postlength = body.data.children.filter(post => 
     !post.data.selftext.length > 1024);
        if (!postlength && !allowed.length) 
        return message.channel.send('It seems we are out of fresh memes!, Try again later.');

        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)
        .addField("Post: ", allowed[randomnumber].data.selftext + allowed[randomnumber].data.selftext.length)
        .setImage(allowed[randomnumber].data.url)
        .addField("Other info:", "Up votes: " + allowed[randomnumber].data.ups + " / Comments: " + allowed[randomnumber].data.num_comments)
        .setFooter("Memes provided by r/dankmemes")
        message.channel.send(embed)

我的变量postlength是我尝试过滤掉reddit帖子的地方,但是似乎没有效果。

1 个答案:

答案 0 :(得分:1)

解决方案

您正在做

filter(post => !post.data.selftext.length > 1024);

它将检查false是否大于1024。我假设您想提交并获得所有长度小于1024的帖子?

将代码更改为...

filter(post => post.data.selftext.length < 1024);

说明

在属性前添加感叹号!时,您将颠倒其真实性。如果不是

,则会是 Truthy
  • 一个空字符串
  • 0
  • 未定义
  • “假”
  • false

在您的情况下,您将要反转不适合上述条件的字符串,因此除非长度实际上为0,否则它将被转换为false:

!post.data.selftext.length