How do I do to make my bot say something? - javascript

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

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.

Deleting all channels doesn't work discord.js

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());

Discord.js V14 - client.on message create not firing [duplicate]

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!.")
}
});
// ...

Discord.js messaging errors

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

Categories

Resources