All files / src/core BotStateController.js

100% Statements 20/20
100% Branches 6/6
100% Functions 10/10
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83            43x   43x             27x 27x             22x 22x             35x             9x 3x   6x             5x             3x             2x 2x             5x 9x 5x   5x               4x
/**
 * BotStateController - Handles bot enable/disable state management
 * Manages individual bot instance states for multi-bot deployments
 */
class BotStateController {
    constructor(client) {
        this.client = client;
        // Bot enable/disable state tracking (Map of bot_id -> enabled status)
        this.botStates = new Map();
    }
 
    /**
     * Enable a bot by ID - allows the bot to respond to commands
     */
    enableBot(botId) {
        this.botStates.set(botId, true);
        console.log(`โœ… Bot ${botId} enabled`);
    }
 
    /**
     * Disable a bot by ID - bot will ignore all commands
     */
    disableBot(botId) {
        this.botStates.set(botId, false);
        console.log(`โŒ Bot ${botId} disabled`);
    }
 
    /**
     * Check if a bot is enabled (defaults to true if not set)
     */
    isBotEnabled(botId) {
        return this.botStates.get(botId) !== false;
    }
 
    /**
     * Check if this current bot instance is enabled
     */
    isThisBotEnabled() {
        if (!this.client || !this.client.user) {
            return true; // Default to enabled if client not ready
        }
        return this.isBotEnabled(this.client.user.id);
    }
 
    /**
     * Get the current bot's ID
     */
    getBotId() {
        return this.client?.user?.id || null;
    }
 
    /**
     * Get all bot states
     */
    getAllBotStates() {
        return new Map(this.botStates);
    }
 
    /**
     * Clear all bot states
     */
    clearAllStates() {
        this.botStates.clear();
        console.log('๐Ÿงน All bot states cleared');
    }
 
    /**
     * Get state statistics
     */
    getStateStats() {
        const total = this.botStates.size;
        const enabled = Array.from(this.botStates.values()).filter(state => state === true).length;
        const disabled = total - enabled;
        
        return {
            total,
            enabled,
            disabled
        };
    }
}
 
module.exports = BotStateController;