A
A
anubis4652021-03-13 15:47:21
Node.js
anubis465, 2021-03-13 15:47:21

How to send messages to the first channel on all Discord servers?

I want to do something like alerts for servers that have a bot. So that I can write one command to send a message to the server through the bot.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-03-13
@anubis465

client.guilds.cache.array().forEach(guild => {
    guild.channels.cache
        .array()
        .filter(c => c.type !== "category" && c.type !== "voice" && c.position === 0)[0]
        .send("сообщение")
})

Don't forget two important things:
  1. the first text channel on the server may have the setting "Prohibit server members from sending messages to the channel".
    will throw an error:
    UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Permissions

  2. the server may not have text channels.
    will throw a fatal error:
    TypeError: Cannot read property 'send' of undefined


make checks to avoid such cases.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question