【发布时间】:2019-10-11 10:57:40
【问题描述】:
我正在尝试让我的机器人 DM 成为所有人(不仅仅是新成员),但我不知道我将如何去做。任何帮助将不胜感激。
【问题讨论】:
标签: javascript discord.js
我正在尝试让我的机器人 DM 成为所有人(不仅仅是新成员),但我不知道我将如何去做。任何帮助将不胜感激。
【问题讨论】:
标签: javascript discord.js
// Async context (within async function), 'message' being the command message.
const members = message.guild.members.filter(m => !m.user.bot).array(); // Filter out bots.
let undelivered = 0;
for (let i = 0; i < members.length; i++) { // Using an array and a for loop rather than
const member = members[i]; // Collection.forEach() due to the fact that
await member.send('Hello there.') // the latter will move onto the proceeding
.catch(() => undelivered++); // code before waiting for the promises to
} // fulfill. https://stackoverflow.com/a/37576787
message.channel.send(`Messages sent. ${undelivered} members couldn't receive it.`)
.catch(console.error);
【讨论】:
async function(...) { 或async (...) => {)。
bot.on('ready', async () => {。您还需要将message.guild 更改为您要使用的公会(即client.guilds.get('id'))。
Collection.find()搜索具有特定属性的公会,即client.guilds.find(g => g.name === 'test')。
message.guild.members.filter(...).forEach(async member => { // code })代替for循环