问题描述
大家好:)我想标题有点奇怪,但我对此并不陌生,我想用自己的话弄清楚。
基本上,我要做的是一个“ embeds-orders.js”文件,该文件将重组一些嵌入,以便我可以在main.js文件中称这些嵌入而不占用空间。
我的问题:我想在嵌入说明中写出触发它的用户名。我会使用“ user.username”之类的东西,但是由于未定义用户,所以出现了ReferenceError。 所以我想我需要将诸如类之类的东西导入到我的文件中吗?我准备向你们学习:D
const { MessageEmbed } = require("discord.js")
const orderStarter = new MessageEmbed()
.setAuthor("STARTER RECOVERY ?","https://i.imgur.com/YOZP0xO.png")
.setDescription(
"Hello " + user.username + ",welcome."
) /* ^^^^^^^^^^^^^ */
.setColor("3232FF")
.setThumbnail(
"https://media.discordapp.net/attachments/224553787824668673/716765453548126228/icon.png.fe88fd1ca437c86cde7cd99961673ef8.png"
)
.addFields(
{
name: "__Wanna Cancel ?__ ?",value: "Type `!cancel`",inline: true,}
)
module.exports = {
orderStarter
}
解决方法
如果您需要动态指定触发嵌入的人员,则应使其成为函数。
module.exports = (username) => {
const { MessageEmbed } = require('discord.js');
return new MessageEmbed()
.setAuthor('STARTER RECOVERY ?','https://i.imgur.com/YOZP0xO.png')
.setDescription(`Hello ${username},welcome.')
.setColor('3232FF')
.setThumbnail('https://media.discordapp.net/attachments/224553787824668673/716765453548126228/icon.png.fe88fd1ca437c86cde7cd99961673ef8.png')
.addField('__Wanna Cancel ?__ ?','Type `!cancel`',true);
};
然后,当您需要发送嵌入内容时,只需将用户名作为参数传递即可。例如,如果这是在message
事件中发生的:
const embed = require('./embeds-orders.js');
client.on('message',(message) => {
if (message.content === 'I want to order the embed')
message.channel.send(embed(message.author.username));
});
编辑:在messageReactionAdd
事件中:
const embed = require('./embeds-orders.js');
client.on('messageReactionAdd',(reaction,user) => {
// bla bla bla
message.channel.send(embed(user.username));
编辑#2:您只需编辑函数即可添加更多参数。
module.exports = (author,description,color) => {
const { MessageEmbed } = require('discord.js');
return new MessageEmbed()
.setAuthor(...author) // author can be an array with the text and the url as elements
.setDescription(description)
.setColor(color)
.setThumbnail(
'https://media.discordapp.net/attachments/224553787824668673/716765453548126228/icon.png.fe88fd1ca437c86cde7cd99961673ef8.png'
)
.addField('__Wanna Cancel ?__ ?',true);
};
const embed = require('./embeds-orders.js');
client.on('messageReactionAdd',user) => {
// bla bla bla
reaction.emoji.name === "Emoji Name"
? message.channel.send(embed(["author text","url"],"description","color"))
: message.channel.send(
embed(
["different author text","different description","different color"
)
);