Toggle Greet command for discord.js v12 - discord.js

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

Related

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

my bot don't see event "guildMemberAdd" why?

he don't view new member and don't write on console "nouveau membre"
Client.on("guildMemberAdd", (member, guild) => {
console.log("nouveau membre");
})
You need to have SERVER_MEMBERS_INTENT in order to receive the guildMemberAdd event.
If you have a verified bot then it needs whitelisting, if not then just go to your application and to the bot tab and toggle the server members intent

How do I make my discord bot auto respond to any message

What I am trying to do is set up an autoresponse discord bot that adds a reaction to any message that is being sent,in a specific channel. I cannot figure out why it is not showing up in discord.
It's very simple, just add a message event listener, validate if the channel id is the one you want it to be, and then use the .react method. For example:
client.on('message', message => {
if (message.channel.id !== 'channel id here') return;
message.react('emoji here')
// if you want to use a custom emoji put it's id there instead.
})

Discord.js Guild Member Add

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.

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

Resources