1

I've been trying to code myself something like a "shift logger bot".

And since I'm pretty new to coding with discord.js in version 14 I can't really repair the mistake I've made in my code. Can someone help?

**const client = new Client({ intents: [Intents.FLAGS.GUILDS] }); ** <-- this is the error in terminal

const { Client, Intents } = require("discord.js");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const { Collection } = require("discord.js");

const commands = [
    {
        name: "start",
        description: "Starts a timer",
    },
    {
        name: "stop",
        description: "Stops the current timer and shows the elapsed time",
    },
    {
        name: "times",
        description: "Shows a table of all the recorded times",
    },
];

const rest = new REST({ version: "9" }).setToken(process.env.TOKEN);

const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

client.commands = new Collection();

client.once("ready", async () => {
    console.log(`Logged in as ${client.user.tag}!`);

    try {
        await rest.put(Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID), { body: commands });

        console.log("Slash commands registered!");
    } catch (error) {
        console.error(error);
    }
});

const timers = new Collection();
let intervalID;

client.on("interactionCreate", async (interaction) => {
    if (!interaction.isCommand()) return;

    if (interaction.commandName === "start") {
        if (timers.has(interaction.user.id)) {
            await interaction.reply("You already have a timer running!");
            return;
        }

        timers.set(interaction.user.id, Date.now());
        await interaction.reply("Timer started!");
        intervalID = setInterval(() => {
            timers.set(interaction.user.id, Date.now());
        }, 1000);
    } else if (interaction.commandName === "stop") {
        if (!timers.has(interaction.user.id)) {
            await interaction.reply("You don't have a timer running!");
            return;
        }

        clearInterval(intervalID);
        const startTime = timers.get(interaction.user.id);
        const elapsedTime = Date.now() - startTime;
        timers.delete(interaction.user.id);
        await interaction.reply(`Timer stopped! Elapsed time: ${elapsedTime}ms`);
    } else if (interaction.commandName === "times") {
        if (timers.size === 0) {
            await interaction.reply("No recorded times!");
            return;
        }

        let response = "Recorded times:\n";
        timers.forEach((value, key) => {
            const elapsedTime = Date.now() - value;
            response += `<@${key}>: ${elapsedTime}ms\n`;
        });
        await interaction.reply(response);
    }
});

client.login(process.env.TOKEN);

I want to get my shift logger bot working.

Jakye
  • 6,440
  • 3
  • 19
  • 38
Dwelt
  • 11
  • 2
  • Does this answer your question? [Discord.js v13 code breaks when upgrading to v14](https://stackoverflow.com/questions/73028854/discord-js-v13-code-breaks-when-upgrading-to-v14) – Zsolt Meszaros Feb 22 '23 at 18:18

0 Answers0