开发者

How can I conduct a poll through an irc bot?

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开发者_StackOverflow中文版 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