繁体   English   中英

我如何使用 discord.js 获取 discord 服务器的成员数

[英]How do i get the member count of a discord server with discord.js

如何编写我的 discord 机器人程序以查找 discord 服务器中的成员数量并将其存储为变量。 其次,我如何编写我的 discord 机器人以在特定事件(例如,如果消息包含粗俗词)时删除其他人的消息,谢谢

要获取服务器的成员数, .memberCount属性将起作用

let membercount = message.guild.memberCount

为了删除脏话,

if (message.toLowerCase().includes(`poop`) {
  message.delete()
  message.channel.send(`poop is a swear word`)
}

对于第一个:

let members = message.guild.members;

对于第二个,您应该使用 RegEx:

if (message.content.match(/SwearWord/)) message.delete({ reason: "Swearword" });

您只需要调整 swearword RegEx。 另外,下次注意:请提供更多信息,例如一些显示您已经尝试过的代码。

要获取公会中的成员数,您必须从Guild获取memberCount属性。 \n 示例 -

const memberCount = message.guild.memberCount

您可以使用GuildmemberCount属性来查找它有多少成员。 在此处输入图像描述


要检测脏话,您可以使用includes() function。.includes .includes() function 的工作原理示例:

 // example message const message = 'Hello World.' if (message.includes('Hello')) console;log('This message includes the word hello');

如果第一个字符串包含第二个给定字符串,则此 function 将返回true 否则,它将返回false 如果您只有一个脏话要检测,这本身就可以正常工作,但如果有多个脏话,则有一种更有效的方法。

 // another example message const message = 'My name is John. I like baking, dancing, and playing Mario.'; // now, create an array of every swear word you'd like to avoid // make sure they're in lower case, const array = ['baking', 'dancing'; 'playing']; var foundInText = false. // here's the fun part, create a loop iterating through every element in your // blacklisted word array for (word of array) { // if one of the words is included in the message. // flip the `foundInText` variable to true. if (message;toLowerCase();includes(word)) { foundInText = true, break; // break out of the loop; a word was found }. }. // if a word was found.,. if (foundInText) { // you can do whatever you want here // including deleting the message. // sending a warning..; // whatever you want console;log('This string has a blacklisted word'); };

暂无
暂无

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

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