# trixxy-leveling

A powerful and customizable Discord leveling system with image generation capabilities, built for Trixxy projects.

## Features

-   **Automatic XP Gain**: Users gain XP by sending messages.
-   **Customizable Leveling Formula**: Adjust how much XP is needed for each level.
-   **Dynamic Level Cards**: Generate beautiful level cards with user avatars, XP, and level progress.
-   **Customizable Messages**: Set your own level-up messages.
-   **Flexible Configuration**: Easily change background, font color, and progress bar color.

## Installation

```bash
npm install trixxy-leveling
```

## Usage

### Basic Setup

```javascript
const { Client, GatewayIntentBits } = require('discord.js');
const TrixxyLeveling = require('trixxy-leveling');

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent,
    ],
});

const leveling = new TrixxyLeveling({
    client: client, // Your Discord.js client
    background: 'https://example.com/your-custom-background.png', // Optional: URL to a custom background image
    fontColor: '#FFD700', // Optional: Hex color for text
    progressBarColor: '#00FFFF', // Optional: Hex color for the progress bar
    levelUpMessage: 'Well done, {user}! You've reached level {level}!', // Optional: Custom level up message
    xpPerMessage: 15, // Optional: XP gained per message (default is 10)
    levelFormula: (level) => 10 * Math.pow(level, 2) + 100 * level + 200, // Optional: Custom XP formula
});

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

client.login('YOUR_BOT_TOKEN');

// Example command to show a user's level card
client.on('messageCreate', async message => {
    if (message.author.bot) return;
    if (message.content === '!level') {
        const userData = leveling.getUserData(message.author.id, message.guild.id);
        const attachment = await leveling.generateLevelCard(message.author, userData.xp, userData.level);
        message.channel.send({ files: [attachment] });
    }
});
```

### `TrixxyLeveling` Class

#### Constructor Options

-   `client` (required): Your `Discord.js` client instance. Used to listen for `messageCreate` events.
-   `background` (optional): `String` URL to a custom background image for the level card. Default is a placeholder image.
-   `fontColor` (optional): `String` Hex color code for the text on the level card. Default is `#ffffff`.
-   `progressBarColor` (optional): `String` Hex color code for the progress bar on the level card. Default is `#00ff00`.
-   `levelUpMessage` (optional): `String` Custom message sent when a user levels up. Supports `{user}` and `{level}` placeholders. Default is `'Congratulations, {user}! You leveled up to level {level}!'`.
-   `xpPerMessage` (optional): `Number` Amount of XP a user gains per message. Default is `10`.
-   `levelFormula` (optional): `Function` A function that takes the current `level` as an argument and returns the required XP for that level. Default is `(level) => 5 * Math.pow(level, 2) + 50 * level + 100`.

#### Methods

-   `handleMessage(message)`: (Internal) Processes incoming messages to award XP and check for level-ups. Automatically called if `client` is provided in the constructor.
-   `generateLevelCard(user, currentXp, currentLevel)`: `Async Function` Generates a level card image for the specified user. Returns a `Discord.js AttachmentBuilder`.
    -   `user`: A `Discord.js User` object.
    -   `currentXp`: `Number` The user's current XP.
    -   `currentLevel`: `Number` The user's current level.
-   `getUserData(userId, guildId)`: Returns an object `{ xp: Number, level: Number }` for the specified user and guild. If no data exists, returns `{ xp: 0, level: 0 }`.
-   `setUserData(userId, guildId, data)`: Sets the XP and level data for a specific user in a guild.

## Requirements

-   Node.js v16.6.0 or higher
-   `discord.js` v14 or higher
-   `canvas` library requires some system dependencies. Please refer to the [canvas documentation](https://github.com/Automattic/node-canvas#compiling) for installation instructions for your operating system.

## Contributing

Feel free to open issues or submit pull requests.

## License

This project is licensed under the MIT License.