简体   繁体   English

我的 discord.js 机器人发送多个 gif 而不是一个。 我该如何解决?

[英]My discord.js bot sends multiple gifs instead of one. How do I fix this?

I'm trying to make this command on my discord bot where it sends a random kirby gif using the giphy api.我试图在我的 discord 机器人上发出这个命令,它使用 giphy api 发送一个随机的 kirby gif。 However, I only want it to send one, but it sends multiple.但是,我只希望它发送一个,但它发送多个。 This is the code:这是代码:

client.on('message', message => {
    if (message.content === 'k!gif')

    giphy.search('gifs', {"q": "kirby"})
    .then((response) => {
        var totalResponses = response.data.length
        var responseIndex = Math.floor((Math.random() * 10) + 1) % totalResponses;
        var responseFinal = response.data[responseIndex]

        message.channel.send("We stan kirby gifs", {
            files: [responseFinal.images.fixed_height.url]
        })
    }).catch(() => {
        message.channel.send('Kirby has run into a roadblock and was unable to complete his task.');
    })

})

Thanks for helping in advance!感谢您提前提供帮助!

As @Giuiopime highlighted, you are not making any requirement for the command.正如@Giuiopime 强调的那样,您对该命令没有任何要求。 So every time you send a message, the current code is executed and the gif is sent.所以每次发送消息时,都会执行当前代码并发送 gif。 Once this one is sent, a new message is caught by the bot and the process continues again and again...发送此消息后,机器人会捕获一条新消息,并且该过程会一次又一次地继续……

Make sure to attach the current code inside the if and also verify that the player isn't a bot.确保在 if 中附加当前代码,并验证玩家不是机器人。 This code may work.此代码可能有效。 I didn't have tested it.我没有测试过。

client.on('message', message => {
  if(message.author.bot) return;
  if (message.content === 'k!gif'){
    giphy.search('gifs', {"q": "kirby"}).then((response) => {
      
        var totalResponses = response.data.length
        var responseIndex = Math.floor((Math.random() * 10) + 1) % totalResponses;
        var responseFinal = response.data[responseIndex]

        message.channel.send("We stan kirby gifs", {
            files: [responseFinal.images.fixed_height.url]
        })
    }).catch(() => {
        message.channel.send('Kirby has run into a roadblock and was unable to complete his task.');
    })
  }
})

You used if (message.content === 'k!gif') without opening any curly braces, so what you need to do is to include all your code in that if.您使用if (message.content === 'k!gif')没有打开任何花括号,所以您需要做的就是在 if 中包含所有代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复我的 discord 机器人(在 discord.js 中)不响应我的命令 - How do I fix my discord bot (in discord.js) not responding to my commands 如何让 discord 机器人在 discord.js 中发送消息后有表情符号反应? - How do I make discord bot have an emoji reaction after it sends its message in discord.js? 如何在 discord.js 中为 discord bot 合并图像? - How do I merge images in discord.js for a discord bot? Bot 多次发送消息内容 | Discord.js - Bot Sends Multiple Times The Content Of The Message | Discord.js 如何使我的 discord 机器人具有自定义状态 (discord.js) - How do I make my discord bot have a custom status (discord.js) (DISCORD.JS) 如何修复我的 discord“cmds”命令? - (DISCORD.JS) how do i fix my discord "cmds" command? discord.js - 如何将“尝试我的命令”功能添加到我的机器人? - discord.js - How do i add the "TRY MY COMMANDS" feature to my bot? 有什么办法可以修复我正在创建的 discord 机器人 discord.js - Is there any way i can fix my discord bot, which i`m creating discord.js discord.js 问题。 我如何让机器人对一个命令给出不同的响应? - discord.js question. how do i make the bot give a different response to one command? 如何为Discord.js机器人将变量XP添加到userData.XP中? - How do I add variable `XP` to `userData.XP` for my Discord.js bot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM