Reputation: 41
I am trying to get the URL of the attachment and send it to a channel , I tried to use attachment.url
but but I'm getting undefined
here is my code:
client.on('message', async message => {
if (message.author.bot || message.channel.type === 'dm') return;
if (message.content.toLowerCase().indexOf(prefix.toLowerCase()) !== 0) return;
var args = message.content.slice(prefix.length).trim().split(/ +/g);
var command = args.shift().toLowerCase();
if(command =='image'){
let args = message.content.split(" ").slice(1).join(' ');
const canvas = Canvas.createCanvas(400, 140);
const ctx = canvas.getContext('2d')
ctx.fillStyle = '#ffffff';
ctx.font = '150px serif';
ctx.shadowColor = 'black';
ctx.shadowBlur = 5;
ctx.fillText(`${args}`, 0, 118);
ctx.strokeStyle = '#000000';
ctx.strokeText(`${args}`, -1, 118);
ctx.textAlign = "center";
const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'image.png')
message.channel.send(`\`${attachment.url}\``)
}})
Upvotes: 0
Views: 381
Reputation: 43
You can indeed only get the Discord URL of an attachment after sending it. An easy way to grab it would be using async/await
:
client.on('message', async message => {
if (message.author.bot || message.channel.type === 'dm') return;
if (message.content.toLowerCase().indexOf(prefix.toLowerCase()) !== 0) return;
var args = message.content.slice(prefix.length).trim().split(/ +/g);
var command = args.shift().toLowerCase();
if (command === 'image') {
const args = message.content.split(' ').slice(1).join(' ');
const canvas = Canvas.createCanvas(400, 140);
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#ffffff';
ctx.font = '150px serif';
ctx.shadowColor = 'black';
ctx.shadowBlur = 5;
ctx.fillText(`${args}`, 0, 118);
ctx.strokeStyle = '#000000';
ctx.strokeText(`${args}`, -1, 118);
ctx.textAlign = 'center';
const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'image.png');
const reply = await message.channel.send({ files: [attachment] });
const attachmentURL = reply.attachments.first().url;
}
});
Upvotes: 1
Reputation: 45
I'm pretty sure you aren't able to get the url of it before it has been sent, you would need to send the attachment first before attempting to get its url.
Upvotes: 0