I'am using discord.js v14.7.1, here is my code
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GuildModeration", "GuildMessages", "GuildMessageTyping", "GuildMembers", "MessageContent"] });
client.once('ready', () => {
console.log("odpalony");
client.user.setActivity("BOT WERSJA BETA");
});
client.on('messageCreate', (message) => {
console.log(message.content);
});
client.login('*****');
I don't get any errors and the console is empty
The messageCreate event won't trigger without the "Guilds" intent, so you'll need to add it.
const client = new Discord.Client({
intents: [
'Guilds',
'GuildMessages',
'GuildMessageTyping',
'GuildMembers',
'GuildModeration',
'MessageContent',
],
});
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.
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.
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 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 the necessary discord.js classes
const { Client, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('The Bot is ready');
});
client.on('messageCreate', (message) => {
if(message.content === 'hello') {
console.log('hello buddy')
}
})
// Login to Discord with your client's token
client.login(token);
You need the GuildMessages intent.
Remplace this line :
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
by :
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] });
PS : I suggest you add the GuildMembers event too
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