ReferenceError: message is not defined, how to solve? - discord

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

Related

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.

TypeError: Cannot read properties of undefined (reading 'execute')

I have an error
CODE:
const prefix = '-';
import fs from 'fs'
client.commands = new DiscordJS.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.cjs'));
for(const file of commandFiles){
const command = import(`./commands/${file}`);
client.commands.set(command.name, command);
}
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 === `test`){
client.commands.get('test').execute(message, args);
}
});
My test.cjs CODE:
module.exports = {
name: 'test',
description: "This is a test",
execute(message, args){
message.channel.send('Test is working');
}
}
Does any1 know how to help??
The Console Error Message is
If any1 can help that would be amazing!
Have a good day!
https://i.stack.imgur.com/G6mOV.png
.cjs is CommonJS file, but your code can run on JavaScript.
Try changing your extension to .js.

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!');
} });

Discord bot won't turn on with node

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)');

How to make bot not stop after error? Discord.js

I have script which have to kick user with reason.
Syntax:
$kick #user Reason
but when I type:
$kick sometext nexttext
I got error in console:
TypeError: Cannot read property 'kick' of undefined
and bot stop...
How can I edit this script so that after entering an incorrect value, an error will not pop up turning off the bot and bot will send message to channel eg. "Incorrect value"?
Script:
const discord = require('discord.js');
const client = new discord.Client;
const prefix = "$";
client.on('message', function(message) {
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === "kick") {
let member = message.mentions.members.first();
let reason = args.slice(1).join(" ");
member.kick(reason);
message.delete();
client.channels.cache.get('737341022782357566').send("User <#" + member.id + "> with id " + member.id + " has been kicked by <#" + message.author.id + "> with reason " + reason)
}})
client.login('token');
in javascript to execute a code that have chances to chrash, you can use the statment
try {
//code to test
} catch(err) {
//code if it crash
}
add a if (member) to test if there was a mention.
In the else, you can send your "bad command" message
you should also only trigger the bot if the message starts with your prefix
const discord = require('discord.js');
const client = new discord.Client;
const prefix = "$";
client.on('message', function(message) {
if (!message.content.startsWith(prefix)) { return }
//this line prevents the bot to execute every message
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === "kick") {
let member = message.mentions.members.first();
let reason = args.slice(1).join(" ");
if (member) { // add this
member.kick(reason);
client.channels.cache.get('737341022782357566').send(`User ${member.user} with id: ${member.id} has been kicked by ${message.author} with reason: ${reason}`);
} else {
message.reply("invalid parameters for $kick")
}
message.delete();
}
})
client.login('token');

Resources