Discord.js -- message is not defined - node.js

const client = new Discord.Client();
const ayarlar = require('./ayarlar.json');
var prefix = ayarlar.prefix;
client.on('ready', () => {
console.log(`Botun olan ${client.user.tag}sunucuya giriş yaptı ve artık aktif!`);
});
client.on('message', msg => {
if (msg.content.toLowerCase() === 'salak aziz') {
msg.channel.send('nerden bildin? helal olsun');
}
if (message.content === `${prefix}mal`) {
message.channel.send('Aziz Mert');
} else if (message.content === `${prefix}adam`) {
message.channel.send('Ata');
}
});
Im a beginner.
The error is 'ReferenceError: message is not defined' How can i fix it?
Thanks.

That is because you defined the parameter of the message event as msg not message
const client = new Discord.Client();
const ayarlar = require('./ayarlar.json');
var prefix = ayarlar.prefix;
client.on('ready', () => {
console.log(`Botun olan ${client.user.tag}sunucuya giriş yaptı ve artık aktif!`);
});
client.on('message', message => {
if (message.content.toLowerCase() === 'salak aziz') {
message.channel.send('nerden bildin? helal olsun');
}
if (message.content === `${prefix}mal`) {
message.channel.send('Aziz Mert');
} else if (message.content === `${prefix}adam`) {
message.channel.send('Ata');
}
});

Related

tag a user using discord node js

I want to make a command that tags a user when there name got mentoined. But now the bot only tags the username and not a nickname the person has in the server
client.on("message", message => {
if(message.content === 'test'){
message.channel.send("works")
console.log("logged the message")
}
})
client.on("message", message => {
const list = message.guild;
list.members.cache.each(member => {
pinging = member.user.id
console.log(member.user.username)
if(message.content.includes(member.user.username)){
message.channel.send(`<#${pinging}>`)
}
})
})
can anyone help?
First thing, you only want to have one listener for the message event, so try this code:
client.on("message", message => {
if (message.content === 'test'){
message.channel.send("works")
console.log("logged the message")
}
// insert choices from below here
})
If you want it to listen for a member being tagged
const mentionedMember = message.mentions.members.first()
if (mentionedMember){
message.channel.send(`${mentionedMember}`)
}
Now if you want it to listen for username instead of #username
const args = message.content.slice().trim().split(' ')
const guild = message.guild
args.forEach(arg => {
const member = guild.members.cache.find(member => member.user.username.toLowerCase() === arg.toLowerCase())
if (member) {
message.channel.send(`${member}`)
} else {
return
}
})
To listen for displayName
const args = message.content.slice().trim().split(' ')
const guild = message.guild
args.forEach(arg => {
const member = guild.members.cache.find(member => member.displayName.toLowerCase() === arg.toLowerCase())
if (member) {
message.channel.send(`${member}`)
} else {
return
}
})

Parsing error: Unexpected token Client Discord.js

