const { MessageEmbed } = require(`discord.js`)
const Discord = require("discord.js");
const nodemon = require("nodemon");
const client = new Discord.Client({
intents: ["CHANNEL", "GUILD_MEMBER", "MESSAGE", "REACTION", "USER"],
});
client.commands = new Discord.Collection();
client.events = new Discord.Collection();
["command.handler", "event.handler"].forEach((handler) => {
require(`./handlers/${handler}`)(client, Discord);
});
client.login(`my token :)`);
throw new RangeError('BITFIELD_INVALID', bit);
^
RangeError [BITFIELD_INVALID]: Invalid bitfield flag or number: CHANNEL.
at Function.resolve (C:\Users\swide\Desktop\NWMPoCoToAleTak\node_modules\discord.js\src\util\BitField.js:152:11)
at Function.resolve (C:\Users\swide\Desktop\NWMPoCoToAleTak\node_modules\discord.js\src\util\BitField.js:147:40)
at Client._validateOptions (C:\Users\swide\Desktop\NWMPoCoToAleTak\node_modules\discord.js\src\client\Client.js:550:33)
at new Client (C:\Users\swide\Desktop\NWMPoCoToAleTak\node_modules\discord.js\src\client\Client.js:76:10)
at Object. (C:\Users\swide\Desktop\NWMPoCoToAleTak\main.js:5:16)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32) {
[Symbol(code)]: 'BITFIELD_INVALID'
}
Your intents are not defined correctly,
intents: ["CHANNEL", "GUILD_MEMBER", "MESSAGE", "REACTION", "USER"],
These are not intents, these are partials.
Please visit this page to see a list of all intents to choose from.
To correct this use this code:
const { MessageEmbed, Intents } = require(`discord.js`)
const client = new Discord.Client({
intents: [], // Insert intents from below, separated by a comma [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS, etc.]
partials: ["CHANNEL", "GUILD_MEMBER", "MESSAGE", "REACTION", "USER"],
});
Intents List:
Intents.FLAGS.GUILDS
Intents.FLAGS.GUILD_MEMBERS
Intents.FLAGS.GUILD_BANS
Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS
Intents.FLAGS.GUILD_INTEGRATIONS
Intents.FLAGS.GUILD_WEBHOOKS
Intents.FLAGS.GUILD_INVITES
Intents.FLAGS.GUILD_VOICE_STATES
Intents.FLAGS.GUILD_PRESENCES
Intents.FLAGS.GUILD_MESSAGES
Intents.FLAGS.GUILD_MESSAGE_REACTIONS
Intents.FLAGS.GUILD_MESSAGE_TYPING
Intents.FLAGS.DIRECT_MESSAGES
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS
Intents.FLAGS.DIRECT_MESSAGE_TYPING
Intents.FLAGS.GUILD_SCHEDULED_EVENTS
As for the other answer, you can do this to enable all intents but it may unnecessary if you don't need all intents but if you chose to, which most would advise against but it is your choice, that code would look like this (with the partials from above as well):
const client = new Discord.Client({
intents: 131071,
partials: ["CHANNEL", "GUILD_MEMBER", "MESSAGE", "REACTION", "USER"],
});
UPDATE intents: 32767 no longer includes all intents, all intents is now 131071
If you want to enable all intents here's a quick way;
const client = new Discord.Client({ intents: new Discord.Intents(32767) })
this allows all intents.
Related
I want to send a private message to every Servermember.
If i use this intents its not working.
Also not working with Intents.ALL, Intents.Guild.ALL, ...
`const client = new Client({intents: [
Intents.all()
]});
client.guilds.cache.forEach(guild => {
guild.members.cache.forEach(member => {
member.send("hi").catch(console.error);
});
});`
Tried many different intents, yet not working.
Never use all intents, it significally slows down your bot and is a bad practise. You can read the guide on intents here and a list of intents here.
Here's an example from the guide:
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
In Discord.JS, all intents are PascalCase. Keep that in mind.
My goal is to have a file run every few minutes on a schedule(cron). The cron isn't the issue, the issue is when I try and retrieve the members, I get Collection(0) [Map] {} Which is odd, there are members and my intents are set correctly.
const dotenvFlow = require('dotenv-flow');
const { Client, Partials, GatewayIntentBits } = require('discord.js');
dotenvFlow.config();
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildIntegrations, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.DirectMessageTyping, GatewayIntentBits.DirectMessages], partials: [Partials.Channel] });
// Get the guild object
async function updateMembers() {
console.log("Starting...")
const guild = await client.guilds.fetch(process.env.hostDiscordServer);
// Get the members of the guild
const members = guild.members.cache;
// Insert or update each member in the database
members.forEach(member => {
// Check if the member belongs to the specified guild
if (member.guild.id === guild.id) {
// Get the member's premium role
const pRole = member.roles.cache.find(role => [process.env.pPremiumRole1, process.env.pPremiumRole2, process.env.pPremiumRole3].includes(role.name))?.name;
console.log("Inserting into DB")
// Insert or update the member's record in the database
console.log(member.id)
console.log(pRole)
}
});
}
// Log in to Discord with your client's token
client.login(process.env.DISCORD_TOKEN);
// Start the code
updateMembers();
You are iterating through the cached members of the guild.
If you want to iterate through all members (even the uncached) you need to fetch them first.
You just need to replace
const members = guild.members.cache;
with
const members = await guild.members.fetch();
discord.js docs
EDIT: I would also recommend changing
updateMembers();
to
client.once("ready", updateMembers);
so that the code only runs when the bot ready.
(Thanks to #Zsolt Meszaros for correcting me on that one)
i try make embed
const exampleEmbed = new EmbedBuilder()
.setAuthor({ name: `name : ${name}` })
.setColor(0x0099ff)
.setTitle(`title: ${title}`)
.setDescription(`content : ${content}`)
make embed!!
looking for discordjs14 guide
// Resending a received embed
const receivedEmbed = message.embeds[0];
const exampleEmbed = EmbedBuilder.from(receivedEmbed).setTitle('New title');
channel.send({ embeds: [exampleEmbed] });
----------------------------------------------
// Editing the embedded message content
const exampleEmbed = new EmbedBuilder()
.setTitle('Some title')
.setDescription('Description after the edit');
message.edit({ embeds: [exampleEmbed] });
i try to get message.embed[0]
async execute(interaction) {
...
console.log(interaction.embed[0])
...
}
error:
i hope search title and edit embed...
If you want to send an embed and edit it on an event, You should make thay message a var.
const channel = <client>.channels.cache.get('channel_id');
let message = await channel.send({embeds: [exampleEmbed] });
// editing it
await message.edit({ embeds: [exampleEmbed1] });
Notes: Replace <client> with what you declared your client. it could be client , bot or anything you have declared it.
fist you need to make embed message
const embed = new EmbedBuilder()
.setColor("Your Color")
.setTitle("YourTitle")
.setThumbnail("your google image address")
.setImage("Your Google Image address")
.setDescription("Your Description")
and so much more stuff just type .set or .add it will show you so many of them
and for send the embed
message.reply({embeds : [embed]});
To start: Yes I have intents setup. I have the "message content" intent enabled in the developer portal.
Recently I decided to setup slash commands for my bot. (That is all working fine, not the purpose of this question). Now however, I find that my "messageCreate" event is not firing at all. I've done some basic testing, including removing the "interactionCreate" event, and it still is non-functional. The only fix I've found is completely reverting to the previous version on my source control.
I haven't been able to find any solutions from the discord.js Discord so I decided to come here in the hopes someone can help. Thanks for your time! Client constructor and event code attached below.
Client:
const myIntents = new Intents(
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS
);
const client = new Client({ intents: myIntents, partials: ["CHANNEL"] });
Events:
client.once('ready', () => {
// Works fine
});
client.on('interactionCreate', async interaction => {
// Works fine
});
client.on('messageCreate', async message => {
console.log('DEBUG') // Never triggers
// Other stuff
});
client.on("error", (e) => console.error(e));
client.on("warn", (e) => console.warn(e));
process.on('unhandledRejection', (e) => console.error(e));
console.log("client") // All events, including messageCreate, are visible in the resulting output
client.login(config.token);
I think you need the "MESSAGE" partial.
You can try and replace
const client = new Client({ intents: myIntents, partials: ["CHANNEL"] });
To
const client = new Client({ intents: myIntents, partials: ["CHANNEL", "MESSAGE"] });
Moreover, I had the same issue but I was very dumb because I didn't realize that my bot didn't have "READ MESSAGES" and "SEND MESSAGES" permissions in that channel, so that might be the issue.
I'm trying to set up a welcome message function on my discord bot, but no matter what I do, the bot doesn't seem to be able to use guildMemberAdd. I'm aware of the new update, and as suggested I've turned on both options under Private Giveaway Intents. But it still does not work. Here is my code:
client.on('guildMemberAdd', member => {
const emb = new MessageEmbed()
.setColor('#FFBCC9')
.setTitle("new member")
.setDescription("welcome to the server!")
member.guild.channels.get('780902470657376298').send(emb);
});
I have also found an answer online suggesting to use this:
const { Client, Intents } = require("discord.js");
const client = new Discord.Client({ ws: { intents: new Discord.Intents(Discord.Intents.ALL) }});
But no matter how I write that, my bot just won't even come online unless I use const client = new Discord.Client(); with nothing in the parentheses.
With v12 coming along, in order to get your full channels list you're only able to get the cached ones in your server. Hence why you should change this line:
member.guild.channels.get('780902470657376298').send(emb);
to:
member.guild.channels.cache.get('780902470657376298').send(emb);
You passed in the intents params wrong. Here is the correct way to do it.
const { Client, Intents } = require("discord.js");
const client = new Discord.Client({ ws: { intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_MEMBERS', 'GUILD_PRESENCES'] } });
If you use this the guildMemberAdd event will emit.
Make sure you have intents turned on in the developer portal. As Shown in this image https://i.imgur.com/WfBLtXY.png.