如何DM所有服务器成员不和谐(JS)

时间:2020-06-15 13:54:31

标签: javascript node.js discord discord.js

我想尝试使用不和谐的bot并使用以下代码对所有服务器成员进行dm:

if (message.content === 'tdmall') {


    const members = async function() {

        message.guild.members.filter(m => !m.user.bot).array();

        let undelivered = 0;

        for (let i = 0; i < members.length; i++) {

            const member = members[i];

            await member.send('Hello there.')
                .catch(() => undelivered++);
        }

        message.channel.send(`Messages sent. ${undelivered} members couldn't receive it.`)

            .catch(console.error);

    }
}

我的机器人程序不会dm每个服务器成员,但是也不会记录错误,因此您可以帮我吗?

这是我按照某人的建议编辑的版本

1 个答案:

答案 0 :(得分:1)

您正在声明异步函数,但实际上并没有调用它。在声明后添加await members();,例如:

if (message.content === 'tdmall') {

    // Declare function
    const messageAllMembers = async function() {

        message.guild.members.filter(m => !m.user.bot).array();

        let undelivered = 0;

        for (let i = 0; i < members.length; i++) {

            const member = members[i];

            await member.send('Hello there.')
                .catch(() => undelivered++);
        }

        message.channel.send(`Messages sent. ${undelivered} members couldn't receive it.`)

            .catch(console.error);

    }

    // Invoke function
    await messageAllMembers();
}