#how to set this line
142 messages · Page 1 of 1 (latest)
can you show your interactionCreate event handler?
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
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
Where should I call it?
you dont call it here. you only return if the interaction isn't a ContextMenuCommandInteraction
you should call it there
or replace isChatInputCommand() to isCommand(), which should also work
Can you show me how this is in code?
just execute()?
it takes an interaction param
as shown in your code when listening for chat input commands above
i get this error
No command matching User Information was found.
but I have it command in server
can you show your code for that?
ok
const { ContextMenuCommandBuilder, ApplicationCommandType } = require('discord.js');
module.exports = {
data : new ContextMenuCommandBuilder()
.setName('User Information')
.setType(ApplicationCommandType.User),
async execute(interaction){
await interaction.deferReply();
},
}
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()
do you use another collection for context menus ?
no
log interaction.client.commands
in this>
?
yeah please
ok
do it right above const command
I don't know where used it
I means line
console.log(interaction.client.commands) above const command
you need to execute an interaction like a slash command for something to log
that way the event is triggered
Do you mean a script that records the commands?
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
what did you do
all you needed to do was put console.log(interaction.client.commands) above const command
I tried to register the slash command with the script to use it, but I encountered this error
no you just needed to run a slash command. you didnt need to register anything
i don't have slash command so i need it
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
I had it before that, but then it was deleted due to deleting and updating my commands
you delete and re-register every time?
you shouldn't do that
make sure your application commands are fully registered
Ok wait
I do it
And then
Run a slash command?
yes. you technically didnt need to make one since you already have context menus, but its fine
oh great! can you show the output you get?
It is related to a slash command, in fact my context menu is not in that list
Ok
are the context menus registered?
Yes
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]
}
}
can you show your command handler?
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?
yeah thats perfect
are your context menus and slash command files in the same folder?
or are they in different folders
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
So
can you show how you register commands?
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 ....
technically yes
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);
}
})();
ah now i see. so yes this is what you would need to do then
so thank you And sorry for the long time, I just started working with djs.
its fine. you're doing great
Thanks.
Have nice day
i hope it works out for you. 😄
😁
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
looks good. try it
any error?
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' ]
}
adjust your file path
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
it’s context yea
oh its not work just log the result
Can you show the log
use context, not command
change commands to what
just thaht?
don’t change client.commands
I’m so happy it’s working
right click the forum post and you should be able to
Yes sure goodbye
how to set this line