const GReYBot = require('../../greybot');

let recentMembers = [];
let kicking = false;
const raidProtectionSeconds = 10;
const raidProtectionUserLimit = 4;

GReYBot.Discord.on('guildMemberAdd', member => {
    const channel = member.guild.defaultChannel;

    // Determine if the antiraid needs to be disabled.
    const resetJoinCount = (kicking && member.joinedTimestamp - recentMembers[recentMembers.length - 1].joinedTimestamp > raidProtectionSeconds * 1000);

    if (recentMembers.length >= raidProtectionUserLimit && !resetJoinCount) {
        // If we haven't started kicking, do so now.
        if (!kicking) {
            if (channel) channel.send('Antiraid measures have been activated.');
            recentMembers.forEach(recentMember => {
                recentMember.kick('Antiraid protection').then(() => {
                    if (channel) channel.send(`${recentMember.displayName} was kicked due to antiraid protection.`)
                });
            });
            kicking = true;
        }

        // To prevent the array from getting too long, remove the first member and add
        // the last so we only show the people who caused the last threshold to be hit.
        recentMembers.shift();
        recentMembers.push(member);
        member.kick('Antiraid protection').then(() => {
            if (channel) channel.send(`${member.displayName} was kicked due to antiraid protection.`);
        });
        return;
    }

    // Remove all users that are outside the protection timeframe.
    if (resetJoinCount) {
        if (channel) channel.send('Antiraid measures have been deactivated.');
        recentMembers = [];
        kicking = false;
    }
    // Add the user to recent users and send the greeting.
	recentMembers.push(member);
	

    if(member.guild.id === GReYBot.Config.welcomeGuild) {
		member.send(`Welcome, ${member.user.username}, to the GReY Community!\nBe sure to read our <#${GReYBot.Config.welcomeChannel}> channel for an overview of our rules and features.`);
	}

	channel.send(`@here, please Welcome ${member.user.username} to ${member.guild.name}!`);    
});
const GReYBot = require('../../greybot');

GReYBot.watchingGuilds = {};
const raidProtectionSeconds = 10;
const raidProtectionUserLimit = 4;

GReYBot.Discord.on('guildMemberAdd', member => {
    const guildId = member.guild.id;
    const guild = GReYBot.Discord.guilds.find('id', guildId);
    
    // Add the server to the list of watched guild
    if (!GReYBot.watchingGuilds[guildId]) {
        GReYBot.watchingGuilds[guildId] = {
            channel: guild ? guild.defaultChannel : null,
            seconds: raidProtectionSeconds,
            limit: raidProtectionUserLimit,
            recentMembers: [],
            kicking: false
        };
    }

    // Get the settings for the server
    const guildSettings = GReYBot.watchingGuilds[guildId];
    const channel = guildSettings.channel;
    const seconds = guildSettings.seconds;
    const limit = guildSettings.limit;

    // Determine if the antiraid needs to be disabled.
    const resetJoinCount = (
        guildSettings.kicking && 
        member.joinedTimestamp - guildSettings.recentMembers[guildSettings.recentMembers.length - 1].joinedTimestamp > seconds * 1000
    );

    if (guildSettings.recentMembers.length >= limit && !resetJoinCount) {
        // If we haven't started kicking, do so now.
        if (!guildSettings.kicking) {
            if (channel) channel.send('Antiraid measures have been activated.');
            guildSettings.recentMembers.forEach(recentMember => {
                recentMember.kick('Antiraid protection').then(() => {
                    if (channel) channel.send(`${recentMember.displayName} was kicked due to antiraid protection.`)
                });
            });

            guildSettings.kicking = true;
        }

        // To prevent the array from getting too long, remove the first member and add
        // the last so we only show the people who caused the last threshold to be hit.
        guildSettings.recentMembers.shift();
        guildSettings.recentMembers.push(member);
        member.kick('Antiraid protection').then(() => {
            if (channel) channel.send(`${member.displayName} was kicked due to antiraid protection.`);
        });

        return;
    }

    // Remove all users that are outside the protection timeframe.
    if (resetJoinCount) {
        if (channel) channel.send('Antiraid measures have been deactivated.');
        guildSettings.recentMembers = [];
        guildSettings.kicking = false;
    }
    // Add the user to recent users and send the greeting.
	guildSettings.recentMembers.push(member);
	

    if(!guildSettings.kicking && member.guild.id === GReYBot.Config.welcomeGuild) {
        member.send(`Welcome, ${member.user.username}, to the GReY Community!\nBe sure to read our <#${GReYBot.Config.welcomeChannel}> channel for an overview of our rules and features.`);
	    if (channel) channel.send(`@here, please Welcome ${member.user.username} to ${member.guild.name}!`);    
	}
});
Allow each server to have its own antiraid settings - bug #13