0

特定のコマンドを使用するために、レベルの昇格および降格機能を備えたchatangoボットがあります。しかし、誰かを昇格させると、たとえば、ブラックリスト (レベル -1) にも追加されます。私が昇格コマンドを使用し、その人がレベル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: 私はオランダ人なので、下手な英語で申し訳ありません!

4

1 に答える 1

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]isの場合、次のようにします。-50next_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()ここのさまざまなポイントに呼び出しを配置し​​て、コードが通過するステップと、さまざまなアクセスとランクに対して取得する値を確認することをお勧めします。

于 2013-03-11T13:45:26.493 に答える