I am trying to implement a function that will be sending PM to every member of channel.
How to do that? I have that fragment responsible for sending PM to user, but how to get every user from channel?
Theres a built-in function called forEach() in Javascript . With this you can send a DM to all members in your server with DMs open
Here is an example
client.on("message", message => {
if (message.content === "!dmall")
message.guild.members.cache.forEach(member => {
member.send(`Hello`).catch(e => console.error(`Couldn't DM member ${member.user.tag}`))
})
Related
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}`);
});
I've made a code which greets new members in a particular channel but I want to toggle it.
If a staff member uses !greet #channel then greet messages will be enabled in #channel. And if they use !greet, it disables greet message for the server.
GuildMemberAdd Event
client.on('guildMemberAdd', member =>{
const channel = member.guild.channels.cache.find(ch => ch.name == 'welcome');
if(!channel) return;
channel.send(`Welcome to **${member.guild.name}**, ${member}!`)
.then(message =>{
message.delete({ timeout: 5000 })
})
.catch(console.error);
});
More Info
I just want to make it a toggle command and I'm not sure how to. I'd be glad for an early answer.
So to toggle which channel will be send welcome message you need to store the channel ID somewhere like database or attach it with guild through collection .set . After that in your event before send message you get information from guild ID if it already set welcome channel so send message to that channel otherwise don't do anything
I'm making a bot to detect when a user joined. The code below does not work when my test account joins. Does it only work when it's the first time a user joins. If so, how can I make it work everytime.
client.on('guildMemberAdd', member => {
// Send the message to a designated channel on a server:
const channel = member.guild.channels.cache.find(ch => ch.name === 'general-chat');
// Do nothing if the channel wasn't found on this server
if (!channel) return;
// Send the message, mentioning the member
channel.send(`Welcome to the server, ${member}`);
});
You might have to enable Presence Intent on your bot settings on the Discord Developer Portal, which makes it able to detect new members.Here's an screenshot.
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)
})
I'm looking a way to send a private message to à group of users, who have the same role (using discord.js)
I found the way to send a message (client.users.get("ID").send("Message");
But not the way to get all member who have the same role and loop on that list to send them a private message. Someone can help me?
You could do so by first making a list of all the members with the desired role (see Collection.filter()), and then loop through (see Map.forEach()) and send a DM to each member. Check out the code below for an example.
// Assuming 'guild' is defined as the guild with the desired members.
const roleID = ''; // Insert ID of role.
const members = guild.members.filter(m => m.roles.has(roleID) && m.user.id !== client.user.id);
members.forEach(member => {
member.send('Hello there.')
.catch(() => console.error(`Unable to send DM to ${member.user.tag}.`));
// The above error would most likely be due to the user's privacy settings within the guild.
});