How to limit messages a bot can send to 1 user - discord

I have a small bit of code, it's supposed to get all guilds the bot is in, and send 1 dm to the discord guild owner. But if the owner owns 2 servers with the bot in it the bot will send 2 messages. My question is how do I limit it to only sending 1 message to the owner?(https://media.discordapp.net/attachments/573277474721366036/575445224751366145/unknown.png)
client.guilds.forEach(guild => {
client.users.get(guild.ownerID).send('test');
});

One simple solution would be to have an array containing the users already messaged, and only send those not in the array a message. For instance...
const done = [];
client.guilds.forEach(guild => {
if (!done.includes(guild.ownerID)) {
client.users.get(guild.ownerID).send('test')
.catch(err => console.error(err));
done.push(guild.ownerID);
}
});

Related

Discord.js bot command to take users with a specific role and to return an embedded message with all the users in that role

I've been working on this and I cannot figure out why the bot will not enact the command. Essentially this is how the situation should work:
In the channel you send a message: "!listroletest"
After that, the bot should send an embedded with every user who has the role "test" with their userID's
client.on("message", message => {
if(message.content == "!listroletest") {
const ListEmbed = new Discord.MessageEmbed()
.setTitle('Users with the test role:')
.setDescription(message.guild.roles.get('901592912171765850').members.map(m=>m.user.tag).join('\n'));
message.channel.send(ListEmbed);
}
});

Toggle Greet command for discord.js v12

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

Discord bot sends replay when a specific user sends a message

userID = "7645876345"
client.on("message", function(message) {
if (message.author.id === userID) {
message.react('test');
}
});
I'm trying to get the discord bot to reply to a SPECIFIC user every time he writes something.
I changed the userID to random numbers for this post.
I figured it out! I replaced message.react with message.channel.send()

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

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)
})

Sending private message to a group of user (Discord.js)

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.
});

Resources