Discord - JavaScript and missing intents - node.js

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);

Related

Discord.js: Invalid bitfield flag or number: GUILDS?

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"
]
});

My bot is running in the terminal, but wont produce an output on Discord- js

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);

My Discord bot is messaging continuously even though I programmed it to do it once

I was creating a discord bot and was testing it. When I say Hi or Yo it will msg me an emoji.
It is working fine but, the problem is that it is msg the emoji continuously.
Here is my code:
console.log('Beep Beep! 🤖');
const Discord = require('discord.js');
const client = new Discord.Client(); // Client is the object that connects with the discord API itself to run the bot
client.login('API');
client.on('ready', readyDiscord);
function readyDiscord(){
console.log('👍');
}
client.on('message', gotMessage);
function gotMessage(msg){
if (msg.channel.id == '849147593392914453' && msg.content === 'Hi' || 'Yo'){
// msg.reply('🤟🤟');
msg.channel.send('🤟🤟');
}
}
You don't have to follow mine since it seems the answer is already here, but here is what I would do.
Also, don't show your token to anyone. The token is your key to your bot, so I'd recommend resetting your bot's token in the Discord Developer Portal.
Anyways, here is what I would do:
const Discord = require('discord.js');
const client = new Discord.Client();
client.login('TOKEN_HERE');
client.once('ready', () => {
console.log('I am ready to help others! 👍');
});
const channelID = '849147593392914453';
client.on('message', message => {
if (message.channel.id == channelID && message.content.toLowerCase() === 'hi' || 'yo' || 'hey'){
message.reply('Hey! 🤟🤟');
}
});
Seems like you added the API token to this post. API token should be kept private at all times.
Second of all, you can use a closure if you're trying to send your message only once. I tested the attached snippet locally and it works fine. The message will be sent only once.
function gotMessage(msg){
let isReplied = false;
function reply() {
if (!isReplied && (msg.content === 'Hi' || 'Yo')){
console.log('🤟🤟');
isReplied = true
}
}
return reply
}

Discord bot is not responding to commands

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...

Discord Bot Mentioning

I am trying to make my bot send a ping to me when someone types !owner, however, the bot only sends my userID or name unlinked. How do I make it into a ping? I am using NodeJS for this bot.
const Discord = require('Discord.js');
const bot = new Discord.Client();
bot.on('message', (message) => {
if(message.content == '!owner') {
message.channel.send('Hi #"userID" is the owner, do you need help');
}
});
bot.login('BotToken')
Replace #"userID" with <#your_user_id_here>.
For example: <#123456789123456789>

Resources