NodeJS 如何获取选项中提到的用户的用户?discord.js v13

iih3973s  于 2023-02-08  发布在  Node.js
关注(0)|答案(1)|浏览(117)

我有一个机器人,它试图踢斜线命令中提到的用户,但当我在“user”选项中提到一个用户时,它说User not found,并给出错误x is not a valid function(x是我正在尝试的)。我试图找到一个有效的函数来实际定义提到的用户,但似乎找不到。感谢任何帮助,因为我是javascript和discord.js的新手

const { SlashCommandBuilder, OptionType } = require('discord.js');

module.exports = {
  data: new SlashCommandBuilder()
    .setName('tkick')
    .setDescription('Kicks the specified user by tag (Moderation)')
    .addMentionableOption((option) =>
      option
        .setName('user')
        .setDescription('The user to be kicked')
        .setRequired(true),
    ),

  async execute(interaction, client) {
    if (!interaction.member.permissions.has(['KICK_MEMBERS']))
      return interaction.reply(
        'You do not have permission to use this command.',
      );

    // Get the user from the interaction options
    const user = interaction.user;
    if (!user) return interaction.reply('User not found.');

    // Kick the user
    await interaction.option.('user').kick(); // This is where I am having the issue

    // Reply with the text
    await interaction.reply({
      content: 'User has been kicked.',
    });
  },
};

我试着查看我查找的不同问题,但它要么是在旧版本中不存在,要么被删除。Discord.js v13于2022年12月发布,但我只能找到之前的帖子。

s5a0g9ez

s5a0g9ez1#

interaction.option.('user').kick()不是有效语法,并且interaction.user不是GuildMember,而是User对象。要能够踢某人,您需要一个GuildMember对象,如果您使用addMentionableOption,则可以使用interaction.options.getMentionable方法获得该对象。
但是,它接受角色和用户。如果你不想让你的机器人接受角色的提及,这会让你的生活有点困难,最好使用addUserOption而不是addMentionableOption。对于addUserOption,你可以通过使用interaction.options.getMember方法来获得GuildMember对象:

module.exports = {
  data: new SlashCommandBuilder()
    .setName('tkick')
    .setDescription('Kicks the specified user by tag (Moderation)')
    . addUserOption((option) =>
      option
        .setName('user')
        .setDescription('The user to be kicked')
        .setRequired(true),
    ),

  async execute(interaction) {
    if (!interaction.member.permissions.has(['KICK_MEMBERS']))
      return interaction.reply(
        'You do not have permission to use this command.',
      );

    try {
      await interaction.options.getMember('user').kick();
      interaction.reply('User has been kicked.');
    } catch (err) {
      console.error(error);
      interaction.reply('⚠️ There was an error kicking the member');
    }
  },
};

相关问题