-3

I know how to use the time.sleep() command to create a delay in a script, but I'm developing a chatango bot and I want to prevent it from flooding the chat by putting a 3 second delay after each command. However, I'm not sure how to make that happen exactly, and my current attempt just makes the script itself delay for 3 seconds. This is what I have as an example:

    s = message.body
    if 'test' in s:
        print(room.message("This is a sample."))
        import time
        time.sleep(3)

I'm not quite sure what to do from here, so any help would be appreciated, thank you.

NOTE: This is NOT the same question as "How can I make a time delay in Python?" No answers on that question helped me in any way, it's a different thing I'm asking for.

NOTE AGAIN: People keep implying it's the same question as another one and IT IS NOT. It is a DIFFERENT QUESTION. Why is that so hard for you people to grasp?

  • possible duplicate of [How can I make a time delay in Python?](http://stackoverflow.com/questions/510348/how-can-i-make-a-time-delay-in-python) – itzmebibin Apr 10 '15 at 04:57
  • It's not a duplicate. My question is asking how to make the delay happen after it executes a command, not delaying the command itself. – ArtemisBasker Apr 10 '15 at 04:58

3 Answers3

1

Save time of the last reply into instance variable inside of onMessage(). Renew it each time you print message into the room. Then only reply if :

import time
class Bot(ch.RoomManager):
    def onMessage(self, room, user, message):
        NOW = time.time()
        s = message.body
        if 'test' in s AND NOW - 3 > self._last_sent:
            print(room.message("This is a sample."))
            self._last_sent = NOW
fukanchik
  • 2,811
  • 24
  • 29
  • For some reason my script keeps crashing no matter what I do with this code, then it won't stop crashing until I remove everything, including "import time". – ArtemisBasker Apr 10 '15 at 05:26
  • I finally got it to where it doesn't crash, but now it simply doesn't work. It just makes the command invalid. I don't know if I'm doing something wrong or what, but I made sure there were no syntax errors. – ArtemisBasker Apr 10 '15 at 05:49
0

This is a method i use for a game command on my bot to make a time-delay on a command.

import time
time_dict = dict()
class Bot(ch.RoomManager)
      def onMessage(self, room, user, message):  
          s = message.body
          if 'test' in s:   
            try:
             if room in time_dict:
                timeset = time_dict[room]
             else:
                timeset = time.time()-3
                time_dict[room] = timeset
                if time.time() - float(timeset) < 150:
                   return
                else:
                    room.message("This is a sample.")
0

Try this:

import ch
import time

z = dict()

class bot(ch.RoomManager):
    def onMessage(self, room, user, message):
        s = message.body
        if 'test' in s:
            if room.name in z:
                if time.time() < z[room.name]:
                    return
                else:
                    z[room.name] = time.time()+3
                    print(room.message("This is a sample."))
            else:
                z[room.name] = time.time()+3
                print(room.message("This is a sample."))

if __name__ == "__main__":
    bot.easy_start()
Kakkoiikun
  • 41
  • 6
  • The content of the `else` of the `if time.time() < z[room.name]` is the same as the content of the `else` of the `if room.name in z` – Kakkoiikun May 26 '18 at 09:21