Team Ai
Apppublic

Stryker-SC/Discord-Bot-VCMaster-JavaScript-Revamped

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes
guildMemberAdd.js44 linesDownload Raw Back to events
1const { Events } = require('discord.js');2const logger = require('../utils/logger');3 4module.exports = {5    name: Events.GuildMemberAdd,6    async execute(member, client) {7        // Initialize cache map if it doesn't exist8        if (!client.roleConfigCache) client.roleConfigCache = new Map();9 10        // Check local cache first, fallback to database11        let config = client.roleConfigCache.get(member.guild.id);12        if (config === undefined) {13            config = await client.db.getRoleConfig(member.guild.id);14            client.roleConfigCache.set(member.guild.id, config || null);15        }16 17        if (!config) return;18 19        try {20            if (member.user.bot) {21                // Check if bot role is configured22                if (config.auto_bot_role_id) {23                    const role = member.guild.roles.cache.get(config.auto_bot_role_id);24                    if (role) {25                        member.roles.add(role).catch(error => logger.error(`[AutoRole] Failed to assign role to bot:`, error));26                        logger.info(`[AutoRole] Dispatched assignment of ${role.name} to bot ${member.user.tag}`);27                    }28                }29            } else {30                // Check if human role is configured31                if (config.auto_join_role_id) {32                    const role = member.guild.roles.cache.get(config.auto_join_role_id);33                    if (role) {34                        member.roles.add(role).catch(error => logger.error(`[AutoRole] Failed to assign role to user:`, error));35                        logger.info(`[AutoRole] Dispatched assignment of ${role.name} to user ${member.user.tag}`);36                    }37                }38            }39        } catch (error) {40            logger.error(`[AutoRole] Failed to assign role:`, error);41        }42    },43};44