Members arent cached - javascript

I'm trying to make a selfbot and before receiving messages saying that's against Discord ToS, I take that risk. My bot sends messages only to people it already has a DM channel opened with, probably because members aren't cached.
I want my bot to send message to everyone in my server. How do I cache members on a selfbot?
Code:
const guild = client.guilds.cache.get("id");
if(!guild) return;
guild.members.fetch();
guild.members.cache.random().createDM().then((dm => {
dm.send("Dont forget to verify !").catch(e => console.log('error'))
})).catch(() => {});
console.log("message sent");

Server Members Intent Image
Image above shows example to this answer;
Did you enable SERVER MEMBERS INTENT when creating your Discord bot?
https://discord.com/developers/applications/"ClientId"/bot
And/or did you create a new discord client with the GUILD_MEMBERS intent?
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.GuildMembers
]
});
And whether or not you know the risks, you should NOT be using self bots.

Related

How can I make bot receive DM on Discord js v14? [duplicate]

This question already has an answer here:
message.content doesn't have any value in Discord.js
(1 answer)
Closed 4 months ago.
module.exports = {
name: 'messageCreate',
execute(message) {
if (message.channel.type == 'DM') {
console.log('Dm recieved')
client.channels.get('1026279100626767924').send(message);
}
}
};
I created event handling by referring to the Discord JS guide. I want my bot to receive a DM and send that message to my admin channel.
But the bot is not recognizing the DM
What should I do
(I'm using a translator)
You can use messageCreate event for listening your bot's DMs.
client.on("messageCreate", async message => {
if (message.guild) return;
console.log(`Someone sent DM to me => ${message.content}`);
await client.channels.cache.get(CHANNEL_ID).send(messsage.content);
});
You need to include partials Channel and Messages configurations on creating client:
const client = new Client({
intents: [
GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent
],
partials: [
Partials.Channel,
Partials.Message
]
})
That solved it for me!
There could be a couple problems with this.
First off, it could be your Intents.
Intents allow your bot to process certain events.
The intents that you will want are:
DirectMessages
MessageContent (this one may not be required, I am unsure).
So, go to where you initialize your client. It should look something like this:
const Discord = require("discord.js");
const client = new Discord.Client();
and change it into this:
const Discord = require("discord.js");
const { GatewayIntentBits, Client } = Discord;
const client = new Client({
intents: [
GatewayIntentBits.DirectMessages
GatewayIntentBits.MessageContent
]
});
The second issue could be that your message handler isn't detecting it properly.
Like the other answer says, you should just determine if there is no Guild attached to the message.
client.on("messageCreate", async(message) => {
if(!message.guild) {
client.channels.cache.get("YOUR_ID").send(message);
}
});
There could be other issues, but those two are the most probable. Considering you're not getting any errors, I'd presume it's the first one, the issue with Intents.
Hope this helps!

Discord.js bot not logging in

I made a discord bot and i have no idea what's wrong with my code i mean look:
const Discord = require("discord.js");
const client = new Discord.Client({
intents: [
"GUILDS",
"GUILD_MESSAGES"
]
});
client.login('you thought haha')
I tried various tutorials but it's still not working
It's looking like about the intents and there's simple mistake as i've seen. If you wan't to open all intents i recommend you this;
const client = new Discord.Client({
intents: new Discord.Intents(32767)
});
If you wan't specific intents then you should make them like;
const client = new Discord.Client({
intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES]
})
Also don't forget to open your intents from bot section on your developer panel!
More info here on docs.
I'm assuming you have already installed Node.js and discord.js, and also have a bot setup.
If you check discord.js Guide, they provide you the following code to start your bot:
// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
// Login to Discord with your client's token
client.login(token);
Then, open your terminal and run node index.js (assuming that your file name is index.js) to start the process. If you see "Ready!" after a few seconds, you're good to go!
I strongly recommend for you to follow this guide, it helped me a lot with the first steps with the discord bot.
Also, make sure that you have the right scopes selected for your bot:
I think the error is on the client declaration. You have to capıtalize the first letter of "intents". So the code becomes:
const Discord = require("discord.js");
const client = new Discord.Client({
Intents: [
"GUILDS",
"GUILD_MESSAGES"
]
});
client.login("your token here");
And also please make sure that on your discord developers page you have your intents turned on.

discord bot is not sending welcome massage

