Discord.js messaging errors - javascript

I've been recently working on my second Discord.JS bot. I coded some basics and tested it. However, when I use the .verify command, it didn't make any reactions. Please help!
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const { token } = require('./config.json');
const prefix = "."
client.on("ready", () => { console.log(`Logged in as ${client.user.tag}!`) })
client.on('message', message => {
if (message.content.startsWith(`${prefix}verify`)) {
message.channel.send('SUCCESFULLY VERIFIED');
}
})
client.login(token);

maybe after message.channel.send('SUCCESFULLY VERIFIED') put .catch(console.error)
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const { token } = require('./config.json');
const prefix = "."
client.on("ready", () => { console.log(`Logged in as ${client.user.tag}!`) })
client.on('message', message => {
if (message.content.startsWith(`${prefix}verify`)) {
message.channel.send('SUCCESFULLY VERIFIED').catch(console.error)
}
})
client.login(token);
actually we can't do anythink because you don't send the error

You should add the GUILD_MESSAGES intent.
In discord.js v13 they changed client.on('message') to client.on('messageCreate').
Documentation

Related

discord bot when connected is not responding according to my code

I am building a Discord bot and I want my bot to respond pong after I type ping but it's not responding. My token is also correct. My code gets connected with bot but there is no response.
const Discord = require("discord.js")
const { Client, GatewayIntentBits, Partials } = require('discord.js');
const { Intents } = Discord;
const client = new Discord.Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
})
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`)
})
client.on("message", msg => {
if (msg.content === "ping") {
msg.reply("pong");
}
})
client.login("Its correct but I cant reveal")
You also need the MessageContent and GuildMembers intent to be enabled when you want to read the message contents in your messageCreate event.
const Discord = require("discord.js")
const { Client, GatewayIntentBits, Partials } = require('discord.js');
const client = new Discord.Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`)
});
client.on("messageCreate", msg => {
if (msg.content === "ping") {
msg.reply("pong");
}
});
client.login("Your token");
Also, double check on your developer portal if the intents are enabled.
! Not sure which version of Discord.js you are using, but the event for receiving message is messageCreate. The event message is deprecated and has been removed in the latest version already.

Discord bot doesn't respond to message

I have put MessageContent in intents, but it does not work. Here is what I've tried:
const { Client, GatewayIntentBits } = require('discord.js');
const logger = require('winston');
const bot = new Discord.Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
const token = "69420"
bot.on('ready', () => {
console.log('bot do online')
});
bot.on('message', msg => {
if (msg.content === "hello") {
msg.channel.send("helo man")
}
})
bot.login(token)
If you are using discord.js#13.xx.x+, you have to use messageCreate event, not message.
bot.on('ready', () => { // when the bot is ready and online
console.log('bot is now online!')
});
bot.on('messageCreate', message => { // when there is a message sent
if (message.content === "ping") {
message.channel.send("pong!")
}
})
message is for discord.js#12+
messageCreate is for discord.js#13+
Make sure you have message content privillaged intent turned on, on Discord developers portal
Also, remember to not share your token anywhere, and put it in a safe file.

noblox.js and discord.js, currently using discord.js and got a intent error, v13

Was watching a tutorial on making a discord.js and noblox.js bot. error is something about intents not being a constructor or whatever. tutorial playlist isnt even helpful like the discord server. im just following the tutorial to set up a bot account and some slash commands with noblox.js (a roblox js API i guess or a wrapper idk)
code:
const { Client, Intents } = require('discord.js')
const noblox = require('noblox.js')
const { Token } = require('./config.json')
// Create a new client instance
const intents = new Intents([
'GUILDS',
'GUILD_MEMBERS',
'GUILD_MESSAGES'
])
const bot = new Client({ Intents : intents })
bot.login(Token)
bot.on('ready', () => {
console.log(`${bot.user.tag} has booted up.`)
})
error:
TypeError: Intents is not a constructor
at Object.<anonymous> (/home/runner/ImpishDefiantClasses/index.js:7:17)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
IDE: Replit
First of all, You should require intents:
const { Client, Intents } = require("discord.js");
const { Token } = require("./config.json")
const bot = new Client({
intents: [Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.MESSAGE_CREATE]
});
Then, the things we want to do when the bot is ready:
bot.on('ready', () => {
console.log(`${bot.user.tag} is now online!`)
})
Also, Make sure you put bot.login(Token) at the end of your code.
At the end, If you want to receive messages, you need to listen to messageCreate event:
client.on("messageCreate", async (message) => {
if (message.author.bot) return;
if (message.content.startsWith('!ping')) {
message.reply('Pong!')
}
});
So your code should look like this:
const { Client, Intents } = require("discord.js");
const bot = new Client({
intents: [Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.MESSAGE_CREATE]
});
bot.on('ready', () => {
console.log(`${bot.user.tag} is now online!`)
})
client.on("messageCreate", async (message) => {
if (message.author.bot) return;
if (message.content.startsWith('!ping')) {
message.reply('Pong!')
}
});
bot.login(Token);
More info on intents.

How do I do to make my bot say something?

so my code is like that and i wanted to do something that says "pong" when we say "ping". I don't understand why it doesn't work and I'm a begginner, ty !
require('dotenv').config();
const { Client, GatewayIntentBits } = require('discord.js')
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
]
})
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('pong');
}
});
client.login(process.env.DISCORD_TOKEN);
You need to add GatewayIntentBits.GuildMessages to your intents
require('dotenv').config();
const { Client, GatewayIntentBits } = require('discord.js')
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages
]
})
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('pong');
}
});
client.login(process.env.DISCORD_TOKEN);
Also you need to enable message content intent
But I recommend you to switch to slash commands

ReferenceError: Discord client is not defined

This is my code :
const Discord = require("discord.js");
const client = new Discord.Client();
client.login("<Discord Token Here>");
console.log("start");
client.on('message', (message) => {
if (message.content == "бот") {
message.channel.send("моя_статья");
}
});
My problem is on this line -
client.login("<Discord Token Here>");
ReferenceError: client is not defined
please can you help me with this.
Make sure you follow all steps Discord.js
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
console.log("start");
});
client.on('message', message => {
if (message.content == "бот") {
message.channel.send("моя_статья");
}
});
client.login('<Discord Token Here>');

Categories

Resources