Discord Bot isn't responding - javascript

For some reason, the basic ping pong command isn't working. I copied and pasted it from a website, but I am not sure what is wrong with it.
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
client.login('hidden')
client.once('ready', () => {
console.log('Ready!');
});
client.on('interactionCreate', interaction => {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
if (commandName === 'ping') {
interaction.reply('Pong.');
}
});
There is no log error which makes it weirder. I know it is connecting because the bot appears online when I do a node index.js in the cmd panel.

Firstly, I recommend that you fix the indentation at the beginning and move the client.login call to the end, as that's how the discord.js docs do it.
Additionally, are you sure you're using Discord.js v13? These events were added in v13, so if you run it with Discord.js v12, your bot will run, but your functions will never be called.
Otherwise, your code looks good (to me), but I'm not an expert.

Related

How can I make bot receive DM on Discord js v14? [duplicate]

This question already has an answer here:
message.content doesn't have any value in Discord.js
(1 answer)
Closed 4 months ago.
module.exports = {
name: 'messageCreate',
execute(message) {
if (message.channel.type == 'DM') {
console.log('Dm recieved')
client.channels.get('1026279100626767924').send(message);
}
}
};
I created event handling by referring to the Discord JS guide. I want my bot to receive a DM and send that message to my admin channel.
But the bot is not recognizing the DM
What should I do
(I'm using a translator)
You can use messageCreate event for listening your bot's DMs.
client.on("messageCreate", async message => {
if (message.guild) return;
console.log(`Someone sent DM to me => ${message.content}`);
await client.channels.cache.get(CHANNEL_ID).send(messsage.content);
});
You need to include partials Channel and Messages configurations on creating client:
const client = new Client({
intents: [
GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent
],
partials: [
Partials.Channel,
Partials.Message
]
})
That solved it for me!
There could be a couple problems with this.
First off, it could be your Intents.
Intents allow your bot to process certain events.
The intents that you will want are:
DirectMessages
MessageContent (this one may not be required, I am unsure).
So, go to where you initialize your client. It should look something like this:
const Discord = require("discord.js");
const client = new Discord.Client();
and change it into this:
const Discord = require("discord.js");
const { GatewayIntentBits, Client } = Discord;
const client = new Client({
intents: [
GatewayIntentBits.DirectMessages
GatewayIntentBits.MessageContent
]
});
The second issue could be that your message handler isn't detecting it properly.
Like the other answer says, you should just determine if there is no Guild attached to the message.
client.on("messageCreate", async(message) => {
if(!message.guild) {
client.channels.cache.get("YOUR_ID").send(message);
}
});
There could be other issues, but those two are the most probable. Considering you're not getting any errors, I'd presume it's the first one, the issue with Intents.
Hope this helps!

Discord.js bot not logging in

I made a discord bot and i have no idea what's wrong with my code i mean look:
const Discord = require("discord.js");
const client = new Discord.Client({
intents: [
"GUILDS",
"GUILD_MESSAGES"
]
});
client.login('you thought haha')
I tried various tutorials but it's still not working
It's looking like about the intents and there's simple mistake as i've seen. If you wan't to open all intents i recommend you this;
const client = new Discord.Client({
intents: new Discord.Intents(32767)
});
If you wan't specific intents then you should make them like;
const client = new Discord.Client({
intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES]
})
Also don't forget to open your intents from bot section on your developer panel!
More info here on docs.
I'm assuming you have already installed Node.js and discord.js, and also have a bot setup.
If you check discord.js Guide, they provide you the following code to start your bot:
// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
// Login to Discord with your client's token
client.login(token);
Then, open your terminal and run node index.js (assuming that your file name is index.js) to start the process. If you see "Ready!" after a few seconds, you're good to go!
I strongly recommend for you to follow this guide, it helped me a lot with the first steps with the discord bot.
Also, make sure that you have the right scopes selected for your bot:
I think the error is on the client declaration. You have to capıtalize the first letter of "intents". So the code becomes:
const Discord = require("discord.js");
const client = new Discord.Client({
Intents: [
"GUILDS",
"GUILD_MESSAGES"
]
});
client.login("your token here");
And also please make sure that on your discord developers page you have your intents turned on.

How do I logout my Discordbot in JS savely with a command

