TypeError: Cannot read properties of undefined (reading 'FLAGS') discord.js - discord

so this is my bot code
const Discord = require("discord.js")
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES] });
client.on("ready", () => {
console.log('Logged in as ${client.user.tag}!')
console.log("Bot is online!")
})
client.on("message", msg => {
if (msg.content === "ping") {
msg.reply("no1se");
}
})
client.login("my token")
The error i get is this:
TypeError: Cannot read properties of undefined (reading 'FLAGS')
at Object.<anonymous> (/home/runner/no1seAlerts-1/index.js:4:47)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
Hint: hit control+c anytime to enter REPL.
(node:2004) ExperimentalWarning: stream/web is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
yea so im trying to make a discor dbot everytime when i try to run it and make it online i get this error code i have no idea what to do and i would really appreciate any help thank you!

Assuming you are using discord.js v13, you can try and change your client to:
const client = new Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })

You should replace Intents with IntentsBitField
In your example it would be:
const client = new Client({ intents: [IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMessages] });
This was changed with version 14 of discord.js I believe.
Alternatively you can use GatewayIntentBits:
const client = new Client({ intents: [GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages] });
More information here
Hope this helps!

Related

DiscordJS v14 guildCreate send message error

I'm making an event on discordjs v14 that when the bot added it send a message to a channel where the bot can send message/open channel but I'm getting this error after it got added...
Cannot read properties of undefined (reading 'has')
My code:
const embed = new EmbedBuilder()
.setDescription('Hi')
.setColor('Blue')
let channel = guild.channels.cache.find(
channel =>
channel.type === ChannelType.GuildText &&
channel.permissionsFor(guild.me.has(PermissionsBitField.Flag.SendMessages))
);
channel.send({ embeds: [embed] })
I've tried:
const embed = new EmbedBuilder()
.setDescription('Hi')
.setColor('Blue')
let channel = guild.channels.cache.find(
channel =>
channel.type === ChannelType.GuildText &&
channel.permissionsFor(guild.me).has(PermissionsBitField.Flag.SendMessages)
);
channel.send({ embeds: [embed] })
To make the bot send a message to a channel/opn channel whenever it got added to the server
In v14, the Guild#me property has been moved to the GuildMemberManager.
channel.permissionsFor(guild.members.me)

How can a bot read embed messages sent by another bot and post a message if it contains a specific content?

I'm trying to code a Discord bot that would read embed messages sent by another bot and ping a specific role if the embed contains '(FREE)' in the title, as displayed here:
https://i.stack.imgur.com/kPsR1.png
Unfortunately, my code produces nothing. I don't run into any error, the bot simply doesn't post a message when conditions are matched to do so — yet it is online and has the permissions to post messages in the channel.
Any help would be really appreciated. I went through all the questions related to this topic on SO, but I couldn't find a working solution to my issue. I'd like to mention that I'm a beginner in JS.
Thank you so much.
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_WEBHOOKS"] })
client.on('ready', () => {
console.log(`Logged in...`);
});
client.on('messageCreate', (message) => {
if (message.embeds) {
const embed = message.embeds[0]
if (embed.title === '(FREE)') {
return message.channel.send('#Free mint')
}
}
})
Edit: I believe my question is different from the one suggested in the comment as I'm looking for specific content in the embed.
I managed to make the bot work with the following code, thanks #Gavin for suggesting it:
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_WEBHOOKS"] })
client.on('ready', () => {
console.log(`Logged in...`);
});
client.on('messageCreate', message => {
for (let embed of message.embeds) {
if (embed.title.includes('FREE')) {
return message.channel.send("<#&" + roleId + ">")
}
}
});

Discordjs TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client

I'm making a bot for ddiscord and I can't solve this error:
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client.
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const config = require('./config.json')
const dotenv = require('dotenv')
dotenv.config()
const privateMessage = require('./private-message');
const test = require('./test.js')
const mySecret = process.env['TOKEN'];
client.on('ready', () => {
console.log(`${client.user.tag} has logged in.`);
});
and this is the complete error:
D:\user\Bots\dadobot2801\a\node_modules\discord.js\src\client\Client.js:548
throw new TypeError('CLIENT_MISSING_INTENTS');
^
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client.
at Client._validateOptions (D:\user\Bots\dadobot2801\a\node_modules\discord.js\src\client\Client.js:548:13)
at new Client (D:\user\Bots\dadobot2801\a\node_modules\discord.js\src\client\Client.js:76:10)
at Object.<anonymous> (D:\user\Bots\dadobot2801\a\test.js:4:16)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (D:\user\Bots\dadobot2801\a\index.js:10:14) {
[Symbol(code)]: 'CLIENT_MISSING_INTENTS'
}
Any ideas?
Try using this. Since new updated of discord.js like version ^13.0 you have to specify client intents:
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
Its how you do it
I had this problem before so i know how to fix it. You need to enable these 3 intents as seen in the image above.
Try this:
npm uni discord.js
npm i discord.js#12.5.3

Discordjs V13: Add Role from Direct Message

I am trying to assign a role to a user who direct messaging the bot. Although the user was being assigned the role but it throw the error below and shut down my program. I have been researching this issue for few hours but still no luck.
TypeError: Cannot read properties of undefined (reading 'id')
Error Stack
TypeError: Cannot read properties of undefined (reading 'id')
at GuildMemberRoleManager.get cache [as cache] (C:\Users\josh\Desktop\discord\node_modules\discord.js\src\managers\GuildMemberRoleManager.js:36:101)
at Client.<anonymous> (C:\Users\josh\Desktop\discord\discordBot.js:46:23)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
TypeError Cannot read properties of undefined (reading 'id')
UNHANDLED REJECTION! 💥 Shutting down...
[nodemon] app crashed - waiting for file changes before starting...
Here is my code.
const { Client, Intents } = require('discord.js');
const client = new Client({
partials: ['CHANNEL'],
intents: [Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.GUILD_MEMBERS],
});
client.once('ready', () => {
console.log(`Ready!`);
});
client.on('messageCreate', async (message) => {
if (message.author.bot) return;
if (message.content.startsWith('/test')) {
const member = await client.guilds.cache
.get(process.env.DISCORD_GUILD_ID)
.members.fetch(message.author.id);
await member.roles.add('123456789012345678');
}
});
client.login(process.env.DISCORD_TOKEN);
This is because of the lack of GUILDS intent. If you look at the source code, here, it shows that it tries to get the #everyone role from cache, but can't find it since it's not cached (giving undefined).
Provide GUILDS intent to fix
const client = new Client({
partials: ['CHANNEL'],
intents: [Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS],
})

Discord.js 'CLIENT_MISSING_INTENTS' Error

I am trying to make my Discord bot appear online, but I am getting an error
Code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready' , () => {
console.log('Bot is online');
});
client.login('My token');
Can anyone point me in the right direction?
Intents were introduced in discord.js v13 and ensure that the bot only does what it intends to do - i.e. it doesn't waste processing power.
//example from the official Discord.js guide
const { Client, Intents } = require('discord.js');
const myIntents = new Intents();
myIntents.add(Intents.FLAGS.GUILD_PRESENCES, Intents.FLAGS.GUILD_MEMBERS);
const client = new Client({ intents: myIntents });
client.login('mytoken');
The docs are always a good place to look first.

Resources