I want to send a welcome message to a new user but the code doesn't work. I tried defiant articles and video tutorials and already asked questions for help but it's not working. I already checked ( Privileged Gateway Intents > PRESENCE INTENT ) and ( Privileged Gateway Intents > SERVER MEMBERS INTENT ) here
here's my code
// Instantiate a new client with some necessary parameters.
const client = new Client(
{ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] }
)
const channelId = "969129834682929212";
const rulesChannel = "969129988299304960";
client.on("guildMemberAdd", (member) => {
console.log(member);
const message = `Welcome <#${
member.id
}> to our server! Be sure to check out our ${member.guild.channels.cache
.get(rulesChannel)
.toString()}`;
const channel = member.guild.channels.cache.get(channelId);
channel.send(message);
});
I think you are facing this issue is because of recent gateway changes of the Discord Api that you need to enable the intents. Here is a fix for you –
Head over to Discord Developers Portal
Choose your application
Inside the bot section if you scroll a little bit down , you will see a section named Privileged Gateway Intents
Enable the SERVER MEMBERS INTENT and restart the bot and your bot will start recieving the guildMemberAdd event!
Learn more about intents at Discord.js

Get presence/activity of mentioned user

I'm trying to create a command for my bot in Discord JS that when triggered, takes the command's mentioned user and retrieves their current game (If no game, then status) and sends a message saying Mentioned user has been playing Game for Time elapsed (And if possible, send the image of the game if it has one).
This is my current code:
module.exports = {
name: 'game',
aliases: ['gm', 'status'],
category: 'Commands',
utilisation: '{prefix}game',
execute(client, message) {
let msgMention = message.mentions.members.first() || message.guild.member(message.author)
console.log(msgMention.presence.activities[0])
message.channel.send(msgMention.presence.activities[0].name);
},
};
All it does with the .name extension says name is undefined and if I remove .name it says cannot send an empty message. Also, I mention myself currently playing a game that so shown publicly on my discord activity status. What can I do so it actually reads the name and time of the activity to then send it?
You need the GUILD_PRESENCES intent.
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_PRESENCES] });
Also btw I'd recommend using message.member instead of message.guild.member(message.author)
let msgMention = message.mentions.members.first() || message.member

Why am I getting an undefined when trying to get a Guild in Discord.js?

I'm pretty new to working on Discord bots, but I'm trying to create a bot for Discord that gets all the members in a guild, so they can be used later on. But everytime I try to get the guild with its ID it gives an undefined back, even though I know I put in the right ID.
const Discord = require("discord.js");
const client = new Discord.Client();
const members = client.guilds.cache.get("guildID");
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
console.log(members);
});
client.login('token');
Am I doing something wrong?
You aren't being very clear. You stated you want your bot to get all members in a guild, but you proceed to get a guild ID instead.
Assuming you want to get all members in a guild. First thing you need is Privileged Gateway Intents enabled in your bot and implemented in your code.
Step 1: Enable Intents
Go to your Bot App Dev
Select your bot
Head to the Bot section of your bot and scroll down till you see "Privileged Gateway Intents" and select both "PRESENCE INTENT" and "SERVER MEMBERS INTENT
"
Example
Step 2: Implementing In Code
const Discord = require('discord.js');
const client = new Discord.Client({ ws: { intents: new Discord.Intents(Discord.Intents.ALL) }});
^^^^^^^
This is what is making you access all server members within a guild
Code for getting all server members:
// If prefix not set: do this: const prefix = "!"; whatever you want
const prefix = "!";
// Usernames
const members = message.guild.members.cache.map(member => member.user.tag);
// ID's
const members = message.guild.members.cache.map(member => member.user.id);
if(message.content.startsWith(prefix + "test")){ // Gets all members usernames within the guild this message was sent on.
console.log(members) // Logs all each members username
}
In depth but I hope it helps.
I believe it's just:
client.guilds.get("guildID");
and not:
client.guilds.cache.get("guildID");
Try using this instead:
client.guilds.fetch("guildID");
Also, I recommend you put that line in your ready event or somewhere where you are sure the client has fully logged in when ran to make sure that it is logged in when you run that.
Before client is ready, use client.guilds.fetch('<guild id>') which returns a promise. Note that before the ready event is triggered many things on the client are uninitialized such as client.user
Put the const members = client.guilds.cache.get("guildID"); inside the ready event.
Try:
const members = client.guilds.cache.get("guildID");
list.members.cache.forEach(member => console.log(member.user.username));

Categories