tindolt
tindolt
DIAdiscord.js - Imagine a boo! 👻
Created by tindolt on 11/19/2023 in #djs-questions
Cooldowns not correctly working
// Requiring Classes necessary for functioning bot
const fs = require('node.fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');

// Creating new client
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.commands = new Collection();
client.cooldowns = new Collection();

const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
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 require "data" or "execute" property.`);
}
}
}

const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}

//Log into discord with token
client.login(token);
// Requiring Classes necessary for functioning bot
const fs = require('node.fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');

// Creating new client
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.commands = new Collection();
client.cooldowns = new Collection();

const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
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 require "data" or "execute" property.`);
}
}
}

const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}

//Log into discord with token
client.login(token);
10 replies
DIAdiscord.js - Imagine a boo! 👻
Created by tindolt on 11/19/2023 in #djs-questions
Cooldowns not correctly working
const { Events, Collection } = require('discord.js');


module.exports = {
name: Events.InteractionCreate,
async execute(interaction) {

const { cooldowns } = interaction.client;

if (!interaction.isChatInputCommand()) return;

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

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

if (!cooldowns.has(command.data.name)) {
cooldowns.set(command.data.name, new Collection());
}

const now = Date.now();
const timestamps = cooldowns.get(command.data.name);
const defaultCooldownDuration = 3;
const cooldownAmount = (command.cooldown ?? defaultCooldownDuration) * 1000;

if (timestamps.has(interaction.user.id)) {
const expirationTime = timestamps.get(interaction.user.id) + cooldownAmount;

if (now < expirationTime) {
const expiredTimestamp = Math.round(expirationTime / 1000);
return interaction.reply({ content: `Please wait, this command is on cooldown for \`${command.data.name}\`. You can use it again at <t:${expiredTimestamp}:R>.`, ephemeral: true});
}
}

timestamps.set(interaction.user.id, now);
setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount);

try {
await command.execute(interaction);
} catch (error) {
console.error(error);

await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
},
};
const { Events, Collection } = require('discord.js');


module.exports = {
name: Events.InteractionCreate,
async execute(interaction) {

const { cooldowns } = interaction.client;

if (!interaction.isChatInputCommand()) return;

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

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

if (!cooldowns.has(command.data.name)) {
cooldowns.set(command.data.name, new Collection());
}

const now = Date.now();
const timestamps = cooldowns.get(command.data.name);
const defaultCooldownDuration = 3;
const cooldownAmount = (command.cooldown ?? defaultCooldownDuration) * 1000;

if (timestamps.has(interaction.user.id)) {
const expirationTime = timestamps.get(interaction.user.id) + cooldownAmount;

if (now < expirationTime) {
const expiredTimestamp = Math.round(expirationTime / 1000);
return interaction.reply({ content: `Please wait, this command is on cooldown for \`${command.data.name}\`. You can use it again at <t:${expiredTimestamp}:R>.`, ephemeral: true});
}
}

timestamps.set(interaction.user.id, now);
setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount);

try {
await command.execute(interaction);
} catch (error) {
console.error(error);

await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
},
};
for the archive of what fixed it
10 replies
DIAdiscord.js - Imagine a boo! 👻
Created by tindolt on 11/19/2023 in #djs-questions
Cooldowns not correctly working
ah there we go! thank you!
10 replies
DIAdiscord.js - Imagine a boo! 👻
Created by tindolt on 11/19/2023 in #djs-questions
Cooldowns not correctly working
Ok so put that in my index.js and call it into my command handler
10 replies
DIAdiscord.js - Imagine a boo! 👻
Created by tindolt on 11/19/2023 in #djs-questions
Cooldowns not correctly working
14.14.1 discord js 18.13.0 node
10 replies