【问题标题】:Problem with Reddit command on discord.js botdiscord.js 机器人上的 Reddit 命令问题
【发布时间】:2021-04-07 10:22:08
【问题描述】:

我正在尝试在我的 discord.js 机器人中放置一个受诅咒的图像命令,但它不断出现此错误

got('https://www.reddit.com/r/madlads/random/.json').then(response => {
^

ReferenceError: got is not defined

这里是代码

if(command === "cursed") {

        got('https://www.reddit.com/r/cursedimages/random/.json').then(response => {
                let content = JSON.parse(response.body);
            var image = content[0].data.children[0].data.url;
            let permalink = content[0].data.children[0].data.permalink;
            let memeUrl = `https://reddit.com${permalink}`;
            let memeImage = content[0].data.children[0].data.url;
            let memeTitle = content[0].data.children[0].data.title;
            let memeUpvotes = content[0].data.children[0].data.ups;
            let memeNumComments = content[0].data.children[0].data.num_comments;
            const cursedembed = new Discord.MessageEmbed()
                .setColor('RANDOM')
                .setTitle(content[0].data.children[0].data.title)
                .setURL(`${memeUrl}`)
                .setImage(image)
                .setFooter(`???? ${memeUpvotes} ???? ${memeNumComments}`)
                .setTimestamp()

            message.channel.send(cursedembed);
        });
}

【问题讨论】:

  • 你确定你输入的是get而不是got
  • 这两种方法我都试过了,都出现了这个错误
  • 如果您尝试使用fetch 而不是get 会怎样?
  • 它仍然显示未定义
  • 你有node-fetch作为你的包之一吗?

标签: discord.js


【解决方案1】:

由于您没有 node-fetch 作为您的软件包之一,您将无法使用 fetch 函数。要使用此软件包,您必须遵循三个步骤。

  1. 安装node-fetch。这可以通过在您的项目目录中运行 npm i node-fetch 来完成。
  2. node-fetch 导入您的代码。这可以通过在代码文件顶部添加 const fetch = require('node-fetch'); 来完成,类似于您的 discord.js 导入。
  3. got 替换为fetch

之后,您应该可以使用fetch 来下载网页内容并将reddit 内容发布到discord 上。

【讨论】:

    猜你喜欢
    • 2020-08-21
    • 2021-04-18
    • 2020-08-25
    • 2021-06-02
    • 2020-11-09
    • 2021-06-02
    • 2020-07-09
    • 2020-09-22
    • 1970-01-01
    相关资源
    最近更新 更多