Error code when running discord bot command (js) - discord.js

So this is the code:
const {
Client,
Attachment
} = require('discord.js');
const bot = new Client();
const cheerio = require('cheerio');
const request = require('request');
const token = 'My Token was here uwu';
const PREFIX = '//';
bot.on('ready', () => {
console.log('Oy lad the bot is online!');
bot.user.setActivity('users', { type: 'WATCHING' }).catch(console.error);
})
bot.on('message', message => {
let args = message.content.substring(PREFIX.length).split(" ");
switch (args[0]) {
case 'ping':
message.channel.send('other stuff')
break;
case 'cmds':
const Embed = new Discord.MessageEmbed()
.setColor(0x000000)
.setTitle("Comenzi:")
.setDescription("stuff")
message.channel.send(Embed);
message.channel.send('2.0')
break;
When I run my code, the bot starts but crashes when I type //cmds, with the following error (note that it was just a piece of the code)
const Embed = new Discord.MessageEmbed()
^
ReferenceError: Discord is not defined

You did not define Discord.
There are 2 solutions for this.
First soulution:
Line 1: const Discord = require("discord.js");
Second soulution:
Line 1: const {Client, Attachment, MessageEmbed} = require('discord.js');
Line 33: const Embed = new MessageEmbed()
Hope this helped you solving your Problem!

Related

Discord.js v13 bot cannot send message

Here is my code
I tried to run it and when i run the command nothing shows up
can someone pls help me with this
const Discord = require("discord.js");
const bot = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
const token =
"MTAxNzgyMjQ1MjU1MTc5NDgxOA.GjO8F-.VWCMsDKV5_YdP1w6gnEME6Jucd7BN9OADesM4s";
const prefix = "lb!";
bot.on("ready", () => {
console.log("Your bot is now online");
});
bot.on("messagecreate", (message) => {
if (message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLocaleLowerCase();
if (command === "hello") {
message.reply("Hi ${message.author}");
}
});
bot.login(token);
Message content is now a privileged intent. You may have to enable it for your bot in the Discord Developer Portal. You could (or should) also use interaction commands instead, so that you don't need that intent, as its purpose is to provide user privacy.
Hello Syed Faizan Ali Kazmi, Done Fix Your Code
// © Ahmed1Dev
const Discord = require("discord.js");
const bot = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
const token =
"MTAxNzgyMjQ1MjU1MTc5NDgxOA.GjO8F-.VWCMsDKV5_YdP1w6gnEME6Jucd7BN9OADesM4s";
const prefix = "lb!";
bot.on("ready", () => {
console.log("Your bot is now online");
});
bot.on("messagecreate", (message) => {
if (message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLocaleLowerCase();
if (command === prefix + "hello") {
message.reply("Hi ${message.author}");
}
});
bot.login(token);
// © Ahmed1Dev
With Regards,
Ahmed1Dev
I rewrite the code. Hope this will work
const Discord = require("discord.js");
const bot = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
const token = "never post your bot token online";
const prefix = "lb!";
bot.on("ready", () => {
console.log("Your bot is now online");
});
bot.on("messageCreate", (message) => {
if (message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (command === "hello") {
message.reply(`Hi ${message.author}`);
}
});
bot.login(token);
Also, if you didn't tick Message Content Intent, then go tick it in Discord Developer Portal

Why does this not send embed to channel id? discord.js

I've tried multiple ways to try to get it to send but it shows no error and doesn't send into channel.
const { MessageEmbed } = require('discord.js');
client.on("ready", async () => {
const embed = new MessageEmbed()
.setTitle(`Bot Status!`)
.setDescription(`${client.user.username} **Is Online!**`)
const channel = client.channels.cache.get('1006667208371490946')
channel.send({embeds: [embed]})
})
in the latest Discord.js version (V14) the correct way is
const { EmbedBuilder } = require('discord.js');
client.on("ready", async () => {
const embed = new EmbedBuilder()
.setTitle(`Bot Status!`)
.setDescription(`${client.user.username} **Is Online!**`);
const channel = client.channels.cache.get('1006667208371490946')
channel.send({embeds: [embed]})
});
If this is not solving your issue,
try to add a console.log(channel) just before channel.send({embeds: [embed]})
If the result is undefined, the problem is the bot can't get in your cache the channel you want. In that case you can fetch (Link to a post speaking about this)
In the other case the bot can't send a message in the channel, could be a permission issue but you can add a .then() / .catch() to see if error is showed or not.
Hope this can helps you
I think the problem is that you don't have the client to call.
const { MessageEmbed } = require('discord.js');
client.on("ready", async (/*client not found in here*/) => {
const embed = new MessageEmbed()
.setTitle(`Bot Status!`)
.setDescription(`${client.user.username} **Is Online!**`)
const channel = client.channels.cache.get('1006667208371490946')
channel.send({embeds: [embed]})
})
So try to add client
const { MessageEmbed } = require('discord.js');
client.on("ready", async(client) => {
const embed = new MessageEmbed()
.setTitle(`Bot Status!`)
.setDescription(`${client.user.username} **Is Online!**`)
const channel = client.channels.cache.get('1006667208371490946')
channel.send({embeds: [embed]})
})

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)

Removing all reactions from a message

I currently have my bot sending an embed then reacting to it.
I was wondering how I would go on removing all reactions from this message.
Code:
const { Client, MessageEmbed} = require('discord.js');
const client = new Client();
let prefix = '~';
client.on('message', async message => {
if (message.content.toLowerCase() === prefix + 'help'
const embed = new MessageEmbed()
.setTitle('Help Cmd')
.setDescription('example')
let msg = await message.channel.send(embed);
await msg.react('▶');
await msg.react('◀');
setTimeout(() => {
//Removing all reactions from msg
}, 5000);
}
You can use msg.reactions.removeAll();.

How to make a discord bot that gives a role on a command

I already know the basics like commands but I couldn't figure this out. I was looking at other answers and they didn't make sense. Here's my code (without the bot token in the const token):
const Discord = require('discord.js');
const bot = new Discord.Client();
const token = 'token';
const PREFIX = '!';
bot.on('ready', () => {
console.log('This bot is online!')
})
bot.on('message', message => {
let args = message.content.substring(PREFIX.length).split(" ");
switch (args[0]) {
case 'ping':
message.channel.send('pong!')
break;
}
})
bot.login(token);
You can try doing this :
switch (args[0]) {
case 'role':
var role = message.guild.roles.cache.find(role => role.name === "rolename");
message.member.roles.add(role);
break;
}
This example adds a role to the message author.

Resources