Estoy tratando de hacer un bot de bloqueo, y ya he hecho la mayor parte, excepto el comando final. Estoy tratando de hacer que finalice la función client.on(). Cualquier ayuda sería apreciada. Aquí está el código que tengo hasta ahora:
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. } }
Aunque el método de BigPouley funcionaría, hay una manera más eficiente. Si llama a EventEmitter#removeListener
una vez que termine el bloqueo, dejará de escuchar Client#guildMemberAdd
por completo.
Si pones en perspectiva cuánto tiempo crees que vas a tener un servidor bloqueado frente a no, habrá muchos miembros del gremio que pasarán por el evento y luego serán rechazados porque no hay un bloqueo actual.
Eso también equivale a mucha memoria desperdiciada, lo que obviamente no quieres. Podría funcionar un poco así:
// 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); } } });
Debe separarlo y usar una verificación simple de verdadero/falso.
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'); } });