ReferenceError: message is not defined - Welcome Message - embed - javascript

So I was playing with my welcome message and wanted to make it an embed, I ended up re-writing it all to work with the embed, however after I finished, I got the error message is not defined.
var welcomePath = './Storage/welcome.json';
var welcomeRead = fs.readFileSync(welcomePath);
var welcomeFile = JSON.parse(welcomeRead);
client.on('guildMemberAdd', (member) => {
var serverId = member.guild.id;
if (!welcomeFile[serverId]) {
console.log('Welcome is disabled!');
} else {
let welcomeChannel = welcomeFile[serverId].channel,
let setChannel = message.guild.channels.find(channel => channel.name === welcomeChannel);
const embed = new Discord.RichEmbed()
.setTitle("Test")
.setAuthor("Test")
.setColor(3447003)
.setDescription("Test")
.setThumbnail(message.author.avatarURL);
member.guild.channels.get(setChannel).send({
embed
});
}
});
The error pertains to this line
let setChannel = message.guild.channels.find(channel => channel.name === welcomeChannel);
I do really want to learn JS, and keep finding myself hitting this brick walls where I need to simply ask for help. I am also unsure if you fix my message is not defined that my code will actually do anything.

message is not defined, you should look for member.
let setChannel = member.guild.channels.find(channel => channel.name === welcomeChannel);
client.on('guildMemberAdd', (member) => {
var serverId = member.guild.id;
if (!welcomeFile[serverId]) {
console.log('Welcome is disabled!')
} else {
let welcomeChannel = welcomeFile[serverId].channel
let setChannel = member.guild.channels.find(channel => channel.name === welcomeChannel);
const embed = new Discord.RichEmbed()
.setTitle("Test")
.setAuthor("Test")
.setColor(3447003)
.setDescription("Test")
.setThumbnail(message.author.avatarURL)
member.guild.channels.get(setChannel).send({embed});
}
})

Related

Invites command bot reply sending message multiple times

I am using the invite tracker from the WOK tutorials, and even though the code works perfectly fine, and im using a command handler that has 0 issues, for some reason, the bot replies with multiple messages instead of 1 as seen in this image:
Here is the code that I used for the invites command:
const Discord = require("discord.js")
module.exports = {
commands: 'invites',
callback: (message) => {
const { guild } = message
guild.fetchInvites().then((invites) => {
const inviteCounter = {}
invites.forEach((invite) => {
const { uses, inviter } = invite
const { username, discriminator } = inviter
const name = `${username}#${discriminator}`
inviteCounter[name] = (inviteCounter[name] || 0) + uses
})
let replyText = ''
const sortedInvites = Object.keys(inviteCounter).sort((a, b) => inviteCounter[b] - inviteCounter[a])
for (const invite of sortedInvites) {
const count = inviteCounter[invite]
replyText += `\n${invite} has invited ${count} member(s)!`
const embed = new Discord.MessageEmbed()
embed.setTitle('Invites: ')
embed.setDescription(replyText)
message.reply(embed)
}
})
}
}
Fyi last time i posted this i forgot to post the code so yeah that was dumb my bad.
This is because you have message.reply in a for...of loop.
for (const invite of sortedInvites) {
const count = inviteCounter[invite]
replyText += `\n${invite} has invited ${count} member(s)!`
const embed = new Discord.MessageEmbed()
embed.setTitle('Invites: ')
embed.setDescription(replyText)
message.reply(embed) //here
}
You probably meant to put it outside of the loop so that it sends the resulting embed after all the iterations.

Discord.js database using repl.it

I made a database script in repl.it and it's working but I keep getting null and then users database here is my code:
client.on("message", async (message) => {
if (message.content.toLowerCase().startsWith("#مخالفات")) {
var pfpMember = message.mentions.members.first() || message.member;
const violation = await db.get(`wallet_${message.mentions.members.first()}`);
if (violation === null) violation = "العضو ليس لديه اي مخالفات";
const pembed = new Discord.MessageEmbed()
.setTitle(`شرطة لوس سانتوس`)
.setDescription(`المخالفات المرورية :` + violation)
.setColor("RANDOM")
.setFooter("شرطة لوس سانتوس")
.setThumbnail(pfpMember.user.displayAvatarURL());
message.channel.send(pembed);
}
});
And here is a screenshot:
The issue is that you cannot reassign violation as it is a constant, not a variable.
Using the following should solve the issue:
client.on("message", async (message) => {
if (message.content.toLowerCase().startsWith("#مخالفات")) {
var pfpMember = message.mentions.members.first() || message.member;
let violation = await db.get(`wallet_${message.mentions.members.first()}`);
if (violation === null) violation = "العضو ليس لديه اي مخالفات";
const pembed = new Discord.MessageEmbed()
.setTitle(`شرطة لوس سانتوس`)
.setDescription(`المخالفات المرورية :` + violation)
.setColor("RANDOM")
.setFooter("شرطة لوس سانتوس")
.setThumbnail(pfpMember.user.displayAvatarURL());
message.channel.send(pembed);
}
});

The Discord.js command I wrote does not working

