問題描述
我正在嘗試為我朋友的 Discord 服務(wù)器制作一個(gè)惡作劇 Discord 機(jī)器人,但機(jī)器人不會(huì)響應(yīng)任何東西;甚至 elseif 函數(shù)也沒有通過.如果有人知道我的代碼為什么不起作用,請指定它.
I'm trying to make a prank Discord bot for my friend's Discord server, but the bot won't respond to anything; not even the elseif function passes. If anyone know why my code isn't working can you please specify it.
注意:如果您需要參考,客戶端變量是 Discord.Client.
NOTE: Client variable is Discord.Client if you needed a reference.
client.on("message", message => {
if (message.channel.id != 425328056777834506) return;
if (Math.floor(Math.random() * Math.floor(4))== 3 && message.embeds.length > 0) {
message.channel.send("https://cdn.discordapp.com/attachments/330441704073330688/453693702687162369/yeet.png");
} else if (message.embeds.length < 0) {
message.channel.send("send me photos of your win >.>");
}
})
推薦答案
消息有一個(gè) attachments 屬性,您可以使用它來獲取消息的附件集合(如果有)
The Message have a attachments property which you can use to get a collection of attached file(s) to a message (if any)
您可以先執(zhí)行 if (message.attachments.size > 0)
來檢查任何附加對象.
之后,您可以遍歷集合并檢查附件 URL 是否以 png
或 jpeg
結(jié)尾.
You can do a if (message.attachments.size > 0)
to check for any attached objects first.
Afterwards, you can loop through the collection and check if the attached file URL ends with a png
or jpeg
.
if (message.attachments.size > 0) {
if (message.attachments.every(attachIsImage)){
//something
}
}
...
function attachIsImage(msgAttach) {
var url = msgAttach.url;
//True if this url is a png image.
return url.indexOf("png", url.length - "png".length /*or 3*/) !== -1;
}
編輯
因?yàn)槟愕臋C(jī)器人沒有響應(yīng)任何東西.確保您在 message.channel.id != 425328056777834506
語句中具有相同 ID 的頻道中測試機(jī)器人.
(或者您可以先注釋掉 if 語句,然后在您的機(jī)器人功能齊全時(shí)添加.)
For your bot not responding to anything. Make sure that you are testing the bot in the channel that has the same ID in the message.channel.id != 425328056777834506
statement.
(Or you can comment out that if statement first, then add that in when your bot is fully functional.)
此外,client.on("message", message => {...
也會(huì)在您的機(jī)器人發(fā)送消息時(shí)被調(diào)用.您可以執(zhí)行 if (message.author.id == <YourBotID>) {return;}
讓機(jī)器人忽略它自己的消息.
或者,如果您希望它忽略其他機(jī)器人發(fā)送的消息,您可以執(zhí)行 if (message.author.bot) {return;}
.
Also, client.on("message", message => {...
gets called when your bot sends a message too. You can do if (message.author.id == <YourBotID>) {return;}
to let the bot ignore it's own messages.
Or you can do if (message.author.bot) {return;}
if you want it to ignore messages sent by other bots.
這篇關(guān)于使用 discord.js 檢測圖像并響應(yīng)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!