Send Message To All Servers Discord.Js

Source URL: https://stackoverflow.com/questions/47781264/how-do-you-make-a-bot-send-a-message-to-every-server-it-is-in-discord-js

Language: javascript

Created on: 2021-01-03 18:21:08

// Using v11.2 of discord.js, should work with v12 too.

const discord = require('discord.js');
const client = new discord.Client();

// So you can see when it's ready
client.on('ready', () => {
    console.log('On Discord!');
});

client.on('message', () => {
    if (message.content === "!sendguildmessages") {
        var guildList = client.guilds.array();
        try {
            guildList.forEach(guild => guild.defaultChannel.send("messageToSend"));
        } catch (err) {
            console.log("Could not send message to " + guild.name);
        }
    }
});
Can anyone help me? I don't know how to do this, but I want to make my bot send a message to every server it is in and if it doesn't have perms to do it, it can just ignore it. How do you do it?
content_copy