Discord bot is not responding to commands - node.js

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

Related

Discord - JavaScript and missing intents

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

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
}

How do I DM a user when they join the server?

And also make an embed which you have to react to with an emoji to get a verified role in the server
You can detect user joining by an event:
const { Client } = require('discord');
const client = new Client();
client.on('guildMemberAdd', function(member) => {
member.send('Welcome to my server!');
});
client.login('<token>');
Then, you can fetch a channel in the server and send the embed there:
const channel = client.channels.cache.get('<channel-id>');
channel.send(embed)
.then(message => {
//reaction collector
});
For the reaction collector, please refer to the djs guide.

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