0

I have an IRC bot that I made for automating stuff.

Here's a snippet of it:

def analyseIRCText(connection, event):
    global adminList, userList, commandPat, flood
    userName = extractUserName(event.source())
    userCommand = event.arguments()[0]
    escapedChannel = cleanUserCommand(config.channel).replace('\\.', '\\\\.')
    escapedUserCommand = cleanUserCommand(event.arguments()[0])
    #print userName, userCommand, escapedChannel, escapedUserCommand

    if flood.has_key(userName):
        flood[userName] += 1
    else:
        flood[userName] = 1
    ... (if flood[userName] > certain number do...)

So the idea is that flood thing is a dictionary where a list of users who have entered in a command to the bot in the recent... some time is kept, and how many times they've said so and so within that time period.

Here's where I run into trouble. There has to be SOMETHING that resets this dictionary so that the users can say stuff every once in awhile, no? I think that a little thing like this would do the trick.

def floodClear():
    global flood
    while 1:
        flood = {} # Clear the list
        time.sleep(4) 

But what would be the best way to do this? At the end of the program, I have a little line called:

thread.start_new_thread(floodClear,())

so that this thing doesn't get called at gets stuck in an infinite loop that halts everything else. Would this be a good solution or is there something better that I could do?

keyser
  • 18,829
  • 16
  • 59
  • 101
Joe
  • 1,378
  • 5
  • 20
  • 32
  • You should really refactor `analyseIRCText` as a class with `adminList`, `userList`, `commandPat` and `flood` as instance attributes. Global variables are usually a [bad idea](http://stackoverflow.com/questions/146557/do-you-use-the-global-statement-in-python). – Lauritz V. Thaulow Jun 02 '13 at 21:48

1 Answers1

1

Your logic should be enough. If you have say:

    if flood.has_key(userName):
        flood[userName] += 1
    else:
        flood[userName] = 1
    if flood[userName] > say 8:
        return 0

That should make your bot ignore the user if he has spammed too many times within your given time period. What you have there should also work to clear up your flood dictionary.

  • Ehh... I already had that figured out. I guess I'll accept it since yours is the only answer and it's technically not wrong... – Joe Jun 15 '13 at 01:51