How can I poll through the irc bot?

I installed the irc bot using a socket. I added a few commands, but would like to add a polling function. Ideally, the bot would receive a command with this format:

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

How can I check the user who voted and completed the survey after a certain time?

Thanks in advance,

A desperate beginner of Python.

EDIT: Thanks so much for the guys answers, I went using global wars (I know, I know) because I couldn't figure out how to do it otherwise. Again, thank you very much!

+4
source share
1 answer

Well, I'm starting to laugh a little with my Python, but I think I can answer this question. This may not be the best answer.

If you plan to run many polls at the same time, you can implement a dictionary containing multiple instances of a custom class, such as Poll. Here you can find the 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 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 will need to edit the poll class in accordance with your needs, that is, allow several values ​​when voting, record who votes what (if you want it), etc.

As for checking the completion of the survey, you can edit the poll class to have a stop time, and have a function that returns True / False, whether this time has passed or not. Perhaps look at the docs for the datetime module ...?

Anyway, hope this helps.

+1
source

Source: https://habr.com/ru/post/1345592/


All Articles