japanballdev
japanballdev

Reputation: 50

End client.on function

I am trying to make a lockdown bot, and I've already got most of it done, except the end command. I am trying to make that end the client.on() function. Any help would be appreiciated. Here's the code I have so far:

if (command == "lockdown") {
    if (!args[0]) return message.channel.send("Please type start or end.");

    if (args[0] === "start") {
       client.on('guildMemberAdd', (member) => {
          member.kick();
          console.log('kicked');
       })

     }

     if (args[0] === "end") {
      //i don't know what to put here.
    }
}

Upvotes: 3

Views: 264

Answers (2)

Lioness100
Lioness100

Reputation: 8402

Although BigPouley's method would work, there's a more efficient way. If you call EventEmitter#removeListener once the lockdown is over, it will completely stop listening to Client#guildMemberAdd.

If you put into perspective how much time you think you're going to have a server on lockdown vs not, there's going to be a lot of guildmembers going through the event and then getting rejected back out because there's no current lockdown.

That also equates to a lot wasted memory, which you obviously don't want. It could work a bit like this:

// before your `client.on('message')` event,
// you should declare the function to trigger
// when someone joins during lockdown
const lockdown = (member) => {
 member.kick();
 console.log('kicked');
};
// this is because to unsubscribe to an event,
// you need the origin function it was listening to

client.on('message', (message) => {
 // bla bla bla command handling...

 if (command == 'lockdown') {
  if (!args[0]) return message.channel.send('Please type start or end.');

  if (args[0] === 'start') {
   // you don't want to subscribe to an event twice,
   // so make sure there isn't a lockdown already in progress
   if (client.eventNames.includes('guildMemberAdd')) {
    // this server is already locked down!
   }

   // use the function you made
   client.on('guildMemberAdd', lockdown);
  }

  if (args[0] === 'end') {
   // now do the reverse and check if there
   // *isn't* a lockdown in progress
   if (!client.eventNames.includes('guildMemberAdd')) {
    // this server isn't locked down!
   }

   // now enter the *same* function to unsubscribe
   client.removeListener('guildMemberAdd', lockdown);
  }
 }
});

Upvotes: 2

BigPouley
BigPouley

Reputation: 91

You should separate it and use a simple true/false check.

let lockdown = false;

client.on('message', message => {
    let command = ...;
    let args = ...;

    if (command == "lockdown") {
        if (!args[0]) return message.channel.send("Please type start or end.");

        if (args[0] === "start") {
            lockdown = true;
        }

        if (args[0] === "end") {
            lockdown = false;
        }
    }
});

client.on('guildMemberAdd', (member) => {
    if(lockdown) {
        member.kick();
        console.log('kicked');
    }
});

Upvotes: 1

Related Questions