简体   繁体   中英

Dm everyone in a function and sends a complete message when it's done

I want to send all the members in a server every 10 min a dm in a specific server. And when it send all the members it wil send a complete message in the console.

  • But it says member.send is not a function
  • And I want also in a specific server
if (command === `${prefix}dmall`) {

    var list = bot.guilds.array();
    sendMessage(list);
  }
});



function sendMessage(list) {
  setTimeout(function () {
    for (i = 0; i < list.length; i++) {
      let member = list.splice(Math.floor(Math.random() * list.length), 1);
      member.send("Test")
      list.length = list.length - 1;

      if (list.length = 0)
        console.log("Done")
    }


    sendMessage(list);
  }, 10 * 1000);

}

bot.login(botconfig.token);

In you code list its not a list of guild members, its array with 1 guild collection. So when you try to send message, you try send it to array. If you have the server ID where you want to send the dm message, you can do it like this

if (command === `${prefix}dmall`) {

    let dmGuild = bot.guilds.get('GUILD ID HERE')
    dmGuild.members.map(member => {
        member.send('YOUR MESSAGE')
        .catch('Member not allowed to recive DM message from this server')
    })    
});
if (command === `${prefix}dmall`) {
   let myGuild = bot.guilds.get('YOU guild HERE')
     let list = myGuild.members.map(member => member.user.id)
    sendMessage(list, myGuild);
  }
});



function sendMessage(list, myGuild) {
  for (var i = 0; i < list.length; i++) {
    setTimeout((function(index){ 
      return function() {
                var member = myGuild.members.get(list[index])
                if(!member.user.bot && member) {
                    member.send('asdsadsa').catch(console.log('Member not allow to send him DM message'))
                }
        if(index === list.length-1) console.log('done')
      };
    })(i), 10000 * (i + 1))
  }
}

bot.login(botconfig.token);

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