Whenever someone joins, the error Cannot read properties of undefined (reading 'send')
appears in the console. I have also correctly selected all the intents that you need for a welcome system or for a welcome message
If I do welcome Channel.send
at the end, send
is also displayed there as a possible suggestion!
Here is the code can someone help me?
const client = require("../index");
const Discord = require("discord.js")
const welcomeSchema = require("../models/welcomschema")
const Canvas = require("canvas");
var welcomeCanvas ={};
welcomeCanvas.create = Canvas.createCanvas(1024, 500)
welcomeCanvas.context = welcomeCanvas.create.getContext('2d')
welcomeCanvas.context.font = '72px Arial';
welcomeCanvas.context.fillStyle = '#ffffff';
Canvas.loadImage('https://media.discordapp.net/attachments/964174410510655538/964582440993706044/schwarz.jpeg').then(async (img) => {
welcomeCanvas.context.drawImage(img, 0, 0, 1024, 500)
welcomeCanvas.context.fillText("welcome", 360, 360);
welcomeCanvas.context.beginPath();
welcomeCanvas.context.arc(512, 166, 128, 0, Math.PI * 2, true);
welcomeCanvas.context.stroke();
welcomeCanvas.context.fill()
})
client.on('guildMemberAdd', async member => {
const guildData = welcomeSchema.findOne({ guildid : member.guild.id })
if(guildData) {
const welcomeid = guildData.channel
let welcomeChannel = client.channels.cache.get(welcomeid);
const welcomeText = guildData.welcometext
let canvas = welcomeCanvas;
canvas.context.font = '42px sans-serif',
canvas.context.textAlign = 'center';
canvas.context.fillText(member.user.tag, 512, 410)
canvas.context.beginPath()
canvas.context.arc(512, 166, 119, 0, Math.PI * 2, true)
canvas.context.closePath()
canvas.context.clip()
await Canvas.loadImage(member.user.displayAvatarURL({format: 'png', size: 1024}))
.then(img => {
canvas.context.drawImage(img, 393, 47, 238 ,238);
})
let atta = new Discord.MessageAttachment(canvas.create.toBuffer(), `welcome-${member.user.tag}.png`)
try {
welcomeChannel.send({
content: `<@${member.id}>, ${welcomeText}`, atta
})
} catch (error) {
console.log(error)
}
}
})
Here is the welcome Schema:
const mongoose = require("mongoose")
let Schema = new mongoose.Schema({
guildid : String,
channel : String,
welcometext : String
})
module.exports = mongoose.model("welcome", Schema)
CodePudding user response:
- Change the following line:
const guildData = welcomeSchema.findOne({ guildid : member.guild.id })
- To
const guildData = await welcomeSchema.findOne({ guildid : member.guild.id })
Awaiting the <Schema>.findOne
will not return an undefined
value