Я изучал основы создания бота Discord и пытался освоить использование Canvas с Discord JS.
Я пытался следовать этому простой урок но я просто не могу понять, что может быть не так.
Для справки, вот точка в уроке, к которой я пришел.
client.on('guildMemberAdd', async member => {
const channel = member.guild.channels.cache.find(ch => ch.name === 'member-log');
if (!channel) return;
const canvas = Canvas.createCanvas(700, 250);
const ctx = canvas.getContext('2d');
// Since the image takes time to load, you should await it
const background = await Canvas.loadImage('./wallpaper.jpg');
// This uses the canvas dimensions to stretch the image onto the entire canvas
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
// Use helpful Attachment class structure to process the file for you
const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'welcome-image.png');
channel.send(`Welcome to the server, ${member}!`, attachment);
});
Когда эта функция срабатывает, она должна заставить бота отправлять на сервер сообщение «Добро пожаловать на сервер, [имя члена здесь]» плюс указанное вложение.
Однако вместо этого , он просто отправляет сообщение на сервер без каких-либо вложений.
При тестировании я тоже не получаю никаких ошибок.
Может кто-нибудь дать мне несколько советов о том, что здесь может пойти не так?
РЕДАКТИРОВАТЬ: После дальнейших экспериментов, я нашел решение.
client.on('guildMemberAdd', async member => {
console.log("Here we go");
console.log(member.guild.name);
const channel = member.guild.channels.find(ch => ch.name === 'general');
if (!channel) return;
// Set a new canvas to the dimensions of 700x250 pixels
const canvas = Canvas.createCanvas(2097, 2097); //Works correctly
// ctx (context) will be used to modify a lot of the canvas
const ctx = canvas.getContext('2d');
// Since the image takes time to load, you should await it
const background = await Canvas.loadImage('./sadman.jpg'); //Finds it
// This uses the canvas dimensions to stretch the image onto the entire canvas
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
// Use helpful Attachment class structure to process the file for you
channel.send(`Welcome to the server, ${member}!`, { files: [{ attachment: canvas.toBuffer() }] });
});