Context Menu

Hey, I'm trying to register context menu commands but not sure how. I've a command handler that sending a put request to discord with a set of comamnds, and it does work well, but im not sure how to do that with the context menus.. example of a menu event file:
import type { ContextMenu } from '@/types/discord'
import { ApplicationCommandType, ContextMenuCommandBuilder } from 'discord.js'

const ClearCommand: ContextMenu = {
command: new ContextMenuCommandBuilder().setName('Profile').setType(ApplicationCommandType.User),

execute: (interaction) => {
console.log(interaction)
},

description: 'Gets the user profile in the website',
cooldown: 5,
staff: false,
}

export default ClearCommand
import type { ContextMenu } from '@/types/discord'
import { ApplicationCommandType, ContextMenuCommandBuilder } from 'discord.js'

const ClearCommand: ContextMenu = {
command: new ContextMenuCommandBuilder().setName('Profile').setType(ApplicationCommandType.User),

execute: (interaction) => {
console.log(interaction)
},

description: 'Gets the user profile in the website',
cooldown: 5,
staff: false,
}

export default ClearCommand
And this is my menu handler, which doesnt work. sometimes it does work for 2 seconds and then doesnt for a few minutes, but im not even think it should work like that..
...
const dir = readdirp(menusDir, { fileFilter: ['*.ts', '*.js'] })

for await (const file of dir) {
let contextMenu: ContextMenu = (await import(`${pathToFileURL(file.fullPath)}`)).default
contextMenus.push(contextMenu.command)
client.contextMenus.set(contextMenu.command.name, contextMenu)
}

const rest = new REST({ version: '10' }).setToken(process.env.BOT_TOKEN!)

const isDev = process.env.MODE === 'dev'

const route = isDev
? Routes.applicationGuildCommands(process.env.BOT_CLIENT_ID!, process.env.GUILD_ID!)
: Routes.applicationCommands(process.env.BOT_CLIENT_ID!)

// Should I do that?
const data: any = await rest.put(route, {
body: contextMenus,
headers: { 'Content-Type': 'application/json' },
})
...
...
const dir = readdirp(menusDir, { fileFilter: ['*.ts', '*.js'] })

for await (const file of dir) {
let contextMenu: ContextMenu = (await import(`${pathToFileURL(file.fullPath)}`)).default
contextMenus.push(contextMenu.command)
client.contextMenus.set(contextMenu.command.name, contextMenu)
}

const rest = new REST({ version: '10' }).setToken(process.env.BOT_TOKEN!)

const isDev = process.env.MODE === 'dev'

const route = isDev
? Routes.applicationGuildCommands(process.env.BOT_CLIENT_ID!, process.env.GUILD_ID!)
: Routes.applicationCommands(process.env.BOT_CLIENT_ID!)

// Should I do that?
const data: any = await rest.put(route, {
body: contextMenus,
headers: { 'Content-Type': 'application/json' },
})
...
Thanks!
12 Replies
d.js toolkit
d.js toolkit11mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
treble/luna
treble/luna11mo ago
Its the same way as you would with regular commands
ShiNxz
ShiNxz11mo ago
Using Routes.applicationGuildCommands with the rest function? it does't really work for me I mean I don't see the application option in my guild and also the rest.put function stuck.
youssef
youssef11mo ago
Yes with that
ShiNxz
ShiNxz11mo ago
As I said it doesnt work as expected.. maybe the slashcommands handler getting this one stuck? all my handlers run at the same time and the slashCommands handler sending a put request with the other commands at the same time as this one, it can cause problems?
Ricardo
Ricardo11mo ago
i have a typescript handler I can give you Took me around 3 projects to get the perfect handler
ShiNxz
ShiNxz11mo ago
Please do
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
ShiNxz
ShiNxz11mo ago
it just that it works but for example when starting the application for the first time it wil lwork, then another bot restart will make it not work for like 5 - 10 minutes and a restart will get it to work until the next bot restart
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
ShiNxz
ShiNxz11mo ago
currently on local host but seems like the slash commands register fine which is weird
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View