Discord bot won't turn on with node - discord.js

I am new to coding, and was trying to code a discord bot and when I tried to turn it on after using npm main.js I tried to use node ., the bot would not turn on and cmd would not react at all, this is the code I have at this time
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '/'
client.once('ready', () => {
console.log('BotMan 2 is online!')
});
client.on('message', message =>{
if(/message.content.startswith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.legnth).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
message.channel.send('pong!');
}
});
-
client.login('tag(My tag was here I removed it)');

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

My Discord Bot is online. My Prefix is "-". My -Ping Command is working but my -Discord command is not working. My Code is below. (Discord.js)

This is my Code ⬇️. My -Ping Command is working but my -Discord Command is not. My Bot is also online, Is there a problem?
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
const prefix = '-'
client.once('ready', () => {
console.log('Super-Lame is Online!')
});
client.on('messageCreate', message => {
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
message.channel.send('pong!');
} else if (command === 'Discord'){
message.channel.send('LecTroLight#9823');
}
});
client.login('My Token Here')
With toLowerCase(), you converted command to lowercase, so it will never equal "Discord". You should match against "discord".
i noticed you are using
.toLowerCase() and then you are checking the conditions for Discord. That wont work unless you check for discord.

discord js v13 message reply don't work (prefix)

I was making discord bot that reply the user word with prefix.
example the user said !Hello the bot will reply with Hello
The error was
(node:11208) DeprecationWarning: The message event is deprecated. Use messageCreate instead (Use node --trace-deprecation ... to show where the warning was created)
So I changed the code message to messageCreate but the bot didn't reply either and have no error in the console log what should I change the code of my script?
this is the source code
prefix is ! (It is in the config.json)
index.js
const {Client,Intents,Collection} = require("discord.js");
const client = new Client ({intents:[Intents.FLAGS.GUILDS,Intents.FLAGS.GUILD_MESSAGES,Intents.FLAGS.GUILD_MEMBERS]})
const fs = require("fs");
const { token, prefix } = require("./config.json");
client.commands = new Collection();
const commandFiles = fs.readdirSync("./commands").filter((file) => file.endsWith(".js"));
var data = []
for(const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
data.push({name:command.name,description:command.description,options:command.options});
}
client.once('ready', async () =>{
console.log(`Logged in as ${client.user.tag}`)
// await client.guilds.cache.get('874178319434801192')?.commands.set(data)
setInterval(() => {
const statuses = [
'mimc!',
'wtf',
'prefix = !'
]
const status = statuses[Math.floor(Math.random()*statuses.length)]
client.user.setActivity(status, {type: "PLAYING"})
}, 10000)
});
client.on("messageCreate", (message) =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift();
if(!client.commands.has(command)) return;
try{
client.commands.get(command).execute(message, args);
}
catch(error){
console.error(error);
}
});
client.login(token)
Hello.js (in the folder of commands)
module.exports = {
name: "Hello",
description: "Say Hi",
execute(message) {
return message.channel.send(`${message.author}, Hello.`)
},
};
The code of index.js (or main file) goes by:
const {Client, Intents, MessageEmbed, Presence, Collection, Interaction}= require ('discord.js')
const client = new Client({intents:[Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]})
const TOKEN = ''// get your TOKEN!
const PREFIX = '!'
const fs = require('fs')
client.commands = new Collection();
const commandFiles = fs.readdirSync('commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.on('ready', ()=> {
console.log('🕐 I am online!!')
client.user.setActivity('A GAME ', {type: ('PLAYING')})
client.user.setPresence({
status: 'Online'
})
})
client.on('message', message => {
if(!message.content.startsWith(PREFIX)|| message.author.bot) return
const arg = message.content.slice(PREFIX.length).split(/ +/)
const command= arg.shift().toLowerCase()
if (command === 'ping'){
client.commands.get('ping').execute(message,arg)
}
})
client.login(TOKEN)
After making a folder commands, add a new .js file, and name it as ping.js
The code for ping.js goes by -
module.exports={
name:'ping',
execute(message, arg){
message.reply('hello!!')
}
}

ReferenceError: message is not defined, how to solve?

I'm learning to code on discord.js so i may have gotten some things wrong, feel free to correct me if you find any issues. Right now this is the error I'm facing : ReferenceError: message is not defined
Here is the code :
const Discord = require('discord.js');
const client = new Discord.Client({
intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGES
]
});
const prefix = '!';
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('This bot is online!');
});
client.on('message', message =>{
if (!message.content.startsWith(prefix) || message.author.client) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'ping'){
message.channel.send('pong!');
} else if (command == 'joe'){
message.channel.send('mama ;)');
} else if (command == 'deez'){
message.channel.send('NUTS!!');
} else if (command == 'ur'){
message.channel.send('mama');
} else if (command === 'kick'){
client.commands.get('kick').execute(message, args);
} else if (command === 'ban'){
client.commands.get('ban').execute(message, args);
}
});
client.login('token')
Everything works for me when I run your code. The reason you get the error might be the fact that message is deprecated in Discord v13 as seen here Updating from v12 to v13. But that wouldn't raise an error and would just show that (node:30426) DeprecationWarning: The message event is deprecated. Use messageCreate instead
Change your event in client.on('message') to client.on('messageCreate') and see if it works. It would help a lot more if you could post the whole error message here

Discord JavaScript (node:1068) DeprecationWarning: The message event is deprecated

So i have been trying to get n simple pong message when I run the command, however I get this error
CODE:
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const prefix = '/stock';
client.once('ready', () => {
console.log('StockBot is online!');
});
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
message.channel.send('pong!');
} });
I blocked away my login code.
ERROR:
(node:1068) DeprecationWarning: The message event is deprecated. Use
messageCreate instead (Use node --trace-deprecation ... to show
where the warning was created)
enter image description here
Since v13, the message event has been deprecated. Replace your code with this:
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const prefix = '/stock';
client.once('ready', () => {
console.log('StockBot is online!');
});
client.on('messageCreate', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
message.channel.send('pong!');
} });

Resources