Search code examples
node.jsserverdiscorddiscord.jsbots

Sending a message into all servers where bot is


How do I send one message per guild that my bot is in? It doesn't matter which channel it will be, but I don't know how to code it.

I finally came up with that idea:

client.guilds.cache.forEach(guild => { 
            const Embed = new discord.MessageEmbed();
            const Channels = guild.channels.cache.map(channel => `${channel.id} | ${channel.name}`).join(", ")
    
            Embed.setTitle(`Channels for ${guild.name}`);
            Embed.setDescription(Channels);
    
            message.channel.send(Embed); 
        });

but it doesn't work.

I'm working with commands handler so it has to be done with module.exports:


module.exports = {
  name: "informacja",
  aliases: [],
  description: "informacja",
  async execute(message) {
    client.guilds.cache.forEach(guild => { 
            const Embed = new discord.MessageEmbed();
            const Channels = guild.channels.cache.map(channel => `${channel.id} | ${channel.name}`).join(", ")
    
            Embed.setTitle(`Channels for ${guild.name}`);
            Embed.setDescription(Channels);
    
            message.channel.send(Embed); 
        });
};

My code is not working at all

When I run it - nothing happens, no errors, no messages, just nothing.

tried to logged everything, but still nothing came up.

I also tried to log everything using console.log() but nothing is working

I remind: I need to send one message to all servers where my bot is, but only one channel


Solution

  • You could find a channel on each server where the client can send a message then send:

    client.guilds.cache.forEach(guild => {
        const channel = guild.channels.cache.find(c => c.type === 'text' && c.permissionFor(client.user.id).has('SEND_MESSAGES')) 
        if(channel) channel.send('MSG')
           .then(() => console.log('Sent on ' + channel.name))
           .catch(console.error)
        else console.log('On guild ' + guild.name + ' I could not find a channel where I can type.')
    
    })