【问题标题】:Message.Edit Function Error With Discord Javascript Bot使用 Discord Javascript Bot 的 Message.Edit 函数错误
【发布时间】:2021-07-04 20:44:48
【问题描述】:

我正在尝试组装一个机器人,它可以将谷歌图像搜索结果检索到一个嵌入中,该嵌入“刷新/更改”页面到下一个图像/结果。现在,这个脚本有一个弹出的反应菜单,但是点击它时,我得到一个错误,上面写着:

       message.edit(gglembed(currentIndex))
                                             ^

TypeError: gglembed is not a function

代码:

    case 'rimg':
        var parts = message.content.split(' ');
        var search = parts.slice(1).join(" ");
        var options = {
            url: "http://results.dogpile.com/serp?qc=images&q=" + search,
            method: "GET",
            headers: {
                "Accept": "text/html",
                "User-Agent": "Chrome"
            }
        };
        request(options, function(error, response, responseBody) {


                $ = cheerio.load(responseBody); // load responseBody into cheerio (jQuery)

                var links = $(".image a.link");


                var urls = new Array(links.length).fill(0).map((v, i) => links.eq(i).attr("href"));
                var imgresults = urls[Math.floor(Math.random() * urls.length)];
                console.log(urls);
                const gglembed = new Discord.MessageEmbed()

                    .setTitle('Image Search Results from  ' + message.author.tag)
                    .setImage(imgresults);
                message.channel.send({
                    embed: gglembed
                }).then(imgsearchembedr => {
                        if (imgresults.length <= 2) {
                            return;
                        }
                        message.react('➡️')
                        const filter = (reactions, user) => true;
                        const collector = message.createReactionCollector(
                            // only collect left and right arrow reactions from the message author
                            (reaction, user) => ['⬅️', '➡️'].includes(reaction.emoji.name) && user.id === message.author.id,
                            // time out after a minute
                            {
                                time: 60000
                            });

                    let currentIndex = urls + 1;
                    collector.on('collect', reaction => {
                        message.reactions.removeAll().then(async () => {
                            reaction.emoji.name === '⬅️' ? currentIndex -= 10 : currentIndex += 10
                            message.edit(gglembed(currentIndex))
                            if (currentIndex !== 0) {
                                await message.react('⬅️')
                            } else {
                                if (currentIndex + 10 < urls.length) message.react('➡️')
                            }
                        })
                    })
                })
        })
break;

任何帮助将不胜感激,谢谢。

【问题讨论】:

  • 嗯,它就在那里说。 gglembed 不是函数,因此不接受任何参数。
  • @WorthyAlpaca 是的,这是一个直接错误。我只是感到困惑,因为 discord.js 文档说它可以是嵌入的;请参阅:discord.js.org/#/docs/main/stable/typedef/MessageEditOptions 我如何使这个嵌入一个函数并在这里调用它?对我来说似乎很奇怪,但我正在学习。
  • 是的,message.edit 可以是嵌入,但嵌入本身不是函数,这就是问题的来源。 gglembed(currentIndex) () 使它成为一个函数,在这种情况下是错误。有关如何 edid 嵌入的帮助,请参阅 here。

标签: javascript scripting discord discord.js


【解决方案1】:

您正在尝试将参数传递给您的 MessageEmbed?那是行不通的。要使用嵌入编辑消息,您必须执行message.edit(gglembed)。你想根据变量currentIndex 持有的内容以某种方式编辑嵌入,我说得对吗?如果是这种情况,您必须解释该变量应该做什么,因为我在您的代码中看不到这一点(我看不出该变量应该如何影响您的嵌入......)

编辑: 好的,要对嵌入消息做出反应,您必须执行以下操作:(我只替换了代码的重要部分)

//create a variable embedMessage which holds the message sent by the bot
const embedMessage = await message.channel.send({
                   embed: gglembed
               }).then(imgsearchembedr => {
                   if (imgresults.length <= 2) {
                       return;
                   }
               });
               embedMessage.react('➡️')
               const filter = (reactions, user) => true;
               const collector = embedMessage.createReactionCollector(
               // only collect left and right arrow reactions from the message author
                   (reaction, user) => ['⬅️', '➡️'].includes(reaction.emoji.name) && user.id === message.author.id,
                   // time out after a minute
                   {
                       time: 60000
                   }
               );
               let currentIndex = urls + 1;
               collector.on('collect', reaction => {
                   embedMessage.reactions.removeAll().then(async () => {
                       reaction.emoji.name === '⬅️' ? currentIndex -= 10 : currentIndex += 10
                       await embedMessage.edit(gglembed)
                       if (currentIndex !== 0) {
                           await embedMessage.react('⬅️')
                       } else {
                           if (currentIndex + 10 < urls.length) message.react('➡️')
                       }
                  })
              })```

【讨论】:

  • 好的,感谢您向我指出这一点。不敢相信我没有注意到这一点。我希望嵌入编辑和发布新的 URLS 结果,我也添加了 +1。你可以看到我现在在我发布的版本中它是灰色的,但在我自己的版本中它现在就在那里。 currentindex 现在不是问题,实际上,反应添加到我自己的消息而不是嵌入,知道为什么会这样吗?
猜你喜欢
  • 2018-03-04
  • 2021-07-15
  • 2017-11-15
  • 1970-01-01
  • 2021-12-12
  • 2020-12-20
  • 2017-11-05
  • 2019-11-10
  • 2018-05-02
相关资源
最近更新 更多