javascript - 将用户图像作为参数,然后使用 Discord.JS 发送具有相同图像的消息
问题描述
让我们开始吧。
我一直在寻找谷歌,试图找到一个关于如何将图像作为参数的指南,然后将相同的图像与用户提供的消息一起发送。
我正在发布公告命令。
现在,我的命令只接受文本作为输入,而不是文件/图像。
这是我的宣布命令:
module.exports = {
name: "afv!announce",
description: "announce something",
execute(msg, args, bot) {
if (msg.member.roles.cache.find((r) => r.name === "Bot Perms")) {
const prevmsg = msg;
const text = args.join().replace(/,/g, " ");
msg
.reply(
"Would you like to do `@here` :shushing_face: or `@everyone` :loudspeaker:?\nIf you would like to ping something else, react with :person_shrugging:. (you will have to ping it yourself, sorry)\n*react with :x: to cancel*"
)
.then((msg) => {
const areusure = msg;
msg
.react("")
.then(() => msg.react(""))
.then(() => msg.react(""))
.then(() => msg.react("❌"));
const filter = (reaction, user) => {
return (
["", "", "", "❌"].includes(reaction.emoji.name) &&
user.id === prevmsg.author.id
);
};
msg
.awaitReactions(filter, { max: 1, time: 60000, errors: ["time"] })
.then((collected) => {
const reaction = collected.first();
if (reaction.emoji.name === "") {
areusure.delete();
prevmsg
.reply("<a:AFVloading:748218375909539923> Give me a sec...")
.then((msg) => {
bot.channels.cache
.get("696135322240548874")
.send("@here\n\n" + text);
msg.edit("<a:AFVdone:748218438551601233> Done!");
});
} else if (reaction.emoji.name === "") {
areusure.delete();
prevmsg
.reply("<a:AFVloading:748218375909539923> Give me a sec...")
.then((msg) => {
bot.channels.cache
.get("696135322240548874")
.send("@everyone\n\n" + text);
msg.edit("<a:AFVdone:748218438551601233> Done!");
});
} else if (reaction.emoji.name === "") {
areusure.delete();
prevmsg
.reply("<a:AFVloading:748218375909539923> Give me a sec...")
.then((msg) => {
bot.channels.cache
.get("696135322240548874")
.send(
"Important: https://afv.page.link/announcement\n\n" +
text
);
msg.edit("<a:AFVdone:748218438551601233> Done!");
});
} else if (reaction.emoji.name === "❌") {
areusure.delete();
prevmsg.reply("Cancelled.");
}
})
.catch((collected) => {
msg.delete();
prevmsg.reply("you didn't react with any of the emojis above.");
});
});
}
},
};
解决方案
阿凡达
要使用图像,您可以使用此功能:
message.author.displayAvatarURL(
{ dynamic: true } /* In case the user avatar is animated we make it dynamic*/
);
然后它将返回一个可用于嵌入缩略图或图像的链接。如果你想在嵌入中使用它
let link = message.author.displayAvatarURL({ dynamic: true });
const embed = new Discord.MessageEmbed().setThumbnail(link);
使用图片链接
如果要使用图像链接,则必须将其转换为不和谐附件。
const args = message.content.split(' ').slice(1);
const attach = new Discord.Attachement(args.join(' '), 'image_name.png');
message.channel.send(attach);
希望我对你有所帮助。如果没有,您仍然可以在discord.js 指南中搜索^^
不知道图片链接在哪里
如果您真的不知道图像链接在消息内容中的位置,您可以将其分开(您已经使用参数)并使用 forEach 函数:
const args = message.content.split(' ').slice(1);
// a function to see if it's an url
function isvalidurl(string) {
try {
const a = new URL(string);
} catch (err) {
return false;
}
return true;
}
// check the function for each argument
args.forEach((a) => {
if (isvalidurl(a)) link = a;
});
if (link) {
// code
} else {
// code
}
推荐阅读
- scrapy - 如何使用 AsyncioSelectorReactor 从扭曲的延迟回调中访问返回值?
- javascript - 使用两个不同的分隔符连接数组中的项目
- youtube - 如何使用 youtube API 获取频道信息
- post - 下载的Angular 9文件损坏
- tfs - 如何在 TFS 2017 中从多个来源克隆代码?
- c - 用 C 中的指针转置方阵。错误的输出
- php - 合并一个具有相同键的数组(对象列表)
- vuejs2 - 如何将条件样式应用于未选中的复选框
- javascript - JavaScript 如何计算这个数学表达式
- python - Python - 将值发送到页面并抓取输出