简体   繁体   中英

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

How do I code my discord bot to find the amount of members in a discord server and store it as a variable. Secondly how do i code my discord bot to delete other peoples messages when a certain event (eg if the message contains a vulgar word) thanks

To get the membercount of the server, the .memberCount property will work

let membercount = message.guild.memberCount

For deleting swear words,

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

For the first one:

let members = message.guild.members;

For the second one, you should use a RegEx:

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

You just need to adjust the swearword RegEx. Also, note for next time: please provide some more info like some code showing what you already tried.

to get member count in a guild you have to get the memberCount property from the Guild . \n example -

const memberCount = message.guild.memberCount

You can use the memberCount property of a Guild to find how many members it has. 在此处输入图像描述


To detect swear words, you can use the includes() function. Example of how the .includes() function works:

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

If the first string includes the second given string, this function will return true . Otherwise, it will return false . This in itself will work fine if you only have one swear word to detect, but there is a more efficient method if there are multiple.

 // 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'); };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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