how to send a message to every channel in every guild? - discord

similar to this question however I want to be able to send it to every channel it has access to!
inside the on message event after I verify myself by ID and the issued command I was using this code:
const listedChannels = [];
msg.guild.channels.forEach(channel => {
//get all channels
client.channels.get(channel.id).send("you like bred? (message) ");
//send a message to every channel in this guild
});
however I get the error that .send is not a function...
I have been told to use .send after getting the ID of the channels

If you are looping through all of the channels, you simpily need to send your content to the channel, which you've already gotten from msg.guild.channels.forEach(channel => {//code}).
Replace what you have inside the .forEach block with;
channel.send("You like bred? (message)");
Although this will send You like bred? (message)
If you're trying to get a response back, perhaps look at this answer that explains collecting responses via reactions to a discord message.

The following explanation pertains only to v11 (stable).
Client.channels is a Collection of Channels your bot is watching. You can only send messages to text channels, and this Collection will include DM channels as well. For this reason, we can use Collection.filter() to retrieve a new Collection of only text channels within a guild. Finally, you can iterate over the channels and call TextChannel.send() on each. Because you're dealing with Promises, I'd recommend a Promise.all()/Collection.map() combination (see hyperlinked documentation).
For example...
// assuming "client" is your Discord Bot
const channels = client.channels.filter(c => c.guild && c.type === 'text');
Promise.all(channels.map(c => c.send('Hello, world!')))
.then(msgs => console.log(`${msgs.length} successfully sent.`))
.catch(console.error);

You can use client.channels for this. Check if channel type is guild text channel and then try send a message.
client.channels.forEach(channel => {
if(channel.type === 'text') channel.send('MSG').catch(console.error)
})

Related

Discord js getting a bot's message

Well i need to do this:
on a channel a bot announces "btc" price (not real)
im trying to get the price and send the price to a specificed channel
My code
sa.on("message", (message) => {
if (message.content.startsWith("🟠")) {
if (message.author.id == "974187708933083157") {
client.channels.get('955536998989447249').send(`${message.content}`);
} else message.channel.send(``);
}
})
So if a message that starts with 🟠 the bot needs to get the price and send it to the channel (955536998989447249)
But it not working my bot is working fine but not getting price and sendimg it
Firstly don't try sending empty message message.channel.send(``);, it will just throw an error.
The main problem, is that client.channels (ChannelManager) doesn't have a "get" method.
Assuming you are using djs v13, to get a channel, you can get it from cache with:
client.channels.cache.get('955536998989447249').send("sth")
however this might not always work if the channel is not currently cached.
Other option is to fetch it, though it is an asynchronous operation, so it looks a bit different:
client.channels.fetch('955536998989447249').then(channel => channel.send("sth"))

I need a code that sends a message every time a channel is created

I want my bot to send the message "first" every time a channel is created
the closest I could get was at Discord JS : How can ı send message when create channel by bot but I don't want to that he sends the message only on channels he created himself, but on channels created by anyone
You can use the channelCreate event.
Here's a simple example for you:
client.on("channelCreate", async (channel) => {
const channelName = channel.name;
console.log(`New channel created: ${channelName}`);
});

When a member joins a server, i want to ping them then delete it (JS)

I would like to ping a user in a channel (to alert them to it) then delete the message.
I have seen this on many large discord servers, they use custom bots for it so I think it wouldn't be too hard!
Inside your guildMemberAdd event get, the channel you want to send the ping in, then do channel.send(`${member.user}`).
Here, member is the argument you gave to the callback function in the event. member.user will ping them in that channel.
send() method returns the message you sent as a promise, which means you can just catch that message and delete it like this: send().then(message => message.delete()).
You can provide timeout as an optional parameter to the delete() method if you want to delete the message after a specific period of time and not instantly. This is what the whole code will look like:
bot.on('guildMemberAdd', (member) => {
const channel = member.guild.channels.cache.get('id'); // get the channel using the id
channel.send(`${member.user}`)
.then(message => message.delete());
}

How do I send message to different channel at the same time in discord.js?

My goal is to send a message to a channel that have the name global-chat.
I tried:
const Discord = require("discord.js")
const client = new Discord.Client();
client.on("message", async(msg) => {
if(msg.channel.name !== "global-chat")return;
let message = msg.content
await client.channels.find("name", "global-chat").send(message)
})
but when I send message in the global-chat channel in one server, it won't send to global-chat on other servers. Can anyone help me fix this?
If I understand your question correctly, you want to send a message to the channel with the same name in all other guilds.
This line will simultaneously relay the message to all of the channels the bot is watching named global-chat. Here's how it works...
Promise.all() executes Promises in a parallel manner.
Client.channels is a Collection of channels the bot is watching. Using Collection.filter(), you can form a new Collection with just the channels named global-chat.
Collection.map() returns an array of the values returned by the function provided for each value in the Collection. In this case, it returns an array of Promises for Promise.all() to use.
await Promise.all(client.channels.filter(c => c.name === 'global-chat').map(c => c.send(msg.content)))
If you use discord.js v12, you'll need to replace client.channels.filter by client.channels.cache.filter.

How to fetch a message from the entire guild

I would like to know how to find a message with its id from the entire guild and not just the current channel.
I have tried
message.guild.channels.fetchMessage(message_ID)
client.channels.fetchMessage(message_ID)
message.channel.guild.fetchMessage(message_ID)
message.fetchMessage(message_ID)
message.guild.fetchMessage(message_ID)
I want it to look through the entire guild and find the message by ID then send what the message has said. Also, I have tried to have it look for the channel that I want the message from but that didn't work either.
If it's possible to get more specific with the code, it should look into a channel called #qotd-suggestions with the ID of 479658126858256406 and then find the message there and then send the message's content.
Also, the message_ID part of fetchMessages gets replaced with what I say after the command so if I say !send 485088208053469204 it should send Message content: "Wow, the admins here are so lame."
There's no way to do that with a Guild method, but you can simply loop through the channels and call TextChannel.fetchMessage() for each of them.
In order to do that, you could use Collection.forEach() but that would be a little more complex if you want to work with vars, so I suggest you to convert Guild.channels to an Array with Collection.array().
Here's an example:
async function findMessage(message, ID) {
let channels = message.guild.channels.filter(c => c.type == 'text').array();
for (let current of channels) {
let target = await current.fetchMessage(ID);
if (target) return target;
}
}
You can then use this function by passing the message that triggered the command (from which you get the guild) and the ID you're looking for. It returns a Promise<Message> or, if the message is not found, Promise<undefined>. You can get the message by either using await or Promise.then():
let m = await findMessage(message, message_ID); // Message or undefined
// OR
findMessage(message, message_ID).then(m => {/* your stuff */});

Resources