My anti-raid function doesn't work

Hello, I want to create a function that kicks new members during 30 seconds if 10 members join in 10 seconds. I am on this for now 1 entire day, but cant find how to do it. Could you help me please ?
41 Replies
d.js docs
d.js docs2y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
'NoXz
'NoXz2y ago
node v17.3.0 discord.js v14.1.1 for the moment I have this
const memberadd = 0;

client.on("guildMemberAdd", member => {
console.log("member joined");
memberadd = memberadd +1;
})


if (memberadd < 10) {
setTimeout(10000);
memberadd = 0
} else {
member.kick()
setTimeout(30000);
}
const memberadd = 0;

client.on("guildMemberAdd", member => {
console.log("member joined");
memberadd = memberadd +1;
})


if (memberadd < 10) {
setTimeout(10000);
memberadd = 0
} else {
member.kick()
setTimeout(30000);
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
'NoXz
'NoXz2y ago
okay ty Okay so now I have this, but still doesnt work
var memberadd = 9;
var raidmode = false;

client.on("guildMemberAdd", member => {
if (raidmode) {
member.kick();
} else {
memberadd = memberadd + 1;
console.log(member.user.tag+" joined the guild !");
console.log(memberadd)
}
});

while (true) {
if (raidmode || memberadd < 10) return;
if (memberadd >= 10) raidmode = true;
if (memberadd >= 10) {
console.log('yippi')
setTimeout(function () {
raidmode = false;
memberadd = 0;
}, 30000);
}
}
var memberadd = 9;
var raidmode = false;

client.on("guildMemberAdd", member => {
if (raidmode) {
member.kick();
} else {
memberadd = memberadd + 1;
console.log(member.user.tag+" joined the guild !");
console.log(memberadd)
}
});

while (true) {
if (raidmode || memberadd < 10) return;
if (memberadd >= 10) raidmode = true;
if (memberadd >= 10) {
console.log('yippi')
setTimeout(function () {
raidmode = false;
memberadd = 0;
}, 30000);
}
}
the console.log(member.user.tag+" joined the guild !"); and console.log(memberadd) works and return what they should So i think the issue is from the while, but i cant figure where
SpecialSauce
SpecialSauce2y ago
Your while loop only executes once because it’s outside in global
probablyraging
Your while condition isn't defined, you've just set the condition as true. I think you mean to have it set a while (raidmode) { while raidmode = true
SpecialSauce
SpecialSauce2y ago
I’d probably advise against a while(true) anyway..... you could easily handle it better in the event logic without a loop.
'NoXz
'NoXz2y ago
Wdym
probablyraging
Everytime a member joins, the event is fired. Check if raidmode is true and run your code inside that
SpecialSauce
SpecialSauce2y ago
Something like that would likely lock the runtime if the loop doesn’t break immediately. There’s also a field in guild members that tells you when they joined. So you could keep a record of the last 10 members to join and check if 10 seconds has elapsed since the first member on the list joined.
'NoXz
'NoXz2y ago
Yes I thought about that but I dont know at all how to do it, so I tried doing it like it is right now
probablyraging
<GuildMember>.joinedTimestamp Then just compare the newest member's timestamp to the previous members
d.js docs
d.js docs2y ago
class GuildMember (extends Base) Represents a member of a guild on Discord.
'NoXz
'NoXz2y ago
Okay thanks How do I compare ?
SpecialSauce
SpecialSauce2y ago
Check out array.slice and array.push as well That field is actually a number so you could just subtract
probablyraging
Probably subtract the previous timestamp from the new timestamp, which will give you the difference in milliseconds difference. Then an if statement?
'NoXz
'NoXz2y ago
okay I'll try that thanks Prayge
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
'NoXz
'NoXz2y ago
the 30s is the duration of the raidmode so it kicks new users during 30s Im trying this, seems to be a good way to do it Could you show your code or not ?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
'NoXz
'NoXz2y ago
okay ty Prayge
client.on("guildMemberAdd", member => {
const joined = member.joinedTimestamp;
if (raidmode == true) {
member.kick();
} else {
console.log(member.user.tag+" joined the guild ! - "+joined);
newMemberCount = newMemberCount + 1;
}
});

while (raidmode == false) {
setInterval(function{
if (newMemberCount >= 10) {
raidmode = true;
} else {
newMemberCount = 0;
}
}, 10000)
}
if (raidmode == true) {
setTimeout(function(){
raidmode = false;
}, 30000);
}
client.on("guildMemberAdd", member => {
const joined = member.joinedTimestamp;
if (raidmode == true) {
member.kick();
} else {
console.log(member.user.tag+" joined the guild ! - "+joined);
newMemberCount = newMemberCount + 1;
}
});

while (raidmode == false) {
setInterval(function{
if (newMemberCount >= 10) {
raidmode = true;
} else {
newMemberCount = 0;
}
}, 10000)
}
if (raidmode == true) {
setTimeout(function(){
raidmode = false;
}, 30000);
}
smth like this ?
SpecialSauce
SpecialSauce2y ago
I’m not sure how you could get the count of members that join within a 10 second time frame without creating a 10 second timeout every time a member joins. At that point it gets really messy trying to keep track of each timeout separately.
if(raidmode)
//kick members
else
//slice array -9
//compare array[0] to current guildjoin < 10000
//if it’s < , raidmode = true, cancel last 30s timeout and start new one
if(raidmode)
//kick members
else
//slice array -9
//compare array[0] to current guildjoin < 10000
//if it’s < , raidmode = true, cancel last 30s timeout and start new one
The last if statement won’t do anything. It’s only going to fire once The interval might work though.
'NoXz
'NoXz2y ago
so how could I do the last statement work put it in a while ? tbh if i can make this work without having to compare an array i'll be happy xD
SpecialSauce
SpecialSauce2y ago
That while loop though will end up creating a ton of intervals
'NoXz
'NoXz2y ago
yeah bot doesnt start
SpecialSauce
SpecialSauce2y ago
The way it is now the while loop would execute forever
'NoXz
'NoXz2y ago
js heap out of memory
SpecialSauce
SpecialSauce2y ago
Yeah it created intervals until crash
'NoXz
'NoXz2y ago
ye
SpecialSauce
SpecialSauce2y ago
No loops.
'NoXz
'NoXz2y ago
So what statement should I use ?
SpecialSauce
SpecialSauce2y ago
If you reallllly want an interval just leave it without the loop
'NoXz
'NoXz2y ago
its not that i want to do an interval its that i dont want to do an array if possible xD
setInterval(function(){
if (newMemberCount >= 10) {
raidmode = true;
console.log("Raidmode activated !");
setTimeout(function(){
raidmode = false;
console.log("Raidmode automatically disabled after 30 seconds !");
}, 30000);
} else {
newMemberCount = 0;
}
}, 10000);
setInterval(function(){
if (newMemberCount >= 10) {
raidmode = true;
console.log("Raidmode activated !");
setTimeout(function(){
raidmode = false;
console.log("Raidmode automatically disabled after 30 seconds !");
}, 30000);
} else {
newMemberCount = 0;
}
}, 10000);
for the timeout i should put it in a loop, no ? while raidmode = true it will exit the loop in 30 s
SpecialSauce
SpecialSauce2y ago
Well think about when you want to start the 30s timer.
'NoXz
'NoXz2y ago
Oh i should put it under raidmode = true, right ? Like this ?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
'NoXz
'NoXz2y ago
sorry i'm new to discord js could you elaborate your point please ? Guess I'll just have to do an array Sadge you do the countdown ? nvm i did it
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
'NoXz
'NoXz2y ago
kinda, only did 2 years of web dev
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
'NoXz
'NoXz2y ago
this event ? client.on("guildMemberAdd", yes im new to node