提升水平

时间:2013-03-11 13:26:19

标签: python

我有一个chatango机器人,它具有升级和降级功能,以便使用某些命令。但是,一旦我宣传某人,它也会将它们添加到黑名单(级别-1),例如。如果我使用了promote命令并且person为0级,它会将它们添加到成员列表中并使他/她成为1级,但由于它还将它们添加到黑名单中,因此它们也会将它们设置为-1。因此,如果我重新启动机器人并尝试推广某人,它就会搞砸了。

这是代码。

        # Command Usage: 'promote <username>, 'promote <username> <rank>
    elif used_prefix and self.getAccess(user.name.lower()) >= lvl_config.rank_req_min_promote and cmd == "promote" and len(args) > 3:
        whole_body = message.body.split(" ", 3)
        promote_username = whole_body[1].lower()
        promote_username = promote_username.strip()
        cur_rank = self.getRank(promote_username)
        try:
            next_rank = 0
            if "-" in whole_body[2]:
                next_rank = 0 - int(whole_body[2])
            else:
                next_rank = int(whole_body[2])

            user_level = self.getAccess(promote_username)
            if user_level == lvl_config.rank_lvl_botowner:
                next_rank = user_level
            elif user_level != -1:
                next_rank = next_rank
            else:
                if self.getAccess(user.name.lower()) >= lvl_config.rank_req_blacklist_rem:
                    next_rank = next_rank
                else:
                    next_rank = user_level
        except:
            user_level = 0
            next_rank = 0

            user_level = self.getAccess(promote_username)
            if user_level == lvl_config.rank_lvl_botowner:
                next_rank = user_level
            elif user_level != -1:
                next_rank = user_level + 1
            else:
                if self.getAccess(user.name.lower()) >= lvl_config.rank_req_blacklist_rem:
                    next_rank = user_level + 1
                else:
                    next_rank = user_level

        if next_rank > len(ranks) - 1 and self.getAccess(user.name.lower()) >= lvl_config.rank_lvl_botowner and next_rank > self.getAccess(promote_username):
            chat_message("<font color='#%s' face='%s' size='%s'>Sorry <b>%s</b>, I couldn't promote <b>%s</b> as they are already one of my <b>%s (%s)</b>.</font>" % (font_color, font_face, font_size, self.getAlias(user.name), self.getAlias(promote_username), self.getRank(promote_username), self.getAccess(promote_username)), True)
        elif next_rank <= len(ranks) - 1 and next_rank >= 0 and self.getAccess(user.name.lower()) >= self.getAccess(promote_username) + lvl_config.rank_req_modifier_promote and next_rank > self.getAccess(promote_username):
            print("%s,%s" % (str(promote_username).strip(), str(user_level).strip()))
            try:
                users.remove("%s,%s" % (str(promote_username).strip(), str(user_level).strip()))
            except Exception as e:
                print(e)
            if next_rank != 0:
                users.append("%s,%s" % (str(promote_username).strip(), str(next_rank).strip()))
            try:
                chat_message("<font color='#%s' face='%s' size='%s'><b>%s</b> has been promoted from <b>%s (%s)</b> and is now <b>%s (%s)</b>.</font>" % (font_color, font_face, font_size, self.getAlias(promote_username), cur_rank, user_level, self.getRank(promote_username), self.getAccess(promote_username)), True)
            except Exception as e:
                print(e)
        elif next_rank == -1 and self.getAccess(user.name.lower()) < lvl_config.rank_req_blacklist_rem and next_rank >= self.getAccess(promote_username):
            chat_message("<font color='#%s' face='%s' size='%s'><b>%s</b> is on the blacklist and cannot be removed by you. Contact a person with level %s or higher access.</font>" % (font_color, font_face, font_size, self.getAlias(promote_username), lvl_config.rank_req_blacklist_rem), True)
        elif next_rank == self.getAccess(promote_username):
            chat_message("<font color='#%s' face='%s' size='%s'><b>%s</b> is already a rank of %s (%s).</font>" % (font_color, font_face, font_size, self.getAlias(promote_username), self.getRank(promote_username), self.getAccess(promote_username)), True)
        # Save all the data now.
        self.savRanks()
    # End Command

我无法弄清楚我做错了什么,一切对我来说都很好。谢谢你的帮助!

PS:我是荷兰人,对不好的英语感到抱歉!

1 个答案:

答案 0 :(得分:0)

whole_body = message.body.split(" ", 3)

这会在个别空间上分裂。如果要按单词分割(即单词之间的任何空格),请使用:

whole_body = message.body.split(None, 3)

    promote_username = whole_body[1].lower()
    promote_username = promote_username.strip()

只需写下:

    promote_username = whole_body[1].lower().strip()

if "-" in whole_body[2]:
    next_rank = 0 - int(whole_body[2])
else:
    next_rank = int(whole_body[2])

这可能会给你一个双重否定,因为int可能是否定的。例如,如果whole_body[2]-50,那么next_rank = 0 - int(whole_body[2]) = 0 - int("-50") = 0 - -50 = 50您可能只想:

next_rank = int(whole_body[2])

next_rank = next_rank

这没有任何作用。它为什么存在?


您的try区块过度了。它正在抛弃实际的异常。在try块中添加较少的语句,并使except部分仅匹配某些例外(例如ValueError)。

您应该使用全部except:的唯一情况是您需要丢弃所有异常,或者在调用raise传播之前只需要进行一些清理时例外。


无论如何,这就是我能想到的。您可能需要查看logging模块,并在此处将logging.debug()次调用放在各个点,以查看代码所经历的步骤以及为各种访问和排名获得的值。