I have an error in these codes.
client.on("message", async message => {
if(message.author.bot || message.channel.type === 'dm') return;
let prefix = botsettings.prefix;
let messageArray = message.content.split(" ");
let cmd = messageArray[0];
let args = message.content.substring(message.content.indexOf(' ')+1);
if(!message.content.startsWith(prefix)) return;
let commandfile = bot.commands.get(cmd.slice(prefix.lenght)) || bot.commands.get(bot.aliases.get(cmd.slice(prefix.lenght)))
if(commandfile) commandfile.run(bot,message,args)
if(cmd === `${prefix} reactions`){
let embed = new Discord.MessageEmbed()
.setTitle('Reaction Roles')
.setDescription('Kişiliğinize göre bir rol seçiniz.')
.setColor('GREEN')
let msgEmbed = await message.channel.send(embed).
msgEmbed.react(':closed_book:')
}
})
I am encountering this error. I couldn't find the botsettings part.
UnhandledPromiseRejectionWarning: ReferenceError: botsettings is not defined
This means that you didn't define the "botsettings" file anywhere.
Try adding
const botsettings = require ("<path to the file>")
to the top of your code.
If you're trying to make commands, try using this. I use it a lot in my bot (currently v11, too much of a hassle to update to v12. But the same code should work anyway.):
client.on("message", async (message) => {
if(message.author.bot) return;
const args = message.content.slice(botsettings.prefix.length).trim().split(' ');
const command = args.shift().toLowerCase();
if (message.content.indexOf(botsettings.prefix) !== 0) return;
if(command === "reactions"){
let embed = new Discord.MessageEmbed()
.setTitle('Reaction Roles')
.setDescription('Kişiliğinize göre bir rol seçiniz.')
.setColor('GREEN')
let msgEmbed = await message.channel.send(embed).
msgEmbed.react(':closed_book:')
}
});
Before doing this, make sure you have r const botsettings = ("./path/to/your/botsettings.json"); at the top of your entire code.

My discord bot loops a lot of messages (discord.js)

I try to create a bot who replies the picture. Its working, but some second later the bots send the embed again without a picture and loop this (Check picture below)
client.on('message' , (message) => {
var content = message.content.split(" ");
const channel = client.channels.cache.find(channel => channel.name === "spam")
const messageinput = `${message.content}`
let messageAttachment = message.attachments.size > 0 ? message.attachments.array()[0].url : null
let embed = new Discord.MessageEmbed();
embed.setAuthor("Test")
if (messageAttachment) embed.setImage(messageAttachment)
embed.setColor(16689911);
message.channel.send(embed)
})
This is the output:
The bot is looping over and over again because it's in reaction to it's own message that it send. You can prevent this by making sure that the message author wasn't a bot.
client.on('message' , (message) => {
if (message.author.bot) return
var content = message.content.split(" ");
const channel = client.channels.cache.find(channel => channel.name === "spam")
const messageinput = `${message.content}`
let messageAttachment = message.attachments.size > 0 ? message.attachments.array()[0].url : null
let embed = new Discord.MessageEmbed();
embed.setAuthor("Test")
if (messageAttachment) embed.setImage(messageAttachment)
embed.setColor(16689911);
message.channel.send(embed)
})

Issue with Java Discord.js Modmail System

I have been trying to make a discord ModMail system on my bot and there is an error that I can't understand my code is below:
client.on('message', message => {
if (message.author.bot || !message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).split(/+ /)
if (!message.channel.name.includes('modmail')) {
return;
} else {
if (isNaN(args[0])) {
return message.channel.send('Sorry but that is not a valid user')
}
let storage = message.guild.members.cache;
let memberId = storage.find(member => member.id.toLowerCase() === args[0]);
if (memberId) {
const msg = message.content.slice(args[0].length + prefix.length).split(" ").join(" ");
let embed = new Discord.MessageEmbed()
.setThumnail(message.author.displayAvatarURL())
.setDescription(`<#${message.author.id}>\n------------\n${msg}`)
.setColor('#599cff')
.setFooter('ModMail System')
.setTimestamp();
client.users.fetch(args[0]).then(user => user.send(embed).catch(err => console.log(err)));
message.channel.send('Your message was sent to the staff team! Please be patient for your reply.')
} else {
return message.channel.send('Could not find the user sorry.');
}
}
});
client.on('message', message => {
var msg = message.content;
var n = message.author.username;
if (message.channel.type === 'dm') {
if (message.author.bot) return;
let blacklisted = ['word1', 'word2']
let foundInText = false;
for (var i in blacklisted) {
if (message.content.toLowerCase().includes(blacklisted[i].toLowerCase())) foundInText = true;
}
if (foundInText) {
return message.channel.send('Please do not use foul language in the message. (if you must please cover it with symbols *not letters*)');
}
message.channel.send('Your message was sent to the staff team! Please be patient for your reply.');
const mailMessage = `${message.author.id} ${msg}`
client.channels.fetch('795031406442315816').then(user => user.send(mailMessage).catch(err => console.log(err)));
}
});
The error that is shown is:
SyntaxError: Invalid regular expression: /+ /: Nothing to repeat
This has been confusing me for a while (I am new to code) please if you know how to fix this it would help a lot thank you.
I think the error is because there is a problem here:
const args = message.content.slice(prefix.length).split(/+ /)
If you change .split(/+ /) to .split(' ') or .split(/ /) it would work.

Categories

Resources