Pta Nhi

Change the name to GENX QUESTOR

Kodari AI
KodariNov 12 at 10:28 am 1
  1. Rename the package to genx-questor (npm-safe spelling).

  2. Update its description to use GENX QUESTOR.

Renamed the package to genx-questor and updated its description to GENX QUESTOR. The bot’s visible Discord username is managed in the Discord Developer Portal.

Pta Nhi

Ok now start the bot and tell me all commands

Kodari AI
KodariNov 12 at 10:28 am 1

I can’t launch the bot process from this chat. Start it with npm start after setting a valid DISCORD_TOKEN in .env; it registers its slash command when it connects.

Commands:

  • /quest list — view quests and your progress.

  • /quest create name:<name> messages:<count> reward:<orbs> — create a quest; requires Manage Server.

  • /quest claim name:<name> — claim a completed quest’s orb reward.

  • /quest orbs — check your orb balance.

Pta Nhi

I'm not able to add token idk how

Kodari AI
KodariNov 12 at 10:29 am 1

Add your bot token in a file named .env in the project’s top-level folder—the same folder as package.json:

Env
DISCORD_TOKEN=paste_your_bot_token_here

To get the token: open the Discord Developer Portal, select your application, go to Bot, then click Reset Token (or View Token) and copy it. Paste it after DISCORD_TOKEN= and save the file, then restart the bot.

Use the bot token, not the Application ID or Public Key. Don’t share the token with anyone; if you accidentally do, reset it in the Developer Portal.

Kodari can make mistakes. Review important code before using.
Files
.gitignore
bot.js
package.json
Code
3 211
bot.js
const fs = require('node:fs');
const path = require('node:path');
const { Client, GatewayIntentBits, PermissionFlagsBits } = require('discord.js');
require('dotenv').config();

const DATA_DIRECTORY = path.join(__dirname, 'data');
const DATA_FILE = path.join(DATA_DIRECTORY, 'quests.json');
const client = new Client({
    intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages]
});

function loadData() {
    try {
        return JSON.parse(fs.readFileSync(DATA_FILE, 'utf8'));
    } catch (error) {
        if (error.code === 'ENOENT') return { guilds: {} };
        throw error;
    }
}

const data = loadData();

function saveData() {
    fs.mkdirSync(DATA_DIRECTORY, { recursive: true });
    const temporaryFile = `${DATA_FILE}.tmp`;
    fs.writeFileSync(temporaryFile, JSON.stringify(data, null, 2));
    fs.renameSync(temporaryFile, DATA_FILE);
}

function getGuildData(guildId) {
    if (!data.guilds[guildId]) data.guilds[guildId] = { quests: [], balances: {} };
    return data.guilds[guildId];
}

const questCommand = {
    name: 'quest',
    description: 'View and manage this server’s custom quests',
    options: [
        {
            type: 1,
            name: 'list',
            description: 'View quests and your progress'
        },
        {
            type: 1,
            name: 'create',
            description: 'Create a message-count quest (requires Manage Server)',
            options: [
                { type: 3, name: 'name', description: 'Quest name', required: true, max_length: 80 },
                { type: 4, name: 'messages', description: 'Messages needed to complete it', required: true, min_value: 1, max_value: 100000 },
                { type: 4, name: 'reward', description: 'Orbs awarded when claimed', required: true, min_value: 0, max_value: 1000000 }
            ]
        },
        {
            type: 1,
            name: 'claim',
            description: 'Manually claim a completed quest',
            options: [
                { type: 3, name: 'name', description: 'Quest name', required: true, max_length: 80 }
            ]
        },
        {
            type: 1,
            name: 'orbs',
            description: 'Check your orb balance'
        }
    ]
};

client.once('clientReady', async () => {
    try {
        await client.application.commands.set([questCommand]);
        console.log(`Bot is ready! Logged in as ${client.user.tag}`);
        console.log('KODARI_READY');
    } catch (error) {
        console.error('Failed to register slash commands:', error);
    }
});

client.on('messageCreate', (message) => {
    if (!message.guild || message.author.bot) return;

    const guildData = getGuildData(message.guild.id);
    let changed = false;

    for (const quest of guildData.quests) {
        if (quest.claimed.includes(message.author.id)) continue;
        const currentProgress = quest.progress[message.author.id] || 0;
        if (currentProgress >= quest.messages) continue;
        quest.progress[message.author.id] = currentProgress + 1;
        changed = true;
    }

    if (changed) saveData();
});

