【问题标题】:How to grab an attachment url from a specific channel?如何从特定频道获取附件 url?
【发布时间】:2020-06-01 15:26:25
【问题描述】:
我有以下代码,现在它正在从任何频道抓取所有附件并将其作为嵌入发送到频道“luv”中。我希望它只从“luv”频道抓取附件并将嵌入内容也发布到“luv”中。有什么帮助吗?
client.on("message", async (message) => {
const channel = message.guild.channels.find(channel => channel.name === "luv");
if (!channel) return;
message.attachments.forEach(attachment => { // do something with the attachment // changes all attachements into embed #luv
if (message.deletable) {
message.delete(1000)
const url = attachment.url;
const aesEmbed = new RichEmbed()
.setImage(attachment.url)
client.channels.get("678987398838747166").send(aesEmbed);
console.log(attachment.url);
}
})
});
【问题讨论】:
标签:
javascript
discord
discord.js
attachment
channel
【解决方案1】:
您可以检查消息通道 ID 或名称。
client.on("message", async (message) => {
if (message.channel.id !== 'PLACE CHANNEL ID HERE') return //Stop function if channel ID !== 'luv' channeld.id
const channel = message.guild.channels.find(channel => channel.name === "luv");
if (!channel) return;
message.attachments.forEach(attachment => { // do something with the attachment // changes all attachements into embed #luv
if (message.deletable) {
message.delete(1000)
const url = attachment.url;
const aesEmbed = new RichEmbed()
.setImage(attachment.url)
client.channels.get("678987398838747166")
.send(aesEmbed);
console.log(attachment.url);
}
})
});
而且最好不要使用find by name 方法来获取频道。
替换
const channel = message.guild.channels.find(channel => channel.name === "luv");
到
const channel = message.guild.channels.get('CHANNEL ID')