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.
Related
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.
I'm coding a bot that deleting all channels of a Discord server.
Here my code :
const { Client, GatewayIntentBits } = require("discord.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
client.on("ready", () => {
console.log("Bot Ready");
});
client.on("messageCreate", (message) => {
if (message.author.bot) return;
console.log(message);
if (message.content === ".setup") {
message.guild.channels.forEach((channel) => channel.delete());
}
});
client.login(
"token"
);
When I launch it and I execute the command, nothing is happening. My bot is Administrator.
Someone can help me please ?
Alden Vacker
It has a very easy fix.
Just put cache like this:
message.guild.channels.cache.forEach((channel) => channel.delete());
This question already has an answer here:
message.content doesn't have any value in Discord.js
(1 answer)
Closed 4 months ago.
I'm trying out V14 of Discord.js, and there are so many new things! But this whole intents thing, I'm not getting for some reason. My messageCreate is not firing, and from other posts, I believe it has something to do with intents. I've messed around with intents, but nothing seems to work. Here is my code:
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("messageCreate", (message) => {
console.log("new message...")
console.log(message.message)
console.log(message.content)
if (message.mentions.users.first() === client) {
message.reply("Hey!.")
}
});
client.login(process.env.token);
Thanks!!
Figured it out! You need to turn on message content intents in the developer portal:
Also, you need to have messageContent intent at the top:
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [
GatewayIntentBits.DirectMessages,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildBans,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,] });
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("messageCreate", (message) => {
if (message.content === "<#1023320497469005938>") {
message.reply("Hey!")
}
});
client.login(process.env.token);
As for getting the ping from the message, not my initial question but something I needed to fix, just use
if (message.content) === "<#BotID>" {CODE HERE}
You need the GuildMessages intent.
Note: You do not need the MessageContent intent in messages that ping the bot, as bots can receive content from messages that ping them without the intent.
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages
]
});
// ...
client.on("messageCreate", (message) => {
// ...
if (message.mentions.users.has(client.user.id)) { // this could be modified to make it have to *start* with or have only a ping
message.reply("Hey!.")
}
});
// ...
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
So I am learning discord.Js and I am trying to figure out why my message.reply function is not working. I created an event for the bot to listen to messages and when a message with the content of "hello" is sent it should reply with "hello buddy" here is the code:
require('dotenv').config()
const discord = require('discord.js')
const client = new discord.Client({
intents: []
});
client.login(process.env.TOKEN);
client.on('ready', () => {
console.log(`${client.user.tag} has logged in`);
});
client.on('message', function(message){
if(message.content === 'hello') {
message.reply('hello buddy')
}
});
The bot in discord has all the permissions required to send messages in the server I am testing it in.
Your bot does not detect messages. You need GUILDS and GUILD_MESSAGES intents
const client = new discord.Client({
intents: ["GUILDS", "GUILD_MESSAGES"]
})
Also, the message event is deprecated. Use messageCreate instead
client.on('messageCreate', (message) => {
if(message.content === 'hello') {
message.reply('hello buddy')
}
})
Note that I used an arrow function. They are cleaner syntax and you should use them in callbacks