SkyDynamic / nonebot_plugin_hypixel

一个用于查询hypixel数据的bot插件
GNU General Public License v3.0
6 stars 1 forks source link

查询起床战争时如果数据为0会报错 #2

Open ABKQPO opened 1 year ago

ABKQPO commented 1 year ago

建议将api_handle.py更改为以下代码 `import time from .request import HypixelAPICallError

class HypixelInformationHandle(): def init(self, data: dict):

初始化数据

    online = data.get('online')
    data = data.get('player')
    '---基本数据---'
    #是否在线
    if online == True:
        self.online = '在线'
    else:
        self.online = '离线'
    #最后登陆的时间
    if data.get('lastLogin'):
        time_array = time.localtime(int(data.get('lastLogin')/1000))
        self.last_login = time.strftime("%Y-%m-%d %H:%M:%S", time_array)
    else:
        self.last_login = '对方隐藏了最后的上线时间'
    #Rank获取
    rank_id = data.get('newPackageRank')
    if rank_id == None:
        self.Rank = ''
    elif rank_id:
        if rank_id == 'VIP' or rank_id == 'MVP':
            self.Rank = f'[{rank_id}]'
        elif rank_id == 'VIP_PLUS' or rank_id == 'MVP_PLUS':
            self.Rank = f'[{str(rank_id).replace("_PLUS", "+")}]'
    #等级
    xp = data.get('networkExp')
    self.level = self.Get_Hypixel_Level(int(xp))
    '---小游戏元数据---'
    stats_data = dict(data.get('stats'))
    if stats_data:
        '---起床战争数据---'
        bedwars_data = stats_data.get('Bedwars')
        self.bw_data_status = 'failed'
        if bedwars_data:
            self.bw_data_status = 'success'
            #基本信息
            self.Get_Hypixel_Bedwars_Level(int(bedwars_data.get('Experience')))#等级
            self.bw_coin = bedwars_data.get('coins')#硬币
            self.winstreak = bedwars_data.get('winstreak')#连胜
            #床
            self.break_bed = bedwars_data.get('beds_broken_bedwars', 0) # 破坏床数
            self.lost_bed = bedwars_data.get('beds_lost_bedwars', 0) # 被破坏床数
            if self.lost_bed != 0 and self.break_bed is not None:
                self.BBLR = round(self.break_bed / self.lost_bed, 3) # 破坏床数和被破坏床数的比
            else:
                self.BBLR = 0
            #胜败
            self.bw_win = bedwars_data.get('wins_bedwars')#胜利
            self.bw_losses = bedwars_data.get('losses_bedwars')#失败
            self.W_L = round(self.bw_win / self.bw_losses, 3)#胜利和失败的比
            #普通击杀/死亡
            self.bw_kill = bedwars_data.get('kills_bedwars')#击杀
            self.bw_death = bedwars_data.get('deaths_bedwars')#死亡
            self.K_D = round(self.bw_kill / self.bw_death, 3)#KD值
            #最终击杀/死亡
            self.bw_final_kill = bedwars_data.get('final_kills_bedwars', 0) # 最终击杀
            self.bw_final_death = bedwars_data.get('final_deaths_bedwars', 0) # 最终死亡
            if self.bw_final_death != 0:
                  self.FKDR = round(self.bw_final_kill / self.bw_final_death, 3) # 最终KD值
            else:
                self.FKDR = 0
            #矿物收集
            self.bw_iron = bedwars_data.get('iron_resources_collected_bedwars') if bedwars_data.get('iron_resources_collected_bedwars') else 0 #铁锭收集
            self.bw_gold = bedwars_data.get('gold_resources_collected_bedwars') if bedwars_data.get('gold_resources_collected_bedwars') else 0 #金锭收集
            self.bw_diamond = bedwars_data.get('diamond_resources_collected_bedwars') if bedwars_data.get('diamond_resources_collected_bedwars') else 0 #钻石收集
            self.bw_emerald = bedwars_data.get('emerald_resources_collected_bedwars') if bedwars_data.get('emerald_resources_collected_bedwars') else 0 #绿宝石收集
    else:
        raise HypixelAPICallError('玩家数据不存在')

def Get_Hypixel_Level(self, xp: int) -> int:
    '''大厅等级算法'''
    prefix = -3.5
    const = 12.25
    divides = 0.0008
    return int((divides*xp+const)**0.5+prefix+1)

def Get_Hypixel_Bedwars_Level(self, Exp: int) -> int:
    '''起床等级算法'''
    if Exp < 500:
        level = '0✫'
        experience = str(Exp) + '/500'
    elif Exp >= 500 and Exp < 1500:
        level = '1✫'
        experience = str(Exp-500) + '/1k'
    elif Exp >= 1500 and Exp < 3500:
        level = '2✫'
        experience = str(Exp-1500) + '/2k'
    elif Exp >= 3500 and Exp < 7000:
        level = '3✫'
        experience = str(Exp-3500) + '/3.5k'
    elif Exp >= 7000:
        if Exp < 487000:
            add_level = int((Exp-7000) / 5000)
            level = str(4+add_level) + '✫'
            experience = str(Exp-7000-add_level*5000) + '/5k'
        if Exp >= 487000:
            surplus_experience = Exp - (int(Exp / 487000)) * 487000
            if surplus_experience < 500:
                add_level = 0
                experience = str(surplus_experience) + '/500'
            elif surplus_experience >= 500 and surplus_experience < 1500:
                add_level = 1
                experience = str(surplus_experience-500) + '/1k'
            elif surplus_experience >= 1500 and surplus_experience < 3500:
                add_level = 2
                experience = str(surplus_experience-1500) + '/2k'
            elif surplus_experience >= 3500 and surplus_experience < 7000:
                add_level = 3
                experience = str(surplus_experience-3500) + '3.5k'
            elif surplus_experience >= 7000:
                add_level = int((surplus_experience-7000) / 5000)
                experience = str(surplus_experience-7000-add_level*5000)
            level = str((int(Exp/487000))*100+ 4 + add_level) + '✫'
    self.bw_level = level
    self.bw_experience = experience`

