#so your saying its not possible

1 messages · Page 1 of 1 (latest)

worthy cradle
#

Tell me - what timeout are you trying to implement, specifically?

rustic summit
#

well basically

#

hold on

#
if (cmd.timeout) {
                const Timeout = require('../schemas/timeout')
                let data = await Timeout.findOne({ userId: interaction.user.id, command: cmd.name }) 
                let current_time = Math.floor(new Date().getTime());
                    let cooldown_amount = cmd.timeout;
                    let new_cooldown = current_time + cooldown_amount;
            if (data) {
              if (current_time > data.lastUse) {
                    await Timeout.findOneAndDelete({ userId: interaction.user.id, command: cmd.name })
                     await cmd.run(client, interaction, args);
             } else {
                 interaction.reply({ content: `Calm down, your on a cooldown, please wait \`${ms(data.lastUse - current_time)}\` until you can use this command again.`, ephemeral: true })
             }
            } else {
              await cmd.run(client, interaction, args);
                data = await new Timeout({
                    userId: interaction.user.id,
                    command: cmd.name,
                    lastUse: new_cooldown,
                });
                data.save()
            }
            } else {
              await cmd.run(client, interaction, args);
            }
#

I have this

#

its for a normal slash command

#

it saves the time to a database

#

and it compares the current date to the time used

#

and if the time has passed, it deletes the timeout

#

what i need to do is do this same thing, with a sub command

#

@worthy cradle

worthy cradle
#

What is cmd, where do you get it from

#

@rustic summit

rustic summit
#

this raps around to the beginning of our convo, its my slash command collection

let cmd = client.slashCommands.get(interaction.commandName);
worthy cradle
#

Ok but theres no timeout parameter for cmd. So the code above will always go the the else statement

rustic summit
#

well yes there is

#

for just the actual slash command and not a sub command,

#

i have a property

#

named timeout

#

and it uses ms

#

so if it was

#
timeout: 60000
#

then the timeout would be 60s

worthy cradle
#

Ok I'll get back to that in a minute
Where does client.slashCommands come from? That isn't discord.js

rustic summit
#
client.slashCommands = new Collection();
worthy cradle
#

k
How do you populate it

rustic summit
#
const slashCommands = await globPromise(
        `${process.cwd()}/SlashCommands/*/*.js`
    );

    const arrayOfSlashCommands = [];
    slashCommands.map((value) => {
        const file = require(value);
        if (!file?.name) return;
        client.slashCommands.set(file.name, file);

        if (["MESSAGE", "USER"].includes(file.type)) delete file.description;
        arrayOfSlashCommands.push(file);
    });
#

i think

#

since im using a command handler from reconlx

worthy cradle
#

Aight can you show me an example of the slash command object with a timeout

rustic summit
#
const { Client, CommandInteraction, ApplicationCommandType, EmbedBuilder } = require("discord.js");