Ok so i have this problem, i am new at node.js, please help, the error says: "Parsing error: Unexpected token Client" i've tried to do something for a while and i don't know what to do.
const {Client, Intents} = require('discord.js')
const client = new Client({intents: ["GUILD_MESSAGES"]})
client.login("TOKEN")
client.on('ready', function () {
}
Client.on("message", message => { try{
let server1 = "861068256844316683";
let server2 = "936852675352481842";
let channel1 = "861068256844316686";
let channel2 = "936852675352481845";
let emojis = [":joy:",":rofl:",":heart_eyes:",":smiling_face_with_3_hearts:",":sunglasses:",":nerd:",":face_with_monocle:",":kissing_heart:",":pensive:",":rage:",":face_with_symbols_over_mouth:",":hot_face:",":cold_face:",":thinking:",":flushed:",":scream:",":yawning_face:",":lying_face:",":heart_eyes_cat:",":joy_cat:",":scream_cat:"];
let msj = message.content;
if (msj.includes("#everyone")) return;
if (msj.includes("#here")) return;
if (msj.includes("<#&")) return;
if (msj.includes("http")) return;
if(message.channel.id === channel1){
let emoji = emojis[Math.floor(Math.random() * 21)];
if(message.member.id === "947818875137953863") return;
let chatGlobal = Client.guilds.cache.find(g => g.id === server2).channels.cache.find(c => c.id === channel2);
chatGlobal.send(`**${emoji} ${message.member.user.username}:**\n> ${msj.replace("\n","\n> ")}`);
}
if(message.channel.id === channel2){
let emoji = emojis[Math.floor(Math.random() * 21)];
if(message.member.id === "947818875137953863") return;
let chatGlobal = Client.guilds.cache.find(g => g.id === server1).channels.cache.find(c => c.id === channel1);
chatGlobal.send(`**${emoji} ${message.member.user.username}:**\n> ${msj.replace("\n","\n> ")}`);
}
} catch(error){
console.log(error)
}})
The error is coming because you missed a parentheses when you are calling client.on('ready'). Also, as #omar al-hamad told, you are confusing the client (with a small c) with Client which is completely different. Your fixed code might look something like this:
const { Client } = require("discord.js");
const client = new Client({ intents: ["GUILD_MESSAGES"] });
let server1 = "861068256844316683";
let server2 = "936852675352481842";
let channel1 = "861068256844316686";
let channel2 = "936852675352481845";
let emojis = [
":joy:",
":rofl:",
":heart_eyes:",
":smiling_face_with_3_hearts:",
":sunglasses:",
":nerd:",
":face_with_monocle:",
":kissing_heart:",
":pensive:",
":rage:",
":face_with_symbols_over_mouth:",
":hot_face:",
":cold_face:",
":thinking:",
":flushed:",
":scream:",
":yawning_face:",
":lying_face:",
":heart_eyes_cat:",
":joy_cat:",
":scream_cat:",
];
client.on("ready", function () {
console.log(`Logged in as ${client.user.tag}!`)
});
client.on("message", (message) => {
try {
if (message.author.bot) return
let msj = message.content;
if (msj.includes("#everyone")) return;
if (msj.includes("#here")) return;
if (msj.includes("<#&")) return;
if (msj.includes("http")) return;
if (message.channel.id === channel1) {
let emoji = emojis[Math.floor(Math.random() * 21)];
if (message.member.id === "947818875137953863") return;
let chatGlobal = Client.guilds.cache
.find((g) => g.id === server2)
.channels.cache.find((c) => c.id === channel2);
chatGlobal.send(
`**${emoji} ${message.member.user.username}:**\n> ${msj.replace(
"\n",
"\n> "
)}`
);
}
if (message.channel.id === channel2) {
let emoji = emojis[Math.floor(Math.random() * 21)];
if (message.member.id === "947818875137953863") return;
let chatGlobal = Client.guilds.cache
.find((g) => g.id === server1)
.channels.cache.find((c) => c.id === channel1);
chatGlobal.send(
`**${emoji} ${message.member.user.username}:**\n> ${msj.replace(
"\n",
"\n> "
)}`
);
}
} catch (error) {
console.log(error);
}
});
client.login("TOKEN");
(Note: I took the liberty to format the code and put some variables at the top so it can be accessed everywhere.)
you declared client with small letters like this:
const client = new Client({intents: ["GUILD_MESSAGES"]})
---------^
and then you are trying to call with with a capital C like this:
Client.on
instead do:
client.on

How do I make a kick, ban and clear command in this code?

I need to know how to I put in some moderation commands such as !ban, !kick and !clear. And i will give you the code i need instruction of where to put it at because i am new to coding and so if possible can you give me:
Instruction on how to put the code in the code I have now
Instruction on how to configure the code
Instruction on where to put it
Instruction on what command should I use to test the command
FYI: I am coding a discord bot with node.js. I have been doing research on the for a week please help me!!!
require('dotenv').config();
const Discord = require('discord.js');
const bot = new Discord.Client();
const TOKEN = process.env.TOKEN;
bot.login('TOKEN');
bot.on('ready', () => {
console.info(`Logged in as ${bot.user.tag}!`);
});
bot.on('message', message => {
if (message.content === '!ping') {
message.channel.send('Pong.');
}
});
bot.on('message', message => {
if (message.content === '!getpizza') {
message.channel.send('Welcome to Lynks Pizza!!! Heres your pizza and have a nice day!!!');
}
});
bot.on('message', message => {
if (message.content === '!shutup') {
message.channel.send('Okay, I am sorry.');
}
});
bot.on('message', message => {
if (message.content === '!playdead') {
message.channel.send(':dizzy_face:');
}
});
bot.on('message', message => {
if (message.content === '!user-info') {
message.channel.send(`Your username: ${message.author.username}\nYour ID: ${message.author.id}`);
}
});
bot.on('message', message => {
if (message.content === '!server-info') {
message.channel.send(`Server name: ${message.guild.name}\nTotal members: ${message.guild.memberCount}`);
}
});
If you want to put your whole code in only one file (and not on command handler), you can do
require('dotenv').config();
const Discord = require('discord.js');
const bot = new Discord.Client();
const TOKEN = process.env.TOKEN;
bot.login('TOKEN');
bot.on('ready', () => {
console.info(`Logged in as ${bot.user.tag}!`);
});
bot.on('message', message => {
if (message.content === '!ping') {
message.channel.send('Pong.');
if (message.content === '!getpizza') {
message.channel.send('Welcome to Lynks Pizza!!! Heres your pizza and have a nice day!!!');
}
if (message.content === '!shutup') {
message.channel.send('Okay, I am sorry.');
}
if (message.content === '!playdead') {
message.channel.send(':dizzy_face:');
}
if (message.content === '!user-info') {
message.channel.send(`Your username: ${message.author.username}\nYour ID: ${message.author.id}`);
}
if (message.content === '!server-info') {
message.channel.send(`Server name: ${message.guild.name}\nTotal members: ${message.guild.memberCount}`);
}
});
The kick and the ban can looks like
if (message.content.startsWith("!kick ")) {
if (message.mentions.members.first()) {
message.mentions.members.first.kick().then((member) => {
message.channel.send( member.displayName + " has been successfully kicked.");
}).catch(() => {
message.channel.send("I do not have permissions to do this");
});
}
if (message.content.startsWith("!ban ")) {
if (!message.member.hasPermission("MANAGE_MESSAGES")) return;
if (message.mentions.members.first()) {
message.mentions.members.first.ban().then((member) => {
message.channel.send(member.displayName + " has been successfully banned.");
}).catch(() => {
message.channel.send("I do not have permissions to do this");
});
}
}
So you can add this to your code:
bot.on('message', message => {
if (message.content.startsWith("!kick ")) {
if (message.mentions.members.first()) {
message.mentions.members.first.kick().then((member) => {
message.channel.send( member.displayName + " has been successfully kicked.");
}).catch(() => {
message.channel.send("I do not have permissions to do this");
});
}
});
bot.on('message', message => {
if (message.content.startsWith("!ban ")) {
if (!message.member.hasPermission("MANAGE_MESSAGES")) return;
if (message.mentions.members.first()) {
message.mentions.members.first.ban().then((member) => {
message.channel.send(member.displayName + " has been successfully banned.");
}).catch(() => {
message.channel.send("I do not have permissions to do this");
});
}
}
});

How do i take a user's message as a variable?

I am trying to make a discord bot where I made prefix as "!e" and after that I wanted it to be a variable of anything...
How do I do that?
client.on("message", (message) => {
var botmessage = usermessage
if (message.content === `${prefix} ${usermessage}`) {
var usermessage = botmessage
message.channel.send(`:${botmessage}:`)
}
})
The solution is substr logic
client.on("message", (message) => {
if (message.content.startsWith('!e ')) {
var usermessage = message.content.substr(3)
var botmessage="whatever"
message.channel.send(`:${botmessage}:`)
}
})

TypeError: message.guild.channels.forEach is not a functionl

i would like to make a Discord bot but i got stucked in here. Heres my code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
console.log('Ready!');
});
client.login('xxx');
client.on('message', message =>{
if(message.author.id == "xxx") {
if(message.content === "!bye") {
message.guild.channels.forEach(channel => channel.delete())
}
}
})
It says:
message.guild.channels.forEach is not a function
I would like to know why is it say to me. (Sorry for my bad english)
In discord 12, you need use new class channelManager
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
console.log('Ready!');
});
client.login('xxx');
client.on('message', message =>{
if(message.author.id == "xxx") {
if(message.content === "!bye") {
message.guild.channels.cache.forEach(channel => channel.delete())
}
}
})

Resources