【发布时间】:2021-10-30 07:38:24
【问题描述】:
我正在使用 discord.js 版本 13.1.0 和 node.js 版本 16.7.0,我编写了这段代码以在嵌入中显示用户信息,但问题是它只显示我的信息。
我也试过这个guildMember.user 而不是user,但它无法执行它在控制台TypeError: Cannot read property 'user' of undefined 中显示的命令。
这是命令的代码:
module.exports = {
data: new SlashCommandBuilder()
.setName('info')
.setDescription('Get info about a user or a server!')
.addSubcommand(subcommand =>
subcommand
.setName('user')
.setDescription('Info about a user')
.addUserOption(option => option.setName('target').setDescription('The user'))),
async userInfo = new MessageEmbed()
.setColor('#0099ff')
.setTitle('Some title')
.setAuthor('Some name', interaction.user.displayAvatarURL({ dynamic: true }))
.setDescription('Some description here')
.setThumbnail(interaction.user.displayAvatarURL({ dynamic: true }))
.addFields(
{ name: 'Hello', value: 'Hello' },
{ name: '\u200B', value: '\u200B' },
{ name: 'ID', value: interaction.user.id, inline: true },
{ name: 'Username', value: interaction.user.tag, inline: true },
)
.setTimestamp()
.setFooter('Some footer text here', 'https://i.imgur.com/AfFp7pu.png');
if (interaction.options.getSubcommand() === 'user') {
const user = interaction.options.getUser('target');
if (user) {
await interaction.reply({ embeds: [userInfo] });
};
};
【问题讨论】:
-
您的机器人中是否启用了特权意图?
-
interaction定义在哪里? -
这段代码甚至不是有效的 javascript...
SlashCommandBuilder为您的命令构建元数据,您应该在函数或interactionCreate侦听器中处理交互。 -
@G-Force 是的,我在我的机器人 (index.js) 的主文件中写了
const client = new Client({ intents:[Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });。 -
@MrMythical 在 index.js 文件中我还没有创建事件处理程序,看!代码正在运行我说问题是机器人只返回我的信息,即使我针对某人获取他的信息,希望你能在下面看到我的答案
标签: javascript node.js discord.js bots