放入不和谐命令时,计算计数代码功能异常 [英] Value tallying code dysfunctions when put into discord command

查看:23
本文介绍了放入不和谐命令时,计算计数代码功能异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚对我的机器人进行了编码,基本上它总结了过去7天每个用户的每日行会XP,并将其以列表的形式打印到Google工作表中.有一个问题,那就是手动添加时列表中的Guild XP值错误,这是API JSON中所有7天的公会XP.

I just coded my bot and basically it sums every user's daily guild XP from the last seven days and prints it in a list into a google sheet. There is one problem and it is that the guild XP values in the list are wrong when manually adding up, which is the guild XP from all seven days from the API JSON.

它打印的名称是正确的,但是值是错误的,我无法确切知道它们的名称.仅当以下代码在discord命令中运行时才会发生这种情况,但是例如在PyCharm中运行时,该代码可以正常运行.

The names it prints are right, but the values are wrong, and I can't tell exactly how they are. This only happens when the code below is run in a discord command, but the code functions properly when ran in PyCharm for example.

我不确定发生了什么.我的bot.py是在Ubuntu 18.04中与tmux一起运行的.我知道这不是Google Sheets,因为我在使用Google Sheets之前就打印了列表,而且它也不是API.

I am not sure what is going on. My bot.py is run with tmux in Ubuntu 18.04. I know that it's not a google sheets thing because I printed the list before I used google sheets, and it's not the API either.

我需要重新排列我的代码吗?有for循环吗?

Do I need to rearrange my code? Something with for loops?

for count in range(len(data['guild']['members'])): # For every guild member:
    res = requests.get("https://playerdb.co/api/player/minecraft/" + data['guild']['members'][count]['uuid']) # Response from server

    if res.status_code != 200: # If it wasn't a success, continue the loop and print a message
        ctx.author.send("Error 1: Invaild name from player database API!")
        continue

    name = res.json()['data']['player']['username'] # We know it was successful if we got to this point, so it's safe to try and get data from our response
    names.append(name)

# Members' GXP
    xp = data['guild']['members'][count]['expHistory']
    xp = sum(xp.values())
    xpHist.append(xp)

# Weekly Total
wTotal = sum(xpHist)
print(xpHist)

https://pastebin.com/ijViVZn7

JSON: https://pastebin.com/FaDESA5i

推荐答案

使用枚举a>将为您提供索引和元素本身.使用字典更容易将xp(值)与名称(键)相关联.

Using enumerate which will give you the index and the element itself. Using a dictionary is easier to relate the xp (value) to the name (key).

scores = {}
# For every guild member:
for count, member in enumerate(data['guild']['members']):
    res = requests.get("https://playerdb.co/api/player/minecraft/" +
                       data['guild']['members'][count]['uuid'])  # Response from server

    if res.status_code != 200:  # If it wasn't a success, continue the loop and print a message
        ctx.author.send("Error 1: Invaild name from player database API!")
        continue

    # We know it was successful if we got to this point, so it's safe to try and get data from our response
    name = res.json()['data']['player']['username']
    # Members' GXP
    xp = data['guild']['members'][count]['expHistory']

    # to record scroe with the name using dict
    scores[name] = sum(xp.values())

# Weekly Total
wTotal = sum(scores.values())
print(wTotal)
print(scores)

这篇关于放入不和谐命令时,计算计数代码功能异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