Message all members, updated code – with counter

Message all members, updated code – with counter

The code snippet from yesterday, just got an upgrade!
I wanted to include a counter, to tell how many messages that has been sent. I thought nothing of it and included a counter like I have before, only to get the result 0 EVERY time, no matter how I wrote and configured the code. Finally frustrated me enough to ask StackOverflow, but didn’t get any closer to a solution.

After hours of reading and testing I resorted to asking a friend who is eons ahead of me in coding, and he came up with a different way of doing it, but the result was the same amount of sent messages, only this time it said so with a counter and I didn’t have to manually check.

    async execute(message, args) {
        const dato = new Date();
        dato.setHours(dato.getHours() + 2);

        if (!message.member.hasPermission(`ADMINISTRATOR`)) {
            message.channel.send(`You don't have permission to use this command!`);
            message.client.channels.cache.get(logchannel).send(`${message.author} tried using DMALL in **${message.guild.name}**!` + dato.toLocaleTimeString() + dato.toLocaleDateString());
            return;
        } else {
            const delay = (msec) => new Promise((resolve) => setTimeout(resolve, msec));
            const sendMessage = args.join(" ");
            message.channel.send(`Sending messages, please wait...`);
            await delay(1000);
            const interval = 500;
            let array;
            try {
                const all = await message.guild.members.fetch();
                array = all.array();
            } catch {
                array = message.guild.members.cache.array();
            }
            let sentMessages = 0;
            for (var i = 0; i < array.length; i++) {
                const user = array[i];
                if (!user.bot) {
                    try {
                        await user.send(sendMessage);
                        sentMessages++;
                        await delay(interval);
                    } catch { }
                }
            }
            message.client.channels.cache.get(logchannel).send(`${message.author} sent a DM to all members of: **${message.guild.name}**!` + dato.toLocaleTimeString() + dato.toLocaleDateString());
            message.channel.send(`Finished sending messages to all users, ${sentMessages} messages sent!`);
        }
    }

This code even has a try/catch clause for getting the members of a guild, and I included a !user.bot if() statement.
Not only is this code better, it’s also easier to understand! No forEach(function(promise)))… blah! I need to get started learning these try/catch ways of coding.

Sitting here I’m thinking of something… How can we catch (get it) the times a message isn’t sent? Can we simply do this?

            let sentMessages = 0;
            let failedMessages = 0;
            for (var i = 0; i < array.length; i++) {
                const user = array[i];
                if (!user.bot) {
                    try {
                        await user.send(sendMessage);
                        sentMessages++;
                        await delay(interval);
                    } catch { failedMessages++; }
                }
            }
            message.channel.send(`Finished sending messages to all users, ${sentMessages} messages sent; ${failedMessages} messages failed!`);

I haven’t tested this latest iteration because I have no real way of testing it, but it should work.

Right… Back to Visual Studio Code!

Leave a Reply

Your email address will not be published. Required fields are marked *