4

I've set up a irc bot using socket. I've added a few commands , but I'd like to add a "poll" function. Ideally, the bot would get a command with this format:

!poll <name> <opt1> <opt2> <opt3> <time>

How would I go about checking user who voted and ending the poll after a certain time?

Thanks in advance,

Desperate Python Beginner.

EDIT: Thanks a lot for the responses guys, I went with using global vars ( I know, I know ) because I couldn't figure out how to do it otherwise. Again, thanks a lot!

Undo
  • 25,519
  • 37
  • 106
  • 129
  • This looks like two questions: 1) How do I make an IRC bot with custom commands 2) How do I design a software poll – Daenyth Mar 28 '11 at 18:54
  • 2
    What exactly are you having trouble with? Also, what format is the data coming in? – Philip Mar 31 '11 at 03:30
  • @Daenyth: I've created quite a few custom commands , it was only this one poll system I couldn't pin down. –  Apr 02 '11 at 17:30
  • @Philip: It's a string, and I had trouble with organising the number of votes and retrieving them, as well as ensuring no-one voted twice. –  Apr 02 '11 at 17:31
  • If you've solved your problem, please post an answer and accept it (you may accept your own answers) it will mark the question as closed. – Madara's Ghost Apr 27 '12 at 20:55
  • Please don't vandalize your question. – Undo Apr 29 '16 at 13:57

1 Answers1

1

Well, I'm starting to get a little rusty with my Python but I think I can answer that - It may not be the best answer, though.

If you plan to have many polls going at once, you could implement a dictionary containing multiple instances of a custom class like Poll.. Here's a possible solution:

class PollVotes(object):
    def __init__(self):
        self.votes = []
        self.stoptime = "some date/time" #I can't remember how to do this bit ;)

    def add_vote(self, vote_value): 
        self.votes.append(vote_value);

    def tally_votes(self):
        return self.votes.size()

    def has_closed(self):
        if time_now >= self.stoptime: # I forget how you'd do this exactly, but it's for sake of example
            return True
        else:
            return False

#then use it something like this
poll_list = {}
#irc processing...
if got_vote_command:
    if poll_list["channel_or_poll_name"].has_ended(): 
        send("You can no longer vote.")
    else:
        poll_list["channel_or_poll_name"].add_vote(persons_vote)
        #send the tally
        send("%d people have now voted!" % poll_list["channel_or_poll_name"].tally_votes())

Of course, you'd have to edit the poll class to fit your needs, i.e. to allow multiple values in a vote, to record who is voting what (if you want that), etc.

As for checking if the poll has ended, you could edit the poll class to have the stop time, and have a function that returns True/False whether that time has passed or not. Possibly look at the docs for the datetime module...?

Anyway, hope this helps.

FurryHead
  • 1,479
  • 3
  • 16
  • 19
  • Your `has_closed` can just be `return time_now >= self.stoptime`. There's no need for the if statement :) – Daenyth Mar 31 '11 at 19:43