My discord bot is not responding even though there is no error - discord.js

const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const config = require('./config.json');
client.once('ready', () => {
console.log("go");
})
client.on('message', (message) => {
if(message.content == "ping"){
message.channel.send("pong");
}
})
client.login(config.token);
There is no error in the code, but when I type ping, the bot does not respond, does anyone know why?

It's because you don't have the GUILD_MESSAGES intent enabled !
Replace that line :
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
with
const client = new Client({ intents: [Intents.FLAGS.GUILDS,Intents.FLAGS.GUILD_MESSAGES] });
As you can see from the official documentation :
GUILD_MESSAGES intent let us have events and one of them is MESSAGE_CREATE (emits when a message is created (sent))

Since after the new update of discord.js, providing intents is an integral part of Disocrd bots.
const {Client, Intents, Collection} = require('discord.js')
const client = new Client({intents:[Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]})

Related

How to Pin a Message in Discord

I am trying to make a discord bot for a small server that I am in, and I want it to pin a certain message to the channel that it was sent in. I have done a few bots before, but it seems that the syntax has changed since I last used it, and code that I was going to reuse no longer works. I have managed to get around some of those changes (like the intents,) but when I try to check for a sent message, it just does nothing. My current code
const { Client, Intents } = require("discord.js");
const { token } = require("./config.json");
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.once("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("message", (msg) => {
if (msg.content == "something goes here") {
msg.pin()
}
});
client.login(token);
I have tried supplementing messageCreate for message like I have seen a few people recommend, but it still seems to do nothing. Even changing the msg.pin() to console.log(msg) still shows nothing in the console
client.on("messageCreate", (msg) => {
if (msg.content == "something goes here") {
console.log(msg)
}
});
I do have the privileged intent toggle enabled, so I don't think that that is the problem
[privileged toggles]
Does anybody know what the problem here is, and how I could fix it? Any help is appreciated, thanks!
First of all, you have not enabled the GUILD_MESSAGES intent in your client so you need to add that by doing this:
const client = new Client({
intents: [
Intents.FLAGS.GUILDS
Intents.FLAGS.GUILD_MESSAGES
]
});
Other than that, the .pin() function is still there and there is no change in it. You can provide a reason as well. The .pin() function also returns a promise, so you will have to use .then() or await, so your final code might look like this:
const { Client, Intents } = require("discord.js");
const { token } = require("./config.json");
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
client.once("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("message", (msg) => {
if (msg.content == "something goes here") {
msg.pin().then(() => console.log)
}
});
client.login(token);
You can learn more about the .pin() function here => pin | discord.js
For Discord.js v13 use the following:
const { Client } = require("discord.js");
const { token } = require("./config.json");
const client = new Client({
intents: new Intents(32767)
});
client.login(token)
client.on("message", (ctx) => {
if (ctx.content == "something goes here") {
ctx.pin(ctx.id, "Pinned")
}
});

Discord.JS My bot don't turn online but there is a simple code

My bot don't turn online but there is a simple code:
const discord = require('discord.js')
const client = new discord.Client();
const token = ('my bot token')
client.on('ready', function(){
console.log('online')
})
client.login(token)
Try this:
const discord = require('discord.js')
const client = new discord.Client();
// you will want to define some intents if you want the bot to be able to do anything
// see https://discordjs.guide/popular-topics/intents.html#enabling-intents and https://discord.js.org/#/docs/main/stable/class/Intents
const token = 'my bot token' // took away the ()
client.on('ready', () => { //removed 'function' and added => to pass everything through
console.log('online')
})
client.login(token)

Welcome message in a specific channel when a user joins the server. (guildMemberAdd, discord.js)

