Recently, I received quick and efficient help with welcome messages. Now I would like to make embed welcome messages but I don't know how I can upadate my code. Code that I have is from Discord.js 12 and I would like to use discord.js v13. I made a few changes to the code to make it work on discord.js 13 but still something is wrong.
Here's index.js:
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES" , "GUILD_MESSAGE_REACTIONS" , "DIRECT_MESSAGE_REACTIONS" , "GUILD_MEMBERS" ] , partials: ["MESSAGE" , "CHANNEL" , "REACTION"] });
const { token } = require('./config.json')
const welcome = require('./commands/Main-Commands/Mod/welcome');
client.once('ready', () => {
console.log('Ready.')
setInterval(() => {
const statuses = [
`Tech Tip Cyber Videos`,
`YouTube Tutorial`,
]
const status = statuses[Math.floor(Math.random() * statuses.length)]
client.user.setActivity(status, { type: "WATCHING"}) // Can Be WATCHING, STREAMING, LISTENING
}, 2000) // Second You Want to Change Status, This Cahnges Every 2 Seconds
welcome(client)
})
client.login(token)
Here's welcome.js
const { MessageEmbed, MessageAttachment } = require("discord.js")
const Canvas = require('canvas')
const { join } = require("path")
const user = require('discord.js')
module.exports = (client) => {
client.on('guildMemberAdd', async member => {
// console.log(member) // If You Want The User Info in Console Who Joined Server Then You Can Add This Line. // Optional
const welcome = '938780971568889876' // Welcome Channel
const rules = '938781016502456380' // Rules Channel
if(!member.guild) return
const canvas = Canvas.createCanvas(1770, 635) // Create Canvas Of Your Wish
const ctx = canvas.getContext('2d') // 2d Only For Now
const background = await Canvas.loadImage(join(__dirname, '../../../Images', 'welcome.png')) // BackGround Image
ctx.drawImage(background, 0 ,0, canvas.width, canvas.height) // Setting BackGround Image
ctx.strokeStyle = '#FFFFFF' // Keep Color As Nothing
ctx.strokeRect(0, 0, canvas.width, canvas.height)
var name = `${member.user.username}` // UserName Of User Who Joined
if(name.length >= 16) { // If Name Is To Long(More Then 16)
ctx.font = 'bold 100px Sans' // Defining Size, Font
ctx.fillStyle = '#0FEEF3' // Keep Color Of UserName
ctx.fillText(name, 680, canvas.height / 2 - 1) // 720 Is Width From Left To Right
} else { // If UserName Is Smaller Then 16 Then...
ctx.font = 'bold 130px Sans' // Defining Size, Font
ctx.fillStyle = '#0FEEF3' // Keep Color Of UserName
ctx.fillText(name, 680, canvas.height / 2 - 1) // 720 Is Width From Left To Right // You Can Change According To Your Will
}
var discrim = `#${member.user.discriminator}` // Discriminator Of User
ctx.font = 'bold 60px Sans' // Defining Size, Font
ctx.fillStyle = '#FA9448' // Keep Color Of Discriminator
ctx.fillText(discrim, 680, canvas.height / 2 70) // You Can Change According To Your Will
var server = `Welcome To ${member.guild.name}`
ctx.font = 'bold 80px Sans' // Defining Size, Font
ctx.fillStyle = '#21FBA1' // Keep Color Of Discriminator
ctx.fillText(server, 670, canvas.height / 2 - 150) // You Can Change According To Your Will
var count = `Member #${member.guild.memberCount}th`
ctx.font = 'bold 60px Sans' // Defining Size, Font
ctx.fillStyle = '#21FBA1' // Keep Color Of Discriminator
ctx.fillText(count, 680, canvas.height / 2 160) // You Can Change According To Your Will
ctx.beginPath()
ctx.arc(315, canvas.height / 2, 250, 0, Math.PI * 2, true) // Avatar Of User
ctx.closePath()
ctx.clip() // Make Avatar As Circle, By Default Its Square
const avatar = await Canvas.loadImage(member.user.displayAvatarURL({ format: 'png' })) // Get Users Avatar
ctx.drawImage(avatar, 65, canvas.height / 2- 250, 500 , 500) // Adjusting Avatar In Circle Of Image
const attachment = new MessageAttachment(canvas.toBuffer(), 'welcome.png') // Send As Attachment
const embed = new MessageEmbed() // Send As Embed
.setAuthor(`${user.tag}`, message.author.displayAvatarURL())
.setDescription(`
Welcome To **${member.guild.name}** <@${member.id}>
Please Check <#${rules}>
`)
.setImage('attachment://welcome.png')
.setColor('RANDOM')
.attachFiles(attachment) // Send Welcome Image
const channel = member.guild.channels.cache.get(welcome) // Get Welcome Channel
channel.send(embed) // Send Embed
client.on("message", message => {
const args = message.content.slice(prefix.length).split(/ /);
const command = args.shift().toLowerCase();
})
})
}
Here's config.json
{
"token": "Your Token",
"def_prefix": " "
}
If someone is able to help me, please explain what is wrong with my code and what should I change to make it work.
Here's the error I receive when new user join server:
.setAuthor(`${user.tag}`, message.author.displayAvatarURL())
^
ReferenceError: message is not defined
CodePudding user response:
As the error said, message is not defined in this scope. You should use member.user.displayAvatarURL().
CodePudding user response:
The error is coming because in the line where you are using setAuthor in your embed, there is nothing defined as message yet. The client.on('message') is defined only after you are using message.author.displayAvatarURL(), so it would be better to change the setAuthor() in your embed to:
const attachment = new MessageAttachment(canvas.toBuffer(), 'welcome.png') // Send As Attachment
const embed = new MessageEmbed() // Send As Embed
.setAuthor(`${user.tag}`, member.user.displayAvatarURL())
.setDescription(`
Welcome To **${member.guild.name}** <@${member.id}>
Please Check <#${rules}>
`)
.setImage('attachment://welcome.png')
.setColor('RANDOM')
channel.send({
embeds: [embed],
files: [attachment]
})
