Discord bot won't log messages - javascript

Currently new to coding Discord bots, and was following along with an online tutorial.
I'm stuck on trying to get the client's message method to log the message that the user typed.
Here's my code:
require('dotenv').config();
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILD_MESSAGES"] });
client.on('ready', () => {
console.log(`${client.user.tag} has logged in`);
});
client.on('message', (msg) => {
console.log(msg.content);
});
client.login(process.env.TOKEN);
The tutorial is a bit outdated since Discordjs has been updated, and I'm not sure if the problem has to do with the intent or something else.
The bot is able to login to the server and shows up as online, with the console registering the login.
However, the console is never able to log any messages sent. Any help would be appreciated.

You need to include both intents:
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
You can still use client.on('message') but it will come with a DEPRACATED warning, so as of v13 you should use client.on('messageCreate')

As per the docs:
//discord.js V12
-client.on('message', (msg) => {
//discord.js V13
+client.on('messageCreate', (msg) => {

Related

javascript discord bot only answers in DMs

I develop a discord bot in javascript and added the bot to a server. The bot has the permissions to view the channel, view message history and send messages.
Sadly, it only replys in DMs.
require("dotenv").config(); //to start process from .env file
const {Client, Intents}=require("discord.js");
const client=new Client({
Intents:[
Intents.FLAGS.GUILDS,//adds server functionality
]
});
client.once("ready", () =>{
console.log(` READY - Logged in as ${client.user.tag}!`); //message when bot is online
})
// messages
client.on('message', msg => {
if (msg.content.toLocaleLowerCase()==='hello') {
msg.reply('hello there!')
}
})
client.login(process.env.TOKEN);
I tried to play around with the permissions on the discord website and to change msg.reply to msg.send and many others. I've gone thru a minimum of 10 tutorials and docs but it still only replies to DMs.
I expected that I get some error codes and informations about why it doesn't respond but nothing happend. Thank you for your help.

Discord.js bot doesnt see messages

My bot is supposed to send a message in console when message is sent. But it doesn't trigger after I am writing a messages on discord server.
Bot is online and joined the server. He also got all needed permissions.
CODE:
const Discord = require('discord.js');
const bot = new Discord.Client({disableEveryone: false, intents: ["0x0000000000000008"]});
bot.on('ready', () => {
console.log('Loaded!');
bot.user.setActivity('Running a test, hopefully.');
});
bot.on("Message", (message) => {
console.log('Message!');
});
bot.login("bot token");
You're using incorrect event name for the incoming message (Message). Correct name of this event is depends on your version of discord.js. If you're using version v13.* or greater, you should use messageCreate event:
// Correct event name for discord.js v13+
client.on('messageCreate', message => {
console.log('Message received! Message content: ' + message.content);
});
// And this event is deprecated
client.on('message', message => {
console.log('Message received! Message content: ' + message.content);
});
As an additional note regarding how you initialize your client: you should use Intents class on v13 or GatewayIntentBits on v14 for better readability. You can read more about intents for v13 here and changed initialization for v14 here.
In your event listener you have logged a string, not the message object
bot.on("message", (message) => {
console.log('Message!'); //incorrect - string, will log 'Message!'
console.log(message.content); //correct - content property of message obj, will log message content
});
You will also need to add the correct intents. Since MessageContent is a privileged gateway intent, you'll need to enable it in the Discord Developer Portal.
Check out a good article on this in the docs.

Discord Bot Doesn't reply back (Node.js)

I'ven struggling creating a bot for my discord server so i used the initial code for the discord bot from discordjs.guide and added a simple command to respond to "ping", i wanted to prove what i did wrong before... but i get no response from the bot.
I already give it permissions with the invite link and with a role, someone can tell me what's wrong with the code?
I installed discord.js and dotenv
// 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.once('ready', () => {
console.log('Ready!');
});
client.on('message', function(msg){
if(msg.content === 'ping'){
msg.reply('pong');
}
});
// Login to Discord with your client's token
client.login(token);
it's because the client.on("message") has been deprecated
And you should define on your discord client the GUILD_MESSAGES as this
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
and the event to receive the message on your server would be
client.on('messageCreate', function(msg){
if(msg.content === 'ping'){
msg.reply('pong');
}
});
Docs of "messageCreate" event
Your code is missing the GUILD_MESSAGES intent. Simply add it into your intents and your bot should respond to messages. Pay in mind that discord is removing message intents after April 30th so you'd need to go into https://discord.com/developers and into the bot section, turn on message intents.
// 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, Intents.FLAGS. GUILD_MESSAGES] });
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
client.on('message', function(msg){
if(msg.content === 'ping'){
msg.reply('pong');
}
});
// Login to Discord with your client's token
client.login(token);
Pay in mind that discord is removing message intents after April 30th so you'd need to go into https://discord.com/developers and into the bot section, turn on message intents.

How do I make my discord bot copy and message every user written message?

So I am new to Javascript and Discord bots. I have a stupid idea for a bot. Mostly just to annoy my friends. But the basic idea is that it will copy their message and respond with their message. I am having a problem where I cannot get the bot to respond to more than one message.
Here is my code:
const Discord = require('discord.js');
const client = new Discord.Client();
// Logs into discord with the app's token
client.login('...');
// when logged in succesfully this code will run once
client.once('ready', () => {
console.log('Ready!');
});
if(client.on) {
client.once('message', function (message) {
// Send message back on the same channel
message.channel.send(message.content);
});
}
I am aware that having client.once only sends the message once but if I were to change that to client.on it would send the same message to an infinite amount. Any help would be appreciated. Thanks.
You just need to prevent the bot from responding to itself.
client.on("message", (message) => {
// Use this if you don't want the bot to respond to itself
if (message.author.id == client.user.id) return;
// Use this if you don't want the bot to respond to other bots (including itself)
if (message.author.bot) return;
message.channel.send(message.content);
});

Server greetings

I tried to make server greetings message but it doesn't even work for me, I saw that you can try to do this using ch.name === 'name' but I want my bot to send message to channel with specific id
client.on("guildMemberAdd", (member) => {
const channel = member.guild.channels.cache.find((ch) => ch.id === `channel-id`);
if (!channel) return;
channel.send(`Welcome to the server, ${member}!`);
});
I believe you need to turn on Privileged Gateway Intents on the Dev Portal (https://discord.com/developers/applications)
gateway intents on
You have to enable intents for the guildMemberAdd event to emit. Make sure to enable them at here
after enabling intents, you can add intents to your client by doing
const client = new Client({ ws: { intents: ['GUILD_MEMBERS', 'GUILD_MESSAGES', 'GUILD_MESSAGE_REACTIONS']} });

Categories