How do I make it so that my bot sends a message in a specific channel when a user joins?
When I do this, it logs nothing: (Also here are my bot settings)
const Discord = require("discord.js");
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
const { MessageEmbed } = require('discord.js');
client.on("ready", () => {
console.log("Cloud Shield has been activated.")
});
client.on("guildMemberAdd", async (member) => {
console.log(member);
});
client.login(process.env.token)
This is because you are not requesting the GUILD_MEMBERS intent.
const Discord = require("discord.js");
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_MEMBERS"] });
const { MessageEmbed } = require('discord.js');
client.on("ready", () => {
console.log("Cloud Shield has been activated.")
});
client.on("guildMemberAdd", async (member) => {
console.log(member);
});
client.login(process.env.token)

How to fix 'CLIENT_MISSING_INTENTS' error in discord.js?

const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
console.log('Meep is online!');
});
client.login('my token was here');
When I run code, I get 'CLIENT_MISSING_INTENTS' error, how to fix it?
I was not getting this error in older discord.js versions, I started getting this error when I updated to the new discord.js version.
You can use any intents you want
with this intents you just can see guilds and messages in guilds
const Discord = require('discord.js');
const client = new Discord.Client({intents : [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGES
]})
client.once('ready', () => {
console.log('Meep is online!');
});
client.login('my token was here');
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_BANS,
Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
Intents.FLAGS.GUILD_INTEGRATIONS,
Intents.FLAGS.GUILD_WEBHOOKS,
Intents.FLAGS.GUILD_INVITES,
Intents.FLAGS.GUILD_VOICE_STATES,
Intents.FLAGS.GUILD_PRESENCES,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_MESSAGE_TYPING,
Intents.FLAGS.DIRECT_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS,
Intents.FLAGS.DIRECT_MESSAGE_TYPING
]
});

message event listener not working properly

I currently have the following code:
const Discord = require('discord.js');
const client = new Discord.Client({
partials: ['MESSAGE', 'CHANNEL', 'REACTION']
});
const db = require('quick.db')
client.on('message', async message => {
const DmLogger = require('./MainServer/dmRecieving.js');
DmLogger(client, message, Discord);
const levels = require('./MainServer/levels/main.js');
levels(client, message)
if (message.channel.id === configFile.LoggingChannel) return;
if (message.author.bot) return;
if (!message.guild) return;
let prefix = db.get(message.guild.id + '.prefix') || '~'
if (!message.content.startsWith(prefix)) return;
let args = message.content
.slice(prefix.length)
.trim()
.split(/ +/g);
if (message.content.toLowerCase() == prefix + 'info') {
const commandFile = require(`./Embeds/info.js`);
return commandFile(client, message);
}
if (message.content.toLowerCase() == prefix + 'help') {
const commandFile = require(`./Embeds/help.js`);
return commandFile(client, prefix, message);
}
if (message.content.toLowerCase() == prefix + 'fonts') {
const commandFile = require(`./Commands/font.js`);
return commandFile(client, msg, args, prefix, message);
}
if (message.content.toLowerCase().startsWith(prefix + 'setup')) {
const commandFile = require(`./Commands/setup/main.js`);
return commandFile(client, message, db);
}
});
Whenever I send a message that includes a command the event listener is firing however it is not detecting the message content.
This module has been working fine for the passed few months its just suddenly erroring after I reinstalled the discord.js module.
In discord.js v13, it is necessary to specify an intent in new discord.Client(). Events other than the specified intent will not be received.
ClientOptions
Intents#FLAGS
To receive the message event, you will need the GUILDS and GUILD_MESSAGES intents.
You mean...
const client = new Discord.Client({
partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES]
});
Or to receive all (including privileged intent) events...
const client = new Discord.Client({
partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
intents: Object.keys(Discord.Intents.FLAGS)
});
Also, in discord.js v13, the message event has been deprecated, so it is recommended to replace it with messageCreate.
- client.on('message', async message => {
+ client.on('messageCreate', async message => {
Since discord.js v13, you need to declare what your bot is doing with Intents
Here's how you declare them :
const client = new Discord.Client({
partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
intents: ['GUILD_MESSAGES']
})
The GUILD_MESSAGES Intent is essential because it allows your bot to read and reply to messages in guilds.
However, you should check the documentation to see if your bot needs some more Intents

Resources