This is my code :
const Discord = require("discord.js");
const client = new Discord.Client();
client.login("<Discord Token Here>");
console.log("start");
client.on('message', (message) => {
if (message.content == "бот") {
message.channel.send("моя_статья");
}
});
My problem is on this line -
client.login("<Discord Token Here>");
ReferenceError: client is not defined
please can you help me with this.
Make sure you follow all steps Discord.js
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
console.log("start");
});
client.on('message', message => {
if (message.content == "бот") {
message.channel.send("моя_статья");
}
});
client.login('<Discord Token Here>');
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.
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.
const Discord = require('discord.js');
// fs
const fs = require('fs');
// path
const path = require('path');
// config
const config = require('./config.json');
// config.json
const token = config.token;
// config config.json
const PREFIX = config.prefix;
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: \[Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES\] })
client.commands = new Discord.Collection();
// message
client.on('message', message =\> {
// message not sent by bot
if (!message.author.bot) {
// if the message starts with the prefix
if (message.content.startsWith(PREFIX)) {
// split the message into an array
const args = message.content.slice(PREFIX.length).split(/ +/);
// command is the first element of the array
const command = args.shift().toLowerCase();
// ping command
if (command === 'ping') {
// send a message to the channel
message.channel.send('pong');
}
// success message after login
client.on('ready', () =\> {
console.log(`Logged in as ${client.user.tag}!`);
})
// token config file
client.login(config.token);
so I run node index.js it's supposed to shown Logged in as "my bot name here" on the VSC terminal and should be online on the discord, but it doesn't and I can't figure it out, mind anyone help
Formatting will save your life, Bro:
Also, I'd recommend less comments. It's a lot of noise. I refactored this stuff out. Look how much nicer your code looks:
const { token, prefix: PREFIX } = require('./config.json');
const { Collection, Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] })
client.commands = new Collection();
client.on('message', message => {
if (message.author.bot) return
if (!message.content.startsWith(PREFIX)) return
const args = message.content.slice(PREFIX.length).split(' ');
const command = args.shift().toLowerCase();
if (command === 'ping') {
message.channel.send('pong');
}
})
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
})
client.login(token);
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
I want to use discord.js to send messages to a certain channel. Here is the code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log('ready');
});
const channel = client.channels.cache.get('my id');
channel.send('content');
client.login(process.env.DISCORD_TOKEN);
But for some reason it throws this error:
TypeError: Cannot read property 'send' of undefined.
What am I doing wrong?
The problem is that client.channels.cache is empty until the Client is ready.
To fix your issue, you can place your code into the ready event of Client.
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log('ready');
const channel = client.channels.cache.get('my id');
channel.send('content');
});
client.login(process.env.DISCORD_TOKEN);