Reputation: 1616
The scenario I need help with is this. I have several channels I want to send a message to whose names all end with the word “events” (pc events, Xbox events, etc).
Rather than typing the below code over and over again I’d like to find a way to search for all channels that include/end with the word events and then send a message to them.
const xboxEvents = bot.channels.cache.find(channel => channel.id === '111111111111111111')
const pcEvents = bot.channels.cache.find(channel => channel.id === '111111111111111111')
const psnEvents = bot.channels.cache.find(channel => channel.id === '111111111111111111')
xboxEvents.send(eventsEmbed)
pcEvents.send(eventsEmbed)
psnEvents.send(eventsEmbed)
Upvotes: 1
Views: 472
Reputation: 23161
You can .filter()
the collection by the channel's name and use the .endsWith()
method to check if the channel's name ends with a certain string. As .filter()
returns a new collection, you can iterate over it and send a message on each channel.
You can also check if the channel's type
is either text
or news
as only these have a .send()
method.
const channels = bot.channels.cache.filter(
(channel) =>
channel.name.endsWith('event') && ['text', 'news'].includes(channel.type)
);
channels.each((channel) => channel.send(eventsEmbed).catch(console.error));
If you want to check if the channel name includes the string "event", you can use .includes()
instead of .endsWith()
.
Upvotes: 1