Client Missing Intents Error (Discord.js) - javascript

So I've been working on a discord bot I made for about a year, although I stopped for a bit a couple months ago. Anyway, I was getting back into it, and last night a made a "-say (something)" command for my bot. It was working out just fine, but today, I check up on the bot again, and I start getting an error about how I didn't state valid intents, but this is my code (for the client intents).
const Discord = require('discord.js');
const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION"]});
If anyone knows what I'm doing wrong, I'd be super grateful if you could let me know.
Thanks!

const { Client, GatewayIntentBits , Partials } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
partials: [Partials.Message, Partials.Channel, Partials.Reaction]
)}
you need to use this if you are on v14

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!

DisallowedIntents if im run discord bot in terminal

const { Client, GatewayIntentBits } = require("discord.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds, //all problem here
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers
],
});
client.login("TOKEN");
You get this problem because you are accessing privileged intents. To fix this go to https://discord.com/developers/applications/ click on your bot. Then go to the section that says bot and scroll down to Privileged Gateway Intents. Enable server members and message content intent. After that it should be fixed.

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.

How to get users in voice channel without cache in DiscordJS?

I'm trying to get all users connected to a voice channel on my server. When someone talks to a bot in #general, I want to get the users inside Voice Channel 1.
I'm using Node 17 and DiscordJS 13.
This is my code:
message.guild.channels
.fetch(channelID, { cache: false, force: true })
.then((channels) => {
console.log(channels.members);
});
Also, I tried this one:
let voiceChannel = client.guilds.cache
.get(process.env.DISCORDJS_GUILD_ID)
.channels.cache.get(process.env.DISCORDJS_CHANNEL_ID);
let membersInChannel = voiceChannel.members;
console.log(membersInChannel);
But, it always returns the voice channel users that joined when I start the node app. If someone leaves the voice channel, it keeps showing him in the console.log when I say something to the bot in #general. How can I achieve this?
I've had the same problem. Although I added { force: true } as the option in fetch, it always showed the same number of members when I started the app, ignoring the ones who joined/left the channel.
Enabling the GUILD_VOICE_STATES intent solved this. So don't forget to add it to your client:
const { Client, Intents } = require('discord.js');
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_VOICE_STATES,
],
});

Event messageCreate not firing/emitting when I send a DM to my bot (discord.js v13)

I made this code for logging the DMs that are sent to my bot:
client.on('messageCreate', async message => {
if (message.author.bot) return;
const attachment = message.attachments.first()
if (message.channel.type === 'DM') {
console.log(message.content)
const dmLogEmbed = new MessageEmbed()
.setColor("RANDOM")
.setTitle(`${message.author.tag} dmed the bot and said: `)
.setDescription(message.content)
.setFooter(`User's id: ${message.author.id}`)
if (message.attachments.size !== 0) {
dmLogEmbed.setImage(attachment.url)
}
client.channels.fetch("852220016249798756").then((channel) => {
channel.send({ embeds: [dmLogEmbed] })
})
}
});
But when updating to discord.js v13 it didn't work anymore, for what I understood the only change is that the 'dm' channel type isn't 'dm' anymore but 'DM', so I changed it in my code, but it is still not working and I don't really know why.
Make sure you have DIRECT_MESSAGES in your client's intents.
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES"] });
There is a breaking change in the Discord API v8. For reference see here.
On Discord API v8 and later, DM Channels do not emit the CHANNEL_CREATE event, which means discord.js is unable to cache them automatically. In order for your bot to receive DMs, the CHANNEL partial must be enabled.
So we need to enable the partial CHANNEL as well. Keep in mind that when dealing with partial data, you often want to fetch the data, because it is not complete. However this doesn't seem to be your case, if you are using the messageCreate event. The message received is not partial nor message.channel. To check if something is partial, you can use the .partial property. For example Message.partial or Channel.partial.
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES"], partials: ["CHANNEL"] });
Now it should work as good old discord.js v12.

Categories