module.exports = {
    name: "ping",
    description: "Ping.",
    type: ApplicationCommandType.ChatInput,
    timeout: 60000,

    /**
     * @param {Client} client
     * @param {CommandInteraction} interaction
     * @param {String[]} args
     */
    run: async (client, interaction, args) => {
            const embed = new EmbedBuilder()        
                .setDescription(`Ping: ${client.ws.ping}ms\`)

        return interaction.reply({ embeds: [embed] });
    }
};

worthy cradle
#

ok what about an example of a subcommand that you want to timeout on

rustic summit
#
const { Client, CommandInteraction, ApplicationCommandType, EmbedBuilder } = require("discord.js");

module.exports = {
    name: "ping",
    description: "Ping.",
    type: ApplicationCommandType.ChatInput,
    options: [
        {
           name: "test",
           description: "test",
           type: 1,
           timeout: 60000
         }
    ],

    /**
     * @param {Client} client
     * @param {CommandInteraction} interaction
     * @param {String[]} args
     */
    run: async (client, interaction, args) => {
            const embed = new EmbedBuilder()        
                .setDescription(`Ping: ${client.ws.ping}ms\`)

        return interaction.reply({ embeds: [embed] });
    }
};

worthy cradle
rustic summit
#

whats the subcommand type, im on mobile so idk

worthy cradle
#

The value for the subcommand type

#

That line basically tries to find the first option that has a type property with the subcommand type value

#

I think it's 1

rustic summit
#

so [1]?

worthy cradle
#

Without the brackets

#

option.type == 1

#

But discord.js has an enum value for that I think

#

Am I making sense orrrr

rustic summit
#

kinda ye

#

ill have to try tomm

#

2:23am

worthy cradle
#

kek

#

same

rustic summit
#

@worthy cradle heyy

#

its saying interaction.options.find isnt a function

rustic summit
#

@worthy cradle

worthy cradle
# rustic summit <@480090143307595786>

Mb. interaction.options is of type CommandInteractionOptionResolver, according to the documentation here:https://discord.js.org/#/docs/discord.js/main/class/ChatInputCommandInteraction?scrollTo=options
Try interaction.options.data.find instead of interaction.options.find

rustic summit
#
const subCmdName = interaction.options.data.find(option => option.type == 1); 

                let subCmd = cmd.options.find(option =>  option.name == subCmdName)
#

?

#

@worthy cradle

node:events:505
      throw er; // Unhandled 'error' event
      ^
TypeError: Cannot read properties of undefined (reading 'timeout')
    at Client.<anonymous> (/home/container/events/interactionCreate.js:51:31)
    at Client.emit (node:events:527:28)
    at InteractionCreateAction.handle (/home/container/node_modules/discord.js/src/client/actions/InteractionCreate.js:81:12)
    at Object.module.exports [as INTERACTION_CREATE] (/home/container/node_modules/discord.js/src/client/websocket/handlers/INTERACTION_CREATE.js:4:36)
    at WebSocketManager.handlePacket (/home/container/node_modules/discord.js/src/client/websocket/WebSocketManager.js:352:31)
    at WebSocketShard.onPacket (/home/container/node_modules/discord.js/src/client/websocket/WebSocketShard.js:481:22)
    at WebSocketShard.onMessage (/home/container/node_modules/discord.js/src/client/websocket/WebSocketShard.js:321:10)
    at WebSocket.onMessage (/home/container/node_modules/ws/lib/event-target.js:199:18)
    at WebSocket.emit (node:events:527:28)
    at Receiver.receiverOnMessage (/home/container/node_modules/ws/lib/websocket.js:1178:20)
Emitted 'error' event on Client instance at:
    at emitUnhandledRejectionOrErr (node:events:384:10)
    at processTicksAndRejections (node:internal/process/task_queues:85:21)
Node.js v17.9.1
#
{
            name: "search",
            description: "Search locations to gain balance.",
            type: ApplicationCommandOptionType.Subcommand,
            timeout: 60000
        }
worthy cradle
#

@rustic summit log subCmdName and cmd to console & paste the results here

rustic summit
# worthy cradle <@725172242744606810> log subCmdName and cmd to console & paste the results here
{ name: 'search', type: 1, options: [] }
{
  name: 'economy',
  description: 'Economy System.',
  type: 1,
  options: [
    {
      name: 'add',
      description: "Add to a user's balance.",
      type: 1,
      options: [Array]
    },
    {
      name: 'remove',
      description: "Remove from a user's balance.",
      type: 1,
      options: [Array]
    },
    {
      name: 'balance',
      description: "Check a user's balance.",
      type: 1,
      options: [Array]
    },
    {
      name: 'deposit',
      description: 'Deposit balance to your bank.',
      type: 1,
      options: [Array]
    },
    {
      name: 'withdraw',
      description: 'Withdraw balance to your wallet.',
      type: 1,
      options: [Array]
    },
    {
      name: 'pay',
      description: 'Pay a user.',
      type: 1,
      options: [Array]
    },
    { name: 'work', description: 'Work to gain balance.', type: 1 },
    {
      name: 'beg',
      description: 'Beg people to gain balance.',
      type: 1
    },
    {
      name: 'search',
      description: 'Search locations to gain balance.',
      type: 1,
      timeout: 60000
    }
  ],
  run: [AsyncFunction: run]
}```
worthy cradle
#

There you have your issue

#

Instead of const subCmdName = interaction.options.data.find(option => option.type == 1);
do:

const subCmdObj = interaction.options.data.find(option => option.type == 1) || {}; 
const subCmdName = subCmdObj.name;
#

@rustic summit

rustic summit
#

hmm

rustic summit
#

subCmdObj.timeout?

worthy cradle
rustic summit
#

holy shit

#

thank you so much lmao

worthy cradle
#

np

verbal tendon
#

what are you even trying to do

#

@rustic summit

rustic summit
#

which i have one issue with rn

verbal tendon
#

thats not... I mean with that code

rustic summit
#

oh

#

accessing the timeout from a sub command

#

like a sub command property

verbal tendon
#

so you want to get the subcommand name that was used

#

?

rustic summit
#

no lol

#

i just had to get the timeout

#

the value is milliseconds

#

which i got

#

its all working except i can use the command twice

#

then it delays it

worthy cradle
#

@verbal tendon He uses some kind of command manager, and stores additional metadata (not found in the interaction object) for some of the commands. He wanted to access a timeout property for a subcommand so that he can use logic to prevent a user from spamming a specific request

#

So the point was to get the subcommand name from the interaction object and get the command object handled by the command manager and access the timeout

verbal tendon
#

yes

#

that was my question

#

interaction.options.getSubcommand() would return the subcommand that was used

worthy cradle
#

Last time I used it, it was broken but hypothetically it should do the same job as the find function when defining subCmdName @rustic summit
So instead of the find statement, you can try interaction.options.getSubcommand(false)

#

It's ever so slightly faster performance-wise too

worthy cradle
rustic summit
#

your talking about subCmdObj right?

worthy cradle
#

no

rustic summit
#

oh LOL

worthy cradle
#

Wait no I'm retarded

rustic summit
worthy cradle
#

Yeah subCmdObj

#

show code

rustic summit
#
const subCmdObj = interaction.options.getSubcommand(false)
        const subCmdName = subCmdObj.name;
        let subCmd = cmd.options.find(option =>  option.name == subCmdName)
```?
worthy cradle
#

Actually no I'm ultra retarded. Don't mind me.
Try removing subCmdObj define line, and replace subCmdName with

const subCmdName = interaction.options.getSubcommand(false);
rustic summit
#

works

worthy cradle
#

K stick with that, it's cleaner

rustic summit
#

but

#

i can use the command twice

#

then it times me out

worthy cradle
#

That's likely an issue with your timeout logic

rustic summit
#

ah shit

#

i guess you cant help anymore since my timeout is mostly mongodb and not djs

worthy cradle
#

I can try, I use mongodb too

rustic summit
#

oh

#

hm

#

take this to dms then?

worthy cradle
#

But I'm assuming the actual logic is iffy. Try printing the timeouts to console first, see if anything looks off

#

Not the 60,000, but the stored times

rustic summit
#

ah

#
Last Use: 1660081792044
Current Time: 1660081882727