Search code examples
javascriptdiscorddiscord.jsbots

How can i get a user id from a .addUserOption?


Hey i made a xp system with discord-xp i wanted to make a slashcommand that can give a user xp. But everytime i give this user xp in the database there is a @ behind the user id and i want it without because otherwise the bot will not work.

Here is my Code:

const { SlashCommandBuilder } = require("@discordjs/builders");
const Levels = require('discord-xp');
const { MessageEmbed } = require('discord.js');
const client = require("../index")




module.exports = {
    data: new SlashCommandBuilder()
        .setName("addxp")
        .setDescription("add xp")
        .addUserOption((option) => option.setName('user').setDescription('add user xp').setRequired(true))
        .addNumberOption(option => option.setName('num').setDescription('Enter a number').setRequired(true)),
    async execute(client, interaction) {
        const user = interaction.options.user.id('target')
        const number = interaction.options.getNumber('num');
        var userID = interaction.user.id;
        const levels = await Levels.fetch(userID, interaction.guildId);



        Levels.appendXp(user, interaction.guild.id, number);



        interaction.reply(`**${user.tag}** got added ${number} XP.`);


    }
}

Solution

  • You can use this:

    const user = interaction.options.getUser('user')
    const id = user?.id