#so your saying its not possible
1 messages · Page 1 of 1 (latest)
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
this raps around to the beginning of our convo, its my slash command collection
let cmd = client.slashCommands.get(interaction.commandName);
Ok but theres no timeout parameter for cmd. So the code above will always go the the else statement
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
Ok I'll get back to that in a minute
Where does client.slashCommands come from? That isn't discord.js
its a collection in my index.js
client.slashCommands = new Collection();
k
How do you populate it
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
Aight can you show me an example of the slash command object with a timeout
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] });
}
};
ok what about an example of a subcommand that you want to timeout on
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] });
}
};
ok so you could do
const subCmdName = interaction.options.find(option => option.type == /*[subcommand type, I'm on mobile so idk]*/);
let subCmd = cmd.options.find(option => option.name == subCmdName)
//And then access
subCmd.timeout
whats the subcommand type, im on mobile so idk
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
so [1]?
Without the brackets
option.type == 1
But discord.js has an enum value for that I think
Am I making sense orrrr
@worthy cradle
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
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
}
@rustic summit 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]
}```
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
hmm
and how would i access timeout
subCmdObj.timeout?
Nope, same code as last time
let subCmd = cmd.options.find(option => option.name == subCmdName)
console.log(subCmd.timeout)
np
cooldown system lol
which i have one issue with rn
thats not... I mean with that code
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
@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
yes
that was my question
interaction.options.getSubcommand() would return the subcommand that was used
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
hm
nope doesnt work
show that line of code
your talking about subCmdObj right?
oh LOL
Wait no I'm retarded
feel the same sometimes
const subCmdObj = interaction.options.getSubcommand(false)
const subCmdName = subCmdObj.name;
let subCmd = cmd.options.find(option => option.name == subCmdName)
```?
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);
works
K stick with that, it's cleaner
That's likely an issue with your timeout logic
ah shit
i guess you cant help anymore since my timeout is mostly mongodb and not djs
I can try, I use mongodb too