2013-03-11 5 views
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 

내가 뭘 잘못하고 있는지 알 수 없다. 모든 것이 잘된 것처럼 보입니다. 도와 주셔서 감사합니다!

추신 : 나는 나쁜 영어 때문에 미안해!

답변

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) 와만 일치 시키십시오.

catch-all except:을 사용해야하는 유일한 경우는 모든 예외를 무시해야하거나 예외를 전파하기 위해 raise을 호출하기 전에 정리를해야하는 경우입니다.


어쨌든, 그게 내가 알아낼 수있는 것입니다. logging 모듈을보고 logging.debug() 호출을 여러 가지 방법으로 호출하여 코드가 수행하는 단계와 다양한 액세스 및 순위에 대한 값을 확인할 수 있습니다.

+0

나는 whole_body [2]에서 if "-"를 대체 할 때까지 작동하지 않았다. 두 목록의 사용자 이름 회원 목록, 회원 목록 및 블랙리스트에 사용자를 배치하라는 명령이 필요합니다. – user2150482