enter image description here
i am trying to create my own discord bot and it gives me that error I don't know what to do
it's giving me that error
enter image description here
can you help me, please?
my version of discord.js is 14.0.3
and my node.js version is 18.5.0
I tried everything that I could even your tips and I can't do this so do you have any tips to please help me because I promised a friend I would help him
I GOTTA SAY THAT ITS THE FIRST TIME THAT IT HAPPENED TO ME AND BEFORE THAT I CREATED 2 BOTS
In discord.js v14, Intents have been replaced by GatewayIntentBits. Therefore, the correct way for creating the client is like this:
const { Client, GatewayIntentBits } = require('discord.js')
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
// ...
]
})
For more information, you can go here => Discord.js v13 code breaks when upgrading to v14
They changed how this works in discord.js v14.
Example from the official updating guide:
const { Client, Intents } = require('discord.js'); // old
const { Client, GatewayIntentBits, Partials } = require('discord.js'); // new
const client = new Client({ intents: [Intents.FLAGS.GUILDS], partials: ['CHANNEL'] }); // old
const client = new Client({ intents: [GatewayIntentBits.Guilds], partials: [Partials.Channel] }); // new
This error will happen if intents are missing or not properly listed. Refer to the code here and it will work.
const Discord = require('discord.js');
const client = new.Discord.Client({
intents: [
"GUILDS",
"GUILD_MESSAGES"
]
});
Related
I'm running node.js version 16.17.0 on macOS running Monterey. I have been building a bot a little at a time and I've gotten to the point where I am making it say hello and goodbye to users entering and leaving the server. The bot says hello to a new user and sends the embedded message (the user's avatar). When I leave the server with my alt account however it only sends the embedded message, not the farewell message that I have additionally. To be clear, the bot should send a hello message along with an embedded avatar picture when someone enters and should send a farewell message along with the embedded avatar when someone leaves but it only sends the avatar with no message for leaving.
I tried adding GUILD_PRESENCES to the intents that I call at the beginning but that runs me into the following error:
/Users/saisonxiang/genshi/node_modules/discord.js/src/util/BitField.js:168
throw new RangeError(ErrorCodes.BitFieldInvalid, bit);
^
RangeError [BitFieldInvalid]: Invalid bitfield flag or number: undefined.
at Function.resolve (/Users/saisonxiang/genshi/node_modules/discord.js/src/util/BitField.js:168:11)
at /Users/saisonxiang/genshi/node_modules/discord.js/src/util/BitField.js:163:54
at Array.map (<anonymous>)
at Function.resolve (/Users/saisonxiang/genshi/node_modules/discord.js/src/util/BitField.js:163:40)
at Client._validateOptions (/Users/saisonxiang/genshi/node_modules/discord.js/src/client/Client.js:482:41)
at new Client (/Users/saisonxiang/genshi/node_modules/discord.js/src/client/Client.js:78:10)
at Object.<anonymous> (/Users/saisonxiang/genshi/index7.js:20:16)
at Module._compile (node:internal/modules/cjs/loader:1126:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
at Module.load (node:internal/modules/cjs/loader:1004:32) {
code: 'BitFieldInvalid'
}
I also tried moving the farewell message outside of the 'ready" statement, deleting and reinstalling the node_modules folder, and deleting and reinstalling node.js (as well as updating after reinstall).
I'm fairly stuck. Any help is appreciated. Here's the working code that just doesn't send the farewell message:
// Import Discord.js
const {
Client,
Collection,
GatewayIntentBits,
Partials,
EmbedBuilder
} = require('discord.js');
const { token } = require('./config.json');
// I like importing the intents and partials this way, just to make things a little more visible.
// But the main things we're importing here are the Guild, and GuildMembers [PLURAL]
const { Guilds, GuildMembers, MessageContent, GuildMessages, GUILD_PRESENCES } = GatewayIntentBits;
// And here, we're importing the GuildMember [SINGULAR] for our Partials
const { User, Message, Channel, GuildMember } = Partials;
// Create Discord Client
const client = new Client({
intents: [
Guilds,
GuildMembers, // IMPORTANT
GuildMessages,
MessageContent,
],
partials: [
User,
Channel,
Message,
GuildMember, // IMPORTANT
],
});
// When the client is ready, run this code (only once)
client.once('ready', () => {
// Adding the GuildMembers to our intents, and GuildMember to our partials allows for us to
// receive the info we need for a welcome message; ei. when a member joins
// And lastly, when a member joins. I noticed you had the event inside your ready event,
// but it needs to be outside the ready so be sure to double check that :)
client.on('guildMemberAdd', async (GuildMember) => {
console.log(GuildMember); // View the member data
await GuildMember.guild.channels.cache.get('1010768161349587035').send(`Well... ${GuildMember.user.username + "#" + GuildMember.user.discriminator} joined the server so I guess we will be okay`);
});
client.on('guildMemberAdd', guildMember =>{
const embed7 = new EmbedBuilder()
.setAuthor({name: `${guildMember.user.username}`, iconURL: guildMember.displayAvatarURL({dyanmic: true})})
.setThumbnail(guildMember.displayAvatarURL({dynamic: true}))
guildMember.guild.channels.cache.get('1010768161349587035').send({embeds: [embed7]})
});
client.on('GuildMemberRemove', async (GuildMember) => {
console.log(GuildMember); // View the member data
await GuildMember.guild.channels.cache.get('1010768161349587035').send(`Apparently ${GuildMember.user.username + "#" + GuildMember.user.discriminator} can't handle our bullshit anymore and decided to leave the server. They will be missed.`);
});
client.on('guildMemberRemove', guildMember => {
const embed8 = new EmbedBuilder()
.setAuthor({name: guildMember.user.username, iconURL: guildMember.displayAvatarURL({dyanmic: true})})
.setThumbnail(guildMember.displayAvatarURL({dynamic: true}))
guildMember.guild.channels.cache.get('1010768161349587035').send({embeds: [embed8]})
});
})
console.log('(。··)_且');
// Login to Discord with your client's token
client.login(token);
You need to use the IntentBitfield from discord import.
Reference: https://discordjs.guide/popular-topics/intents.html#the-intents-bitfield
Main.JS
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
console.log('TrooperCohen is online!');
});
client.login('BOT TOKEN');
Whenever I try to put my bot online by doing either "node ." or "ts-node main.js" I get this same symbol code where it says I am missing intents.
Please remove the token from your question as it can lead to a security risk.
I have the code as follows and it works flawlessly
const Discord = require('Discord.js');
const client = new Discord.Client();
client.on("ready", () => {
print.success(`The bot has started, with ${client.users.cache.size} users, in ${client.channels.cache.size} channels in ${client.guilds.cache.size} guilds.`);
});
client.login(process.env.DISCORD_TOKEN);
I'm pretty new so excuse my lack of technical terms. The bot is running (token is correct) and has no errors in the terminal, but I am not getting a reply to my message in Discord.
I was getting BOT NOT DEFINED error, but I moved client.login(TOKEN) to the last line of the code, and it went away. I am following the tutorial code pretty closely, so I am not sure why I am getting the error.
Using node v16.9.1
require('dotenv').config();
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] })
client.on('ready', () => {
console.log(`It's welcome time`);
});
client.on('message', msg => {
if (msg.content === 'Hi') {
msg.reply('Welcome wallet warrior!');
msg.channel.send('Welcome wallet warrior!');
}
else {
msg.channel.send('Hi, please introduce yourself')
};
});
const TOKEN = '(ERASED MY TOKEN)';
console.log(TOKEN)
client.login(TOKEN);
This question already has an answer here:
Discord js reaction not detected [duplicate]
(1 answer)
Closed 1 year ago.
I've set up a bot with Discord.js version 13.2 and Node version 16.10.0. I've verified this using 'node -v' in the terminal and checking the Discord version using discord.version. I've pasted my code below. My bot is added to my server using Admin permissions (8) and using the Bot scope.
When I run the code my bot connects. I can see it go online on my server. I get the Ready message in the terminal. But when I type a message into any channel on Discord, I don't get the event. I used an old bot I had working in Discord.js version 12 and using Node version 14 (running on a separate laptop), and I was able to get it detecting messages I send in my server. I've been wracking my brain over anything I could have done but I can't figure out what's wrong here.
const {Client, Intents} = require('discord.js')
const {token} = require('./config.json')
//const client = new Client({intents: [Intents.FLAGS.GUILDS]})
var client = new Client({autoReconnect:true,intents:[Intents.FLAGS.GUILDS]})
client.on('ready',()=> {
console.log("Ready")
})
.on("error",(err)=> {
console.log(err)
})
.on('invalidated',()=> {
console.log("Client invalid.")
})
.on('guildUnavailable',(guild)=> {
console.log("Guild unavailable.")
})
// Trying both 'message' and 'messageCreate' just in case
.on('message',(msg)=> {
console.log(msg.content)
})
.on('messageCreate',(msg)=> {
console.log(msg.content)
})
.on('messageReactionAdd',(reaction,user)=> {
console.log("reaction!")
})
// Connect to Discord
client.login(token)
You need GUILD_MESSAGES intent to detect messages
var client = new Client({
autoReconnect: true,
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES
]
})
and reactions need GUILD_MESSAGE_REACTIONS intents
var client = new Client({
autoReconnect: true,
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS
]
})
I started to make a bot for my Discord server, but I'm completely new to it (I have programming skills, but in web development). I made an application on the Discord developer portal, I made a folder on my PC, I created a package.json file, a main.js file, installed node.js, installed discord.js, I deployed my bot on a test server, etc. (not in this order but whatever).
Then, following a tutorial from a site, I made this in the index.js file:
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
console.log('Ready!');
});
client.on('message', message => {
if (message.content === '!ping') {
message.channel.send('Pong.');
}
});
client.login(' I PUTTED MY TOCKEN HERE ');
When I put the command !ping on the test server I created, the bot remains offline and I don't receive Pong...
Can you please help me, please?
If the bot does not turn on, that means you did not correctly login or start the bot. Try to define token like const token = "BOT TOKEN HERE", then put client.login(token) instead of what you have.
If that does not help, also make sure that you did node . in your terminal, which will start the bot.
So, your whole code should look something like this:
const Discord = require('discord.js');
const client = new Discord.Client();
const token = "bot token here";
client.on('ready', () => {
console.log('Ready!');
});
client.on('message', message => {
if (message.content === '!ping') {
message.channel.send('Pong.');
}
});
client.login(token);
Make sure that your bot is allowed to recieve messages (discord.com/developers > your bot > bot > the two intent-switches).
Then add this into the brackets of new Discord.Client();
{ partials: ['MESSAGE', 'CHANNEL', 'REACTION', 'USER', 'GUILD_MEMBER']} (you can remove some of those parameters but message will be needed).
Sorry for bad English...