client.on('interactionCreate', async (interaction) => {
    if (!interaction.isChatInputCommand() || interaction.commandName !== 'quest') return;
    if (!interaction.guildId) {
        await interaction.reply({ content: 'Use this command inside a server.', ephemeral: true });
        return;
    }

    try {
        const guildData = getGuildData(interaction.guildId);
        const subcommand = interaction.options.getSubcommand();

        if (subcommand === 'create') {
            if (!interaction.memberPermissions?.has(PermissionFlagsBits.ManageGuild)) {
                await interaction.reply({ content: 'You need Manage Server permission to create quests.', ephemeral: true });
                return;
            }

            const name = interaction.options.getString('name', true).trim();
            if (!name) {
                await interaction.reply({ content: 'Quest names cannot be empty.', ephemeral: true });
                return;
            }
            if (guildData.quests.some((quest) => quest.name.toLowerCase() === name.toLowerCase())) {
                await interaction.reply({ content: 'A quest with that name already exists.', ephemeral: true });
                return;
            }

            const messages = interaction.options.getInteger('messages', true);
            const reward = interaction.options.getInteger('reward', true);
            guildData.quests.push({ name, messages, reward, progress: {}, claimed: [] });
            saveData();
            await interaction.reply(`Quest **${name}** created: send ${messages} messages, then use \/quest claim to collect **${reward} orbs**.`);
            return;
        }

        if (subcommand === 'list') {
            if (guildData.quests.length === 0) {
                await interaction.reply({ content: 'There are no quests yet. A server manager can add one with `/quest create`.', ephemeral: true });
                return;
            }

            const lines = guildData.quests.map((quest) => {
                const claimed = quest.claimed.includes(interaction.user.id);
                const progress = Math.min(quest.progress[interaction.user.id] || 0, quest.messages);
                const status = claimed ? 'claimed' : `${progress}/${quest.messages} messages`;
                return `• **${quest.name}** — ${status}; reward: ${quest.reward} orbs`;
            });
            await interaction.reply({ content: lines.join('\n'), ephemeral: true });
            return;
        }

        if (subcommand === 'claim') {
            const name = interaction.options.getString('name', true).trim();
            const quest = guildData.quests.find((entry) => entry.name.toLowerCase() === name.toLowerCase());
            if (!quest) {
                await interaction.reply({ content: 'I couldn’t find that quest. Check `/quest list` for its name.', ephemeral: true });
                return;
            }
            if (quest.claimed.includes(interaction.user.id)) {
                await interaction.reply({ content: 'You have already claimed that quest.', ephemeral: true });
                return;
            }

            const progress = quest.progress[interaction.user.id] || 0;
            if (progress < quest.messages) {
                await interaction.reply({ content: `Not complete yet: ${progress}/${quest.messages} messages.`, ephemeral: true });
                return;
            }

            quest.claimed.push(interaction.user.id);
            guildData.balances[interaction.user.id] = (guildData.balances[interaction.user.id] || 0) + quest.reward;
            saveData();
            await interaction.reply(`Quest **${quest.name}** claimed! You received **${quest.reward} orbs**.`);
            return;
        }

        if (subcommand === 'orbs') {
            const balance = guildData.balances[interaction.user.id] || 0;
            await interaction.reply({ content: `You have **${balance} orbs**.`, ephemeral: true });
        }
    } catch (error) {
        console.error('Quest command failed:', error);
        const response = { content: 'Something went wrong while handling that quest command.', ephemeral: true };
        if (interaction.replied || interaction.deferred) await interaction.followUp(response);
        else await interaction.reply(response);
    }
});

client.on('error', (error) => console.error('Discord client error:', error));
process.on('unhandledRejection', (error) => console.error('Unhandled promise rejection:', error));

if (!process.env.DISCORD_TOKEN || process.env.DISCORD_TOKEN === 'your_discord_bot_token_here') {
    throw new Error('Set DISCORD_TOKEN in .env to your Discord bot token before starting the bot.');
}

client.login(process.env.DISCORD_TOKEN).catch((error) => {
    console.error('Failed to log in. Check DISCORD_TOKEN in .env:', error);
    process.exitCode = 1;
});