問題描述
我正在嘗試為我朋友的 Discord 服務器制作一個惡作劇 Discord 機器人,但機器人不會響應任何東西;甚至 elseif 函數也沒有通過.如果有人知道我的代碼為什么不起作用,請指定它.
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 >.>");
}
})
推薦答案
消息有一個 attachments 屬性,您可以使用它來獲取消息的附件集合(如果有)
The Message have a attachments property which you can use to get a collection of attached file(s) to a message (if any)
您可以先執行 if (message.attachments.size > 0)
來檢查任何附加對象.
之后,您可以遍歷集合并檢查附件 URL 是否以 png
或 jpeg
結尾.
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;
}
編輯
因為你的機器人沒有響應任何東西.確保您在 message.channel.id != 425328056777834506
語句中具有相同 ID 的頻道中測試機器人.
(或者您可以先注釋掉 if 語句,然后在您的機器人功能齊全時添加.)
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 => {...
也會在您的機器人發送消息時被調用.您可以執行 if (message.author.id == <YourBotID>) {return;}
讓機器人忽略它自己的消息.
或者,如果您希望它忽略其他機器人發送的消息,您可以執行 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.
這篇關于使用 discord.js 檢測圖像并響應的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!