加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 如何通过irc bot进行投票?

发布时间:2020-12-20 13:22:39 所属栏目:Python 来源:网络整理
导读:我已经使用套接字设置了一个irc机器人.我添加了一些命令,但我想添加一个“poll”功能. 理想情况下,机器人会获得这种格式的命令: !poll name OPT1 OPT2 OPT3 时间 如何在一段时间后检查投票和结束投票的用户? 提前致谢, 绝望的Python初学者. 编辑:非常感
我已经使用套接字设置了一个irc机器人.我添加了一些命令,但我想添加一个“poll”功能.
理想情况下,机器人会获得这种格式的命令:

!poll< name> < OPT1> < OPT2> < OPT3> <时间>

如何在一段时间后检查投票和结束投票的用户?

提前致谢,

绝望的Python初学者.

编辑:非常感谢回复人员,我使用全球变量(我知道,我知道),因为我无法弄清楚如何做到这一点.再次,非常感谢!

解决方法

好吧,我的Python开始变得有些生疏,但我想我可以回答一下 – 但这可能不是最好的答案.

如果您计划一次进行多次民意调查,您可以实现一个包含Poll等自定义类的多个实例的字典.这是一个可能的解决方案:

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())

当然,您必须编辑轮询类以满足您的需求,即允许投票中的多个值,以记录投票的人(如果您想要的话)等.

至于检查轮询是否已经结束,您可以编辑轮询类以具有停止时间,并且具有返回True / False的函数,无论该时间是否已经过去.可能会看一下datetime模块的文档……?

无论如何,希望这会有所帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读