I built a basic Discord bot which I want to go offline when I type the corresponding command on the Discord server "!logout". I searched for answers, but those who used discord.js weren't responsed...
If I use client.logout() it throws an error and crashs with
TypeError: client.logout is not a function
and it won't even call my error function.
Maybe the answer is simple but I can't find it anywhere.
Here's my basic code:
require('dotenv').config()
const Discord = require('discord.js')
const client = new Discord.Client({
intents: ["GUILDS", "GUILD_MESSAGES"],
})
client.on('ready', () => {
console.log('Bot is ready');
});
client.on("messageCreate", msg => {
if (msg.content === '!logout'){
client.logout(() => {
msg.reply('couldn^t go offline')
})
}
})
client.login(process.env.BOT_TOKEN)
Thank you! :)
Try using the function <Client>#destroy()
If you want to exit Node.js, you can then use process.exit(0)
Documentation
process.exit(0)
From the docs:
process.exit([exitcode])
Ends the process with the specified code. If
omitted, exit uses the 'success' code 0.
To exit with a 'failure' code:
process.exit(1); The shell that executed node should see the exit code
as 1.

Event messageCreate not firing/emitting when I send a DM to my bot (discord.js v13)

I made this code for logging the DMs that are sent to my bot:
client.on('messageCreate', async message => {
if (message.author.bot) return;
const attachment = message.attachments.first()
if (message.channel.type === 'DM') {
console.log(message.content)
const dmLogEmbed = new MessageEmbed()
.setColor("RANDOM")
.setTitle(`${message.author.tag} dmed the bot and said: `)
.setDescription(message.content)
.setFooter(`User's id: ${message.author.id}`)
if (message.attachments.size !== 0) {
dmLogEmbed.setImage(attachment.url)
}
client.channels.fetch("852220016249798756").then((channel) => {
channel.send({ embeds: [dmLogEmbed] })
})
}
});
But when updating to discord.js v13 it didn't work anymore, for what I understood the only change is that the 'dm' channel type isn't 'dm' anymore but 'DM', so I changed it in my code, but it is still not working and I don't really know why.
Make sure you have DIRECT_MESSAGES in your client's intents.
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES"] });
There is a breaking change in the Discord API v8. For reference see here.
On Discord API v8 and later, DM Channels do not emit the CHANNEL_CREATE event, which means discord.js is unable to cache them automatically. In order for your bot to receive DMs, the CHANNEL partial must be enabled.
So we need to enable the partial CHANNEL as well. Keep in mind that when dealing with partial data, you often want to fetch the data, because it is not complete. However this doesn't seem to be your case, if you are using the messageCreate event. The message received is not partial nor message.channel. To check if something is partial, you can use the .partial property. For example Message.partial or Channel.partial.
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES"], partials: ["CHANNEL"] });
Now it should work as good old discord.js v12.

How do i make reactions working after restart?

So i am trying to make an ticket bot, using discord.js . By clicking on an reaction, users will be added to an ticket channel, where they can get help. The ticket system itself works perfectly fine, exactly like i want it to work. But the problem is, after i restart the bot, nothing happens anymore when you click on the reaction. Here is the relevant code:
let helpdeskmessageEmbed = await message.channel.send(helpdeskticketEmbed)
helpdeskmessageEmbed.react("1️⃣")
helpdeskmessageEmbed.react("2️⃣")
helpdeskmessageEmbed.react("3️⃣")
client.on('messageReactionAdd', async (reaction, user) => {
const categoryID = "820920950114615318"
if (reaction.message.partial) await reaction.message.fetch()
if (reaction.partial) await reaction.fetch()
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id == channel ) {
if(reaction.emoji.name === "1️⃣") {
reaction.users.remove(user)
message.guild.channels.create(`report-${user.username}`).then(
The rest of the code isn't relevant to my question. Could anyone tell me how to fix this.
Note: I am using an command handler, if that is relevant
I've had this exact issue some time ago. Long story short, you need to use partials.
Be aware, that:
"Partials do not have all the information necessary to make them fully functional discord.js structures, so it would not be a good idea to enable the functionality by default. Users should know how to handle them before opting into this feature."
I see that you have tried to use them, but you need to add the ability to handle partials in your code. Change const client = new Discord.Client(); into:
const client = new Discord.Client({
partials: [`MESSAGE`, `CHANNEL`, `REACTION`], //unsure if message and channel are needed, feel free to test it out
});
This basically allows handling all messages, even those posted before your bot started. Without the change above, your code will not detect partials.
client.on("messageReactionAdd", async (reaction, user) => {
let msg = reaction.message;
if (msg.partial) {
await msg.fetch().catch(() => {});
}
//your code goes here
});
Answering your comment:
I have read your replit code and it looks fine, I can only suggest to change this:
const Discord = require ('discord.js');
const client = new Discord.Client({ partials: [`MESSAGE`, `CHANNEL`, `REACTION` ]});
into this:
const { Discord } = require('discord.js');
const client = new Discord({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });

Categories