简体   繁体   中英

Is it possible to send a message to every guild my bot is in?

I have tried every tutorial but they don't work, here is my current code:

bot.on('message', message => {
    if (message.content.startsWith(`${prefix}globalannounce`)) {
        var msg = message.content.split(" ").slice(1).join(" ")
        var guildList = bot.guilds.array;
        try {
            let messageToSend = new Discord.MessageEmbed()
                .setTitle("Hello, you don't see me messaging in your server often...")
                .setDescription(`I have just flown in to tell you that my developers have something to say: \n ${msg}`)
            guildList.array.forEach(channel => {
                if (channel.type === 'text') channel.send(messageToSend).catch(console.error)
            });
        } catch (err) {
            console.log(err);
        }
    }
});    

It will not work and the error is TypeError: Cannot read property 'array' of undefined .

If you're using discord.js v12

You have the error TypeError: Cannot read property 'array' of undefined . This means that bot.guilds is equal to undefined . So the problem is here :

        var guildList = bot.guilds.array;

You'll have to replace it with

var guildList = bot.guilds.cache

So your entire code would look like this :

bot.on('message', message => {
    if (message.content.startsWith(`${prefix}globalannounce`)) {
        var msg = message.content.split(" ").slice(1).join(" ")
        var guildList = bot.guilds.cache
        try {
            let messageToSend = new Discord.MessageEmbed()
                .setTitle("Hello, you don't see me messaging in your server often...")
                .setDescription(`I have just flown in to tell you that my developers have something to say: \n ${msg}`)
            guildList.forEach(guild =>{
guild.channels.cache.find(c => c.type === 'text').send(messageToSend)
            });
        } catch (err) {
            console.log(err);
        }
    }
});    

discord.js v12.x uses Managers , so you'll have to go through the cache property to get a list of guilds. See this post for more information.

Also, GuildList.forEach() would iterate a function throughout all Guilds in the collection, not all the channels. You can use the Channel.type property and Collection.find() to find the first text channel available.

var guildList = bot.guilds.cache; // go through the cache property
try {
 let messageToSend = new Discord.MessageEmbed()
  .setTitle("Hello, you don't see me messaging in your server often...")
  .setDescription(
   `I have just flown in to tell you that my developers have something to say: \n ${msg}`
  );
 guildList.forEach((guild) => {
  const channel = guild.channels.cache.find((channel) => channel.type === 'text') // try to find the channel
  if (!channel) return; // if it couldn't find a text channel, skip this guild
  channel.send(messageToSend); // otherwise, send the message
 });
} catch (err) {
 console.log(err);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM