javascriptnode.jsdiscord.js

Discord.js: My code throws "" is not a fucntion error


I am writing a private bot. When I wanted to make a command with option to ping another user. However, when I try to run it i am getting an error: TypeError: " " is not a function This is my code:

const { Client, Events, GatewayIntentBits, SlashCommandBuilder, REST, Routes } = require('discord.js');
const {token} = require('./config.json');

const client = new Client({intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers]}); 

client.once(Events.ClientReady, c => {
    console.log(`Logged in as ${c.user.tag}`);

    const ping = new SlashCommandBuilder()
        .setName('ping')
        .setDescription('Pings another user')
        .addUserOption(option =>
            option
                .setName('pingd')
                .setDescription('Who to ping?')
                .setRequired(false)
        )
        .setIntegrationTypes(0, 1)
        .setContexts(0, 1, 2);
        

    const noni = new SlashCommandBuilder()
        .setName('noni')
        .setDescription('Test command')
        .setIntegrationTypes(0, 1)
        .setContexts(0, 1, 2);

    client.application.commands.create(ping);
    client.application.commands.create(noni);
});


client.on(Events.InteractionCreate, interaction => {
    if(!interaction.isChatInputCommand()) return;
    if(interaction.commandName === "ping"){
        let user = interaction.options.getUser('pingd'); 
        if (!user) user = interaction.user;
        console.log(user)
        interaction.reply(` ``test``\n @${user.username}`);
    }
    if(interaction.commandName === "noni"){
        interaction.reply(` ``another test!`` `);
    }
    console.log(interaction);
});

client.login(token);

How can i fix it? Thanks

I have tried to remove @${user.username} section, but it didn't work.


Solution

  • When you want to use a ` inside a template literal, you need to escape it with \, otherwise it ends the literal. You have several places in your code where you've written `` inside a template literal without escaping it; this ends the literal and then tries to use it as the tag for a new one, thus the error that a string is not a function.