discord.js 获取 Canvas 附件的 URL
discord.js Getting the URL Of Canvas Attachment
我正在尝试获取附件的 URL 并将其发送到频道,我尝试使用 attachment.url
但我收到 undefined
这是我的代码:
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}\``)
}})
我很确定您无法在发送之前获取它的 url,您需要先发送附件,然后再尝试获取它的 url。
您确实只能在 发送附件后 获得附件的 Discord URL。获取它的一种简单方法是使用 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;
}
});
我正在尝试获取附件的 URL 并将其发送到频道,我尝试使用 attachment.url
但我收到 undefined
这是我的代码:
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}\``)
}})
我很确定您无法在发送之前获取它的 url,您需要先发送附件,然后再尝试获取它的 url。
您确实只能在 发送附件后 获得附件的 Discord URL。获取它的一种简单方法是使用 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;
}
});