i want the command to dm 1 member then wait 5 seconds then dm the next member untill all members are dm by the bot
this is the code i have
if (command === 'dmall') {
message.guild.members.cache.forEach(async (member) => {
const messageSent = await member.send(args[0]);
console.log(messageSent);
await wait(5000);
});
}
the error i get is member.send is not function
this is the wait thing it has no problems
let wait = (ms) => {
if (!ms) throw new TypeError("Time isn't specified");
return new Promise((resolve) => setTimeout(resolve, ms));
};
Your code is working for me. So the problem is not in your code that you give probably.
You must add GUILD_MEMBERS intent to your client in order to read all the guild member user data.
You should replace your code with this when creating a new client instances :
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS] });
( Or if you have another intents, just add Intents.FLAGS.GUILD_MEMBERS to it. )
Note : This intent is privilaged. Which means if you add this intents to your bot, it will not be able to join any guild anymore after 100 guild reached until your bot verified. If this is a private bot. Then it would be fine.
Also make sure to catch an error in your code when you send a message to all member to prevent your bot to crashing when :
So it should be :
if (command === 'dmall') {
message.guild.members.cache.forEach(async (member) => {
const messageSent = await member.send(args[0])
.catch(err => {console.log(err)});
console.log(messageSent);
await wait(5000);
});
}
Tell me if its works or not.