【问题标题】:How can i pull out r/copypasta content and use it as a string?如何提取 r/copypasta 内容并将其用作字符串?
【发布时间】:2020-10-20 03:11:31
【问题描述】:

我编写了一个 Discord-bot,它回复收到的 dms 时会说“bruh dming me has just no point”。一个朋友给了我这个惊人的想法,从 r/copypasta 中提取答案。但我不知道该怎么做,所以我问。代码的重要部分:

client.on('message', message => {
    if (message.channel.type === "dm" && message.author.id !== client.user.id) {
        console.log("-----DM-----")
        console.log(message.content)
        console.log(message.author.tag)
        console.log("-----DM-----")
        message.author.send("bruh dming me has literally no point");
        client.channels.cache.get('726919268142415973').send({
                embed: {
                color: 0x00baba,
                    author: {
                        name: "I recieved the following DM:",
                        icon_url: message.author.avatarURL
                    },
                    title: message.author.tag,
                    description: message.content,
                    timestamp: new Date(),
                    footer: {
                        icon_url: client.user.avatarURL,
                        text: "Staff"
                    }
                }
        });
    }
});

【问题讨论】:

    标签: javascript bots discord.js


    【解决方案1】:

    您想要的是使用 reddit API 从某个 subreddit 中提取内容。 Reddit 的 api 对公众开放,您可以注册您的应用并使用他们的official API

    或者你可以使用他们的开放端点!这样就不需要凭据了

    1. 致电GET http://www.reddit.com/r/subreddit/new.json?sort=new。 (将sort=new 更改为以下任何一个:上升、热门、顶级、新......)。这将返回一个 JSON 结果,只需解析 JSON 并获取所需的 copypastas!

    2. 在 JSON 的末尾,有 2 个元素:"after": "t3_hi5yy6", "before": null 这些是您的“下一页”和“上一页”ID。要获取下一页,您只需调用:GET http://www.reddit.com/r/copypasta/new.json?sort=new&after=t3_hi7vqw

    使用 axios 的示例:

    var url = "http://www.reddit.com/r/subreddit/new.json?sort=new"
    var response = await axios.get(url);
    var after = response.data.data.after;
    var copypastas = response.data.data.children.map(x => x.data.selftext);
    //copypastas now contains an array of strings being copypatas;
    //Change url with url+="&after="+after and repeat the process in a loop
    

    【讨论】:

      【解决方案2】:

      我现在想出了这个:

          if (message.channel.type !== 'text') {
          if (message.author.id == client.user.id) return;
          fetch("https://www.reddit.com/r/copypasta/new.json?sort=new")
              .then((out) => {
                  console.log(out)
                  message.reply(`${out.data.data.children[0].data.selftext}`);
              });
              return;
      }
      

      (我知道我也可以使用if (message.author.bot) return; 而不是if (message.author.id == client.user.id) return;,但也许有些机器人会在频道中重定向他们的 DMS,哈哈)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-18
        • 2021-09-24
        • 1970-01-01
        • 2011-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多