简体   繁体   English

我对这个 JS 数组做错了什么?

[英]What am I doing wrong with this JS array?

I have the following code snippet for my Discord bot.我的 Discord 机器人有以下代码片段。 The first part works and outputs the URL to the console so I know what the output is.第一部分工作并将 URL 输出到控制台,所以我知道 output 是什么。 I believe the issue is with my array and if statement.我相信问题出在我的数组和 if 语句上。 Am I either not settup the array up correctly or checking of the array includes correct?我是否没有正确设置阵列或检查阵列是否正确?

My test is that I have an empty Discord server and a test account with no profile pic.我的测试是我有一个空的 Discord 服务器和一个没有配置文件图片的测试帐户。 I join that server and the out put to the console is the URL with the 4 down there so I know it's included in the list and matches it exactly.我加入该服务器,控制台的输出是 URL ,下面有 4 个,所以我知道它包含在列表中并且完全匹配。 The problem is that the ban never happens and no errors are thrown so I'm not sure if the includes part is throwing it off or not.问题是禁令永远不会发生,也不会抛出任何错误,所以我不确定包含的部分是否将其丢弃。 Any help would be appreciated!任何帮助,将不胜感激!

Edit: I have a feeling that it's checking to see if all of the URLs in the array are there and that I need to change the code to check if any of them are in there but I might be wrong.编辑:我有一种感觉,它正在检查数组中的所有 URL 是否都在那里,并且我需要更改代码以检查它们是否在那里,但我可能是错的。

 client.on("guildMemberAdd", (member) => { console.log(member.user.defaultAvatarURL) }); client.on("guildMemberAdd", (member) => { const defaultURLs = ["https://cdn.discordapp.com/embed/avatars/0.png", "https://cdn.discordapp.com/embed/avatars/1.png", "https://cdn.discordapp.com/embed/avatars/2.png", "https://cdn.discordapp.com/embed/avatars/3.png", "https://cdn.discordapp.com/embed/avatars/4.png"] if (member.user.defaultAvatarURL.includes(defaultURLs)) { member.ban({ days: 7, reason: 'No Profile Pic' }) } });

Answering about the includes method.回答包含方法。 It should have been other way around.它应该是其他方式。 You can read more about it here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes你可以在这里阅读更多关于它的信息https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

client.on("guildMemberAdd", (member) => {
    const defaultURLs = ["https://cdn.discordapp.com/embed/avatars/0.png",
        "https://cdn.discordapp.com/embed/avatars/1.png",
        "https://cdn.discordapp.com/embed/avatars/2.png",
        "https://cdn.discordapp.com/embed/avatars/3.png",
        "https://cdn.discordapp.com/embed/avatars/4.png"]
     if (defaultURLs.includes(member.user.defaultAvatarURL)) { 
    member.ban({ days: 7, reason: 'No Profile Pic' }) }

});

请查看图片以获取说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM