I have a piece of code that automatically runs every 4 hours to send a set message to a specific discord channel on my discord server but I would like to have this function start upon command and I would like that command to be in a separate file from my main index code if someone could give me a template for this code as a slash command I would greatly appreciate it you would be saving me days possibly weeks of tedious research and trial and error I need code that is up to date with v13 of discord.js and that will be compatible with my main index file code which will be set below the specific function that I want accomplished.
setInterval(() => {
client.channels.cache.get("916642101989617684").send("!stats");
}, (1000 * 60) * 240);
const fs = require('fs');
const { Client, Collection, Intents } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
}
const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const event = require(`./events/${file}`);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
client.once('ready', () => {
console.log('Ready!');
});
setInterval(() => {
client.channels.cache.get("916642101989617684").send("!stats");
}, (1000 * 60) * 240);
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
return interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
client.login(token);
Simply put it in an interactionCreate event, but make sure it's once so that it doesn't constantly make more intervals
client.once("interactionCreate", async (interaction) => {
if (interaction.commandName === "register") { //or whatever name the command should be
//the code you want to start here
})