[英]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.