Discord.js sent message to all member in server? - javascript

Discord.js version : 11.4.2
sent message to everyone in server Hello
if(message.content === '!Hello'){
message.sentall("Hello")
}

Since you're using Discord.js v11, I suppose that this is what you'll need :
const guild = client.guilds.get("YourGuildID");
guild.members.forEach(member => {
member.send("YourMessageToSend");
});
Make sure to replace YourGuildID with the id of the guild you want to send the message to, and do the same thing with YourMessageToSend but, with the message you want to send this time.

Related

How do I get the guild.id from the server when the bot joins discord.js v13

as the caption says I don't know how I get the guild.id from a server when the bot just joined.
I need the guild.id inside the guildCreate event.
EDIT:
My problem was a misspell in my code that didn't got tagged as an error.
Because I need the guild.id for a database entry I'm using MySQL and I was missing a "`". Thanks for all the help.
Just take the guild as a parameter to guildCreate:
client.on('guildCreate', (guild) => {
const { id } = guild;
// do something with id
});

Getting the channel name in discord.js

I'm trying to specify the channel my discord bot is posting in using the following code:
client.on("message", message => {
if message.content.includes("?chan") {
const channel = client.channels.cache.get('CHANNEL_ID_HERE')
console.log(channel)
channel.send("pong")
}
})
Upon sending the message "?chan", I want the discord bot to print whatever "channel" contains into the console, as well as send the message "pong" to the specified channel. It does neither of these things, and I'm not sure why. Also the rest of the code runs fine, including the parts with code from discord.js.

Discord.js bot Cannot read property 'send' of undefined

I am trying to implement for my discord server and my discord bot a possibility to send a PM message to a lot of users.
I created something like this:
client.on("message", message => {
mentiondm = message.mentions.users.first();
mentionMessage = message.content.slice(22)
mentiondm.send(mentionMessage);
console.log('Message Sent!')
})
It's working for first mentioned user, message is sending but there are several problems:
after sending first message bot is crashing down with this error Cannot read property 'send' of undefined
I tried to change message.mentions.users to message.mentions.roles but then message is not sending.
There is a way to do my functionality better or at least upgrade this what I have here to working properly?
It's because it tries to get the first mentioned user from the bot's message too. Check if the message author is a bot and if it is, don't send the message.
Also, try to check if there is someone mentioned and if not, simply return.
client.on('message', (message) => {
if (message.author.bot) return;
let mentionDM = message.mentions.users.first();
if (!mentionDM)
return console.log('No mentioned user');
let mentionMessage = message.content.slice(22);
mentionDM.send(mentionMessage);
console.log('Message Sent!');
});

How do I make my discord bot only read content in a certain channel

I need help making my discord bot only read messages in a certain channel
So let's say you put !+!Verify in the verification channel I want it to verify you
But lets say you put it in the general channel I want it to not verify you
You can get the channel ID with channel.id and if you get the channel from a message (message.channel.id) you can use it in an if statement to filter channels
// client.on etc...
if (message.channel.id === 'CHANNEL-ID') {
// When it is in the right channel
}

discord.js Greeting message not sending

I was following the discord.js docs and when I tried to test the greeting thing the message didn't send. The channel is right, I even tried doing it with the channel ID (with different code), I also tried just sending a direct message. No error appears, the console is just empty and the message doesn't appear.
client.on('guildMemberAdd', member => {
const channel = member.guild.channels.cache.find(ch => ch.name === 'member-log');
if (!channel) return;
message.channel.send(`Welcome to the server, ${member}!`);
});
There is no need for "cache" in guildMemberAdd, that's why the channel was not found. The message variable was also not defined.
client.on('guildMemberAdd', member => {
const channel = member.guild.channels.find(ch => ch.name === 'member-log');
if (!channel) return;
channel.send(`Welcome to the server, ${member}!`);
});
I had this same exact problem and for me it wasn't a problem in the code itself, I just gave the bot every single permission on the category of the welcome channel as well as the welcome channel itself.
It could also be that you have something else as the const name instead of 'client'.

Categories

Resources