0

I'm making something for a discord bot and i cant figure out this part...

Discord a chat application where you can make servers and channels for people to talk in, there are also bots and that's what i'm making.

The basic idea is i write a command and the bot denies everyone the permission to talk in the chat, the count down starts and when the count down reaches 0 the bot allows talking again, yes this is a JoJo reference. I have all the allowing and denying ready, just need this countdown to work.

My problem is i cant make my code wait for itself.

//some other code

    console.log("Toki uo tomare!")

    var x = 0;
    function  go() {
        console.log(x)
        if (x++ < 5) {
            setTimeout(go, 500);
        }
    }
    go();

    console.log("Toki wa ugokidasu...")

//some other code

I expected to see

Toki uo tomare! 0 1 2 3 4 5 Toki wa ugokidasu...

But I saw this instead

Toki uo tomare! 0 Toki wa ugokidasu... 1 2 3 4 5

GamBar
  • 38
  • 7
  • Simply add `else { ... }` to your `go()` function and put there the last `console.log` line. So that when the function calls itself and see `x == 5`, then it will go to `else` block and log that message. – Andrew Dashkov Nov 09 '19 at 22:24
  • Other linked answers, or @Andrew's comment above, will give you clues on how to do this properly. I'll try to explain what's happening here: The code *is* running in order. First the first line. Then there's a function definition, which does nothing in itself. Then the function is called, and runs once (it also queues itself up to run again later). And then the last line runs. 500 microseconds later, the fxn runs again, as it was told to. – TRiG Nov 09 '19 at 22:32
  • @GamBar Happy to help :) – Andrew Dashkov Nov 10 '19 at 01:31

1 Answers1

0

const TalkingDuration=5000;
let canTalk=true;

window.botInterval=setTimeout(function(){
  canTalk=false;
}, TalkingDuration);

setInterval(function(){
  if(canTalk) console.log("Toki uo tomare!");
}, 100);

//So the clients can talk for 5s and then the bot will stop the chat you can use a loop to redo the task each 5s just by adding another setInterval ... Hope you like it
Adnane Ar
  • 683
  • 7
  • 11
  • Your code just spams `Toki uo tomare!` while i need it to be a count down of sorts, hence why i make it log X – GamBar Nov 09 '19 at 22:46