#how to set this line

142 messages · Page 1 of 1 (latest)

clever flame
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

cedar granite
#

can you show your interactionCreate event handler?

clever flame
#

Yes

#

Wait pls

#
const { Events } = require('discord.js');
const wait = require('node:timers/promises').setTimeout;

module.exports = {
    name: Events.InteractionCreate,
    async execute(interaction) {
        if (!interaction.isContextMenuCommand()) return;
    }
}
#

this is contextMenu handler

#

and

#
const { Events } = require('discord.js');
const wait = require('node:timers/promises').setTimeout;

module.exports = {
    name: Events.InteractionCreate,
    async execute(interaction) {
        if (!interaction.isChatInputCommand()) return;

        const command = interaction.client.commands.get(interaction.commandName);

        if (!command) {
            console.error(`No command matching ${interaction.commandName} was found.`);
            return;
        }

        try {
            await command.execute(interaction);
        } catch (error) {
            console.error(`Error executing ${interaction.commandName}`);
            console.error(error);
        }
    },
};
#

this is interaction handler

thick lynx
#

you dont call your execute function when listening to context menu interactions

#

you also dont need 2 event listeners. interaction.isCommand() listens for both slash and context menus

thick lynx
#

you should call it there

or replace isChatInputCommand() to isCommand(), which should also work

clever flame
thick lynx
#

show you what

#

simply call your excecute method

clever flame
#

just execute()?

thick lynx
#

it takes an interaction param

#

as shown in your code when listening for chat input commands above

clever flame
#

i get this error

#
No command matching User Information was found.
#

but I have it command in server

thick lynx
#

can you show your code for that?

clever flame
#

ok

#
const { ContextMenuCommandBuilder, ApplicationCommandType } = require('discord.js');

module.exports = {
    data : new ContextMenuCommandBuilder()
    .setName('User Information')
    .setType(ApplicationCommandType.User),

    async execute(interaction){
        await interaction.deferReply();
    },
}
thick lynx
#

what did you change in your interactionCreate

#

show the updated code for that

clever flame
#

oh ok

#
const { Events } = require('discord.js');
const wait = require('node:timers/promises').setTimeout;

module.exports = {
    name: Events.InteractionCreate,
    async execute(interaction) {
        if (!interaction.isCommand()) return;

        const command = interaction.client.commands.get(interaction.commandName);

        if (!command) {
            console.error(`No command matching ${interaction.commandName} was found.`);
            return;
        }
        

        try {
            await command.execute(interaction);
        } catch (error) {
            console.error(`Error executing ${interaction.commandName}`);
            console.error(error);
        }
    },
};
#

I used isCommand()

thick lynx
#

do you use another collection for context menus ?

clever flame
#

no

thick lynx
#

log interaction.client.commands

thick lynx
#

yeah please

clever flame
#

ok

thick lynx
#

do it right above const command

clever flame
#

I means line

thick lynx
#

console.log(interaction.client.commands) above const command

clever flame
#

ok

#

result is nothing :\

thick lynx
#

you need to execute an interaction like a slash command for something to log

#

that way the event is triggered

clever flame
#

Do you mean a script that records the commands?

thick lynx
#

just run a slash command

#

and then see if you get a log output

clever flame
#

Ok

#

I get this

