I'm trying to create my own discord bot to learn more code and to make features for my server. I've been following this tutorial on how to get started but when I run the nodemon --inspect index.js, I get this message and it doesn't work as intended (see picture).
UnhandledPromiseRejectionWarning: AbortError: The user aborted a request.
It's the most basic of command and I've just copy-pasted so I don't understand what I've done wrong.
Here's the code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('pong');
}
});
client.login('TOKEN');
Related
i have a problem with discord bot. I just triing to get it working, but i cannot figure out where this error is.
const {discord, Intents} = require('discord.js');
const client = new discord.client();
client.on('ready', () => {
console.log(client.user.tag);
});
client.login("token");
This is my entire code without package.json
Error message looks like this: Cannot read properties of undefined (reading 'client')
Thanks for help.
Methods you are using seem to be deprecated on Discord.js v13 and posterior.
You could instead use:
const { Client } = require('discord.js');
const client = new Client({ intents: 32767 });
client.on('ready', () => {
console.log('We are up!');
});
client.login(process.env.SUPER_SECRET_TOKEN);
If you need help moving your entire codebase to Discord.js v13 or v14, you can always check the docs and the official guide.
My discord bot is not coming online.
Here is my .js file
const Discord = require("discord.js");
const client = new Discord.Client();
const TOKEN = "my-token-is-here";
client.on("message", message => {
if (message.content === "Hello Discord Bot") {
message.channel.send("Hi There!");
}
{
console.log("Bot Is Ready!");
}
});
client.login(TOKEN)
The error I am getting is
SyntaxError: Unexpected token ?
You should never post any private tokens publicly. Be sure to make a new token on the Discord-Developer-Portal and don't continue using this one!
To your question:
It seems like the lines
{
console.log("Bot Is Ready!");
}
cause the syntax error. You probably want to move that statement into it's own event handler for the event "ready" (triggered when DiscordJs has finished setting up the bot-client for you).
client.on("message", message => {
if (message.content === "Hello Discord Bot") {
message.channel.send("Hi There!");
}
});
client.on("ready", () => {
console.log("Bot Is Ready!");
});
client.login(TOKEN)
Based on your comments you've installed Discord.js version 13.1.0 but your NodeJS version is lower than 16.6. There are two options for you to solve this problem.
Option 1 (most recommended)
You'll need to update your NodeJS version to 16.6 or higher. You can do this by downloading a version of NodeJS at the NodeJS website and complete the setup again.
Once you've completed the setup you should be able to run your Discord bot again.
Option 2
If you don't want to update your version you can install Discord.js version 12.5.3. This version is not as stable as version 13.1.0 is but it is possible. You can do this by using the npm command npm install discord.js#12.5.3. After installing the 12.5.3 version you should be able to run your bot again.
Also like Luca Mertens noticed your breaks are incorrectely. You should replace them to
client.on('ready', () => console.log(`Bot is ready`));
client.on("message", message => {
if (message.content === "Hello Discord Bot") {
message.channel.send("Hi There!");
}
});
i'm trying to code a simple bot for my server on discord but this error happened:
ReferenceError: Intents is not defined
My code is really simple:
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.login('TOKENcode');
bot.on("message", (message) =>{
console.log('hello world!');
})
what should i do? i just want to write a message on Discord and make my Visual Studio Code prompt say "hello world!"
According to the Discord.js documentation you need to pass Intents to you client initialization. New Discord rules for bot developers has been set that every bot creator needs to pass intents.
For example, this is a simple construction, copied from the documentation:
// Define Client and Intents
const { Client, Intents } = require('discord.js');
// Set flags for your bot
// All flags: https://discord.js.org/#/docs/main/stable/class/Intents?scrollTo=s-FLAGS
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES
]
});
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
// Receive messages sent in your server.
client.on("messageCreate", message => {
console.log('Hello World!');
});
client.login('token');
! Note that Discord.js version 13, requires Node.js v16 to be installed on your machine. Make sure you have installed this by typing node -v in your console.
This question already has answers here:
message event listener not working properly
(2 answers)
Closed 1 year ago.
my discord.js bot has version 13.1.0, and my node's version is 16.7.0. I entered these commands in the terminal : npm init to create package.json and npm install discord.js to install discord package.
I writed the code in index.js, and I created config.json to put the token there.
When I run then the code it shows me 'Ready!' in the console and being online in Discord. I also can change the status and the activity of the bot without a problem. The problem here is the bot doesn't send or reply to a message.
Here's the code bellow
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.once('ready', () => {
console.log('Ready!');
});
client.on ('messageCreate', (message) => {
if (message.content === 'hello') {
message.reply('Hello')
};
});
client.login(token);
this's config.json's code just in case.
{
"token": "my_bot_token!"
}
The issue is caused by missing intents. To listen to messages, you need to specify the GUILD_MESSAGES flag:
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
I am running this with Command Prompt.
I have already tried going into Command Prompt, saving, and then using alternatives, like typing "node .", or "node index", or "node index.js".
const Discord require('discord.js');
const bot = new Discord.Client();
const token 'XXXXXXXXXXXXXXXXX';
bot.on('ready', ()=>{
console.log('this bot is online :D');
})
bot.on('message', msg=>{
if (msg.content === "HELLO") {
msg.reply('HELLO FRIEND!')
}
})
bot.login(token);
It should be that when I go to Discord, it says my bot is on, but instead, in Command Prompt, it says this: "SyntaxError: Missing initializer in const declaration" and it doesn't say my bot is online in Discord.
You're missing a couple equal signs in your code.
const Discord = require('discord.js'); // Equal sign was missing here
const bot = new Discord.Client();
const token = 'SUPER SECRET YOU SHOULD NEVER REVEAL TO THE WORLD'; // Equal sign was missing here
I tested with your token and the bot worked.