将_init.py更改为以下代码 `from nonebot import on_command from nonebot.adapters.onebot.v11 import GROUP from nonebot.adapters.onebot.v11.event import GroupMessageEvent from nonebot.adapters.onebot.v11.message import Message, MessageSegment from nonebot.params import CommandArg

from .config import api_key from .request import PlayerNameNotFound, HypixelAPICallError, player_data from .api_handle import HypixelInformationHandle as HIH

Hypixel = oncommand('hypixel', permission=GROUP, aliases={'hyp'}, priority=0, block=True) @Hypixel.handle() async def (event: GroupMessageEvent, arg: Message = CommandArg()): '''/hypixel (ID) [bw]''' args = str(arg).split() reply = MessageSegment.reply(event.message_id) if api_key not in [None, 'API_KEY']: if len(args) >= 1: try: Original_data = await player_data(args[0], api_key) data = HIH(Original_data) if len(args) == 1: msg = f'{data.Rank} {args[0]} 的Hypixel大厅信息:\n在线情况: {data.online} | Hypixel大厅等级: {data.level}\n最后登录时间: {data.last_login}\n起床战争数据查询:/hyp {args[0]} bw' if len(args) == 2: if args[1] in ['bw','bedwars','起床']: if data.bw_data_status == 'success': msg = '\n'.join( [ f"[{data.bw_level if data.bw_level is not None else 'N/A'}] {data.Rank if data.Rank is not None else 'N/A'} {args[0]} 的起床战争数据:", f"经验: {data.bw_experience if data.bw_experience is not None else 'N/A'} | 硬币: {format(data.bw_coin if data.bw_coin is not None else 0, ',d')} | 连胜: {format(data.winstreak if data.winstreak is not None else 0, ',d')}", f"拆床: {format(data.break_bed if data.break_bed is not None else 0, ',d')} | 被拆床: {format(data.lost_bed if data.lost_bed is not None else 0, ',d')} | BBLR: {data.BBLR if data.BBLR is not None else 'N/A'}", f"胜场: {format(data.bw_win if data.bw_win is not None else 0, ',d')} | 败场: {format(data.bw_losses if data.bw_losses is not None else 0, ',d')} | W/L: {data.W_L if data.W_L is not None else 'N/A'}", f"击杀: {format(data.bw_kill if data.bw_kill is not None else 0, ',d')} | 死亡: {format(data.bw_death if data.bw_death is not None else 0, ',d')} | K/D: {data.K_D if data.K_D is not None else 'N/A'}", f"终杀: {format(data.bw_final_kill if data.bw_final_kill is not None else 0, ',d')} | 终死: {format(data.bw_final_death if data.bw_final_death is not None else 0, ',d')} | FKDR: {data.FKDR if data.FKDR is not None else 'N/A'}", f"收集铁锭: {format(data.bw_iron if data.bw_iron is not None else 0, ',d')} | 收集金锭: {format(data.bw_gold if data.bw_gold is not None else 0, ',d')}", f"收集钻石: {format(data.bw_diamond if data.bw_diamond is not None else 0, ',d')} | 收集绿宝石: {format(data.bw_emerald if data.bw_emerald is not None else 0, ',d')}" ] ) else: await Hypixel.finish(reply + '此玩家的起床战争数据不存在') await Hypixel.finish(reply + msg) except (PlayerNameNotFound, HypixelAPICallError) as e: await Hypixel.finish(reply + str(e)) else: await Hypixel.finish(reply + '缺少必要参数!') else: await Hypixel.finish(reply + '请填写API密钥\n获取方式:\n在Hypixel服务器中输入指令:/api')`

SkyDynamic commented 1 year ago

您fork项目然后提pr吧,这样看眼睛回瞎的,这个项目我很久没维护了,没时间维护(