大家好:)我想这个标题有点奇怪,但我对此很陌生,我正在尝试用自己的话来弄清楚。
基本上我所做的是一个“embeds-orders.js”文件,它将重新组合一些嵌入,这样我就可以在我的 main.js 文件中调用这些嵌入,而不占用其中的空间。
我的问题:我想在嵌入的描述中写入触发它的用户名。我会使用“user.username”或其他东西,但由于我没有定义用户,所以我得到了一个ReferenceError。 所以我想我需要将诸如类之类的东西导入到我的文件中才能做到这一点?我已经准备好向你们学习了:D
↓ 这是我的“embeds-orders.js” 文件 ↓
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 ?__ 🠗', 'Type `!cancel`', true);
};
const embed = require('./embeds-orders.js');
client.on('messageReactionAdd', (reaction, 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", "url"],
"different description",
"different color"
)
);
关于javascript - 如何在嵌入中调用 .username 属性? (不和谐.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64343381/