Bot not responding to message - javascript

I'm new to this and have been following a tutorial online but I cannot figure out why the bot will not respond to the message.
Things I have checked:
the tokens match up
bot has proper permissions
not missing any ";" etc etc
Here is the code for reference
const Discord = require('discord.js');
const bot = new Discord.Client({ intents: [Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMessages, Discord.GatewayIntentBits.DirectMessages] });
const token = <token>;
bot.on('ready' , () =>{
console.log('ts ready gangy :3');
})
bot.on('message', msg=>{
if(msg.content === "hi"){
msg.reply('hi');
}
})
bot.login(token);
The code runs and the bot will appear online as normal but when inputting "hi" and sending it, the bot gives no response.

Add Discord.GatewayIntentBits.MessageContent to your intents and tick Message Content Intent if you didn't tick

Related

Why doesn't my discord.js bot reply to messages? [duplicate]

This question already has an answer here:
message.content doesn't have any value in Discord.js
(1 answer)
Closed 4 months ago.
So my code is this:
const { GatewayIntentBits } = require('discord.js');
const Discord = require('discord.js');
const token = 'My Token';
const client = new Discord.Client({
intents:
[
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages
]
});
client.on('ready', () =>
{
console.log("Bot is on");
});
client.on('message', (message) =>
{
const channel = client.channels.get("1028177045773094915")
if (message.content === 'ping')
{
channel.send("pong")
}
});
// This is always the end
client.login(token);
I don't know why it is not working, it's in online but when I type anything in channel it doesnt do anything.
There are a few errors with this.
Before I get into the more complicated stuff, please know that the message event doesn't work -- use messageCreate.
The second smaller thing that I need to point out is that you don't have the MessageContent Intent enabled; that needs to be enabled for you to detect the text/attachments of a message that isn't a Direct Message to the bot, and doesn't ping/mention the bot.
Now, for the bigger stuff.
client.channels.get() isn't a function, and that's because of the Channel Cache.
I'll try my best to break down what a Cache is, but I'm not too good with this, so someone can correct me below.
A cache is similar to a pocket; when someone (being discord.js) looks inside of your pocket (the cache), that someone can only see the items inside of the pocket. It can't detect what isn't there. That's what the cache is. It's the inside of the pocket, and holds the things that you want discord.js to see.
If your item isn't in the cache, discord.js won't use it. But, you can get discord.js to look for that item elsewhere, and put it into that pocket for later.
This is the .fetch() method. It puts something that isn't in the cache, well, in the cache.
An example of using it:
const channel = await client.channels.fetch("CHANNEL_ID");
Or, what I'd call the more "traditional" way of doing it:
client.channels.fetch("CHANNEL_ID").then(channel => {
});
Now, after fetching your channel, you can call it like this:
client.channels.cache.get("CHANNEL_ID");
Another fun thing about the .fetch() method is that you can use it to shove everything in the cache, though it isn't necessary in most cases.
client.channels.fetch();
That will fetch every channel that the bot can see.
client.guilds.fetch();
That will fetch every guild the bot is in.
guild.members.fetch();
That will fetch every guild member of a guild.
And so on.
After Everything
After applying everything that I've said previously, you should wind up with code that looks like this:
const { Client, GatewayIntentBits } = require("discord.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent // Make sure this is enabled in discord.com/developers/applications!
]
});
client.on("messageCreate", async(message) => {
if(message.content.toLowerCase() === "ping") {
// I added the .toLowerCase() method so that you can do PING and it works.
client.channels.fetch("1028177045773094915").then(channel => {
channel.send({ content: "pong!" });
});
}
});
client.login("Your Token");

Discord bot not on members list but online

I'm trying to start my first discord bot. Installed NodeJS on my PC, started app in discord dev tools, make it bot, got token, selected priviliges, added bot to my server. Actually another bot i already have even sent welcome message saying my new bot joined the server. When i start it using command "node main.js" it says it went online, doesn't give any errors, however my bot isn't showing in members list (neither offline nor online) and it doesn't react to messages with its prefix. Here's my main.js code:
const Discord = require('discord.js');
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const prefix = '=';
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'status') {
console.log(command);
message.channel.send('Bot dziala poprawnie.');
}
} );
client.once('ready', () => {
console.log('GuruBot online');
});
client.login('my_token_is_here');```
The bot probably doesn't have access to the text channel. Check the bot's permissions on your server, and check the invite link that you're using to make sure that the bot can view channels and incoming messages.
I'm very late to the question, but hopefully this will help anyone who is searching for this issue.

How do I get my Discord bot to send a welcome message?

I've watched numerous videos, and followed examples online, and still can't get my discord bot to simply send a message upon a member joining. Maybe I'm missing out on an important update?
//require the necessary discord.js classes
const { Client, Intents } = require("discord.js");
const { token } = require("./config.json");
//create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
//when the client is ready, run this code (only once)
client.on('ready', () => {
console.log("Online");
});
//sends user a welcome message upon joining
client.on('guildMemberAdd', member => {
const channel = member.guild.channels.cache.get('921490782958014576');
channel.send("Welcome to the server!!")
});
client.login(token);
I get nothing. Nada. I've tried several different pieces of code. I've TRIPLE checked the permissions. This is very frustrating. I've tried unadding and readding the bot to the server. I checked up on the discord.js documentation and nothing seems to be wrong with my code. To me it seems as if the guildMemberAdd isn't emitting anything. I'm using notepad++ to edit my code. I feel I could be using something better for debugging purposes possibly.
The issue may be due to missing intents. Try adding the GUILD_MEMBERS intent to your list of intents like so.
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS] });
discord.js docs: https://discord.js.org/#/docs/main/stable/class/Intents?scrollTo=s-FLAGS
Developer docs: https://discord.com/developers/docs/topics/gateway#gateway-intents

How to fetch discord.js bot informations like bot username or bot id without login in it and using token

All i want is to get simple discord bot informations using token, but i dont want to make bot online.
I tried using discord API and it doesnt work for me but discord.js client it works but makes bot online and i dont want it.
This code will login to discord, get the bot's info, then logout.
const { Client } = require('discord.js'),
const client = new Client();
client.on('ready', async () => {
console.log(client) // or client.user if you just want client user info
client.destroy() // logs out of Discord
})
client.login('...')
You can set the status of the bot to invisible by using <client>.setStatus().
Full example:
const Discord = require('discord.js'); // Define Discord
const client = new client(); // Define client
client.login('your-token-here'); // Login your Discord bot
client.once('ready', function() { // When the client is ready
client.setStatus('invisible'); // Makes the bot look offline
});

Message reaction

I am trying to get my Discord bot to react to people's messages. For example, if somebody said "welcome", the bot will react to their message, because it has the word welcome, which I would set the keyword too. I understand how to get the bot to reply to messages with a response, but not reactions.
In discord.js you can use something like this:
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("message", message => {
if(message.content === "welcome") {
message.react("😄");
}
}
client.login("YOUR_TOKEN");
This in the Discord.js Docs might be helpful as well.

Categories

Resources