#
ReferenceError: interaction is not defined
    at Object.<anonymous> (C:\Users\Arsham\Desktop\discord-project\dpc.js:16:14)
    at Module._compile (node:internal/modules/cjs/loader:1218:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
    at Module.load (node:internal/modules/cjs/loader:1081:32)
    at Module._load (node:internal/modules/cjs/loader:922:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47
thick lynx
#

what did you do

#

all you needed to do was put console.log(interaction.client.commands) above const command

clever flame
#

I tried to register the slash command with the script to use it, but I encountered this error

thick lynx
#

no you just needed to run a slash command. you didnt need to register anything

clever flame
#

i don't have slash command so i need it

thick lynx
#

wait then why did you have isChatInputCommand() in your event if you dont have any slash commands

#

just replace that with isContextMenuCommand()

#

i mean isCommand() works as well

clever flame
#

I had it before that, but then it was deleted due to deleting and updating my commands

thick lynx
#

you delete and re-register every time?

#

you shouldn't do that

#

make sure your application commands are fully registered

clever flame
#

Ok wait

clever flame
#

And then

#

Run a slash command?

thick lynx
#

yes. you technically didnt need to make one since you already have context menus, but its fine

clever flame
#

Ok

#

I get a collection information

thick lynx
#

oh great! can you show the output you get?

clever flame
#

It is related to a slash command, in fact my context menu is not in that list

clever flame
thick lynx
#

are the context menus registered?

clever flame
#
Collection(1) [Map] {
  'ping' => {
    data: SlashCommandBuilder {
      options: [],
      name: 'ping',
      name_localizations: undefined,
      description: 'Replies with Pong!',
      description_localizations: undefined,
      default_permission: undefined,
      default_member_permissions: undefined,
      dm_permission: undefined
    },
    execute: [AsyncFunction: execute]
  }
}
thick lynx
#

can you show your command handler?

clever flame
#

Ok

#

You mean interactionCreate?

thick lynx
#

no, the command handler

#

how you handle commands from files

clever flame
#

Oh i got it

#

Ok

#

Wait

#
for (const file of commandFiles) {
    const filePath = path.join(commandsPath, file);
    const command = require(filePath);

    if ('data' in command && 'execute' in command) {
        client.commands.set(command.data.name, command);
    } else {
        console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
    }
}
#

is this?

thick lynx
#

yeah thats perfect

#

are your context menus and slash command files in the same folder?

#

or are they in different folders

clever flame
#

No

#

In different

#

Commands in commands
And
Contexts menu in contexts

thick lynx
#

can you show your file tree?

since they're in different folders, your commands collection isn't setting the context menus. this means you'll need to make a context menu handler just like that code, or move the context files in the same place as your slash commands

#

you can still use the same collection

clever flame
#

So

thick lynx
#

can you show how you register commands?

clever flame
#
for (const file of Contextfiles) {
    const filePath = path.join(contextPath, file);
    const command = require(filePath);

    if ('data' in context && 'execute' in command) {
        client.commands.set(command.data.name, command);
    } else {
        console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
    }
}
#

And ....

thick lynx
#

technically yes

clever flame
# thick lynx can you show how you register commands?
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    commands.push(command.data.toJSON());
}


const rest = new REST({ version: '10' }).setToken(token);


(async () => {
    try {
        console.log(`Started refreshing ${commands.length} application (/) commands.`);


        const data = await rest.put(
            Routes.applicationGuildCommands(clientId, guildId),
            { body: commands },
        );

        console.log(`Successfully reloaded ${data.length} application (/) commands.`);
    } catch (error) {

        console.error(error);
    }
})();

// context

for (const file of contextFiles) {
    const context = require(`./contexts/${file}`);
    contexts.push(context.data.toJSON());
}

(async () => {
    try {
        console.log(`Started refreshing ${contexts.length} context menu.`);


        const data = await rest.put(
            Routes.applicationCommands(clientId, guildId),
            { body: contexts },
        );

        console.log(`Successfully reloaded ${data.length} context menu.`);
    } catch (error) {

        console.error(error);
    }
})();
thick lynx
clever flame
#

so thank you And sorry for the long time, I just started working with djs.

thick lynx
#

its fine. you're doing great

clever flame
#

Thanks.
Have nice day

thick lynx
#

i hope it works out for you. 😄

clever flame
#

😁

#
for (const file of contextFiles) {
    const filePath = path.join(contextsPath, file);
    const context = require(filePath);

    if ('data' in context && 'execute' in context) {
        client.commands.set(command.data.name, command);
    } else {
        console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
    }
}

its good? or need a change

thick lynx
#

looks good. try it

clever flame
#

ok

#

oh its not good

thick lynx
#

any error?

clever flame
#
Error: Cannot find module 'C:\Users\Arsham\Desktop\discord-project\context\ping.js'
Require stack:
- C:\Users\Arsham\Desktop\discord-project\index.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1039:15)
    at Module._load (node:internal/modules/cjs/loader:885:27)
    at Module.require (node:internal/modules/cjs/loader:1105:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (C:\Users\Arsham\Desktop\discord-project\index.js:31:18)
    at Module._compile (node:internal/modules/cjs/loader:1218:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
    at Module.load (node:internal/modules/cjs/loader:1081:32)
    at Module._load (node:internal/modules/cjs/loader:922:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'C:\\Users\\Arsham\\Desktop\\discord-project\\index.js' ]
}
thick lynx
#

adjust your file path

clever flame
#

uh i see it

#
client.commands.set(command.data.name, command);
                                    ^

ReferenceError: command is not defined
    at Object.<anonymous> (C:\Users\Arsham\Desktop\discord-project\index.js:34:23)
    at Module._compile (node:internal/modules/cjs/loader:1218:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
    at Module.load (node:internal/modules/cjs/loader:1081:32)
    at Module._load (node:internal/modules/cjs/loader:922:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47
#

oohh

#

i got it

thick lynx
#

it’s context yea

clever flame
#

oh its not work just log the result

thick lynx
#

Can you show the log

clever flame
#

wait

#

jere

#
client.commands.set(command.data.name, command);
thick lynx
#

use context, not command

clever flame
#

change commands to what

clever flame
thick lynx
#

don’t change client.commands

clever flame
#

yessssss

#

its woooork

thick lynx
#

just replace (command.data.name, command) to (context.data.name, context)

#

Oh yay!

clever flame
#

😁

#

thank you so much

thick lynx
#

I’m so happy it’s working

clever flame
#

me too xD

#

Does djs bot not have the command to close this chat?

thick lynx
#

right click the forum post and you should be able to

clever flame
#

Yes sure goodbye

clever flame
#

how to set this line

chilly drum
#

Just add a property

#

why would you need that anyways?