【问题标题】:Discord.py get_user KeyError(key)Discord.py get_user KeyError(key)
【发布时间】:2021-04-28 09:37:00
【问题描述】:

这是我第一次做这些,对我来说太赤裸裸了。

我目前面临一个问题,使用 replit 的数据库(我很确定这是一本字典),我正在尝试创建一个生日循环,但我得到了这个 KeyError。

in get_raw raise KeyError(key) keyErorr: 310439746247589889

这来自的 KeyError 来自我要拆分的列表。是用空格分隔的两个数字,或者在这种情况下:

"bd043010" : "690738010903281734 310439746247589889"

这是我第一次看到这个错误,我不知道如何解决它。错误在第二个 for 循环之后开始。

@tasks.loop(hours = 23.9)
async def checkTodaysBirthdays():
    announcements=client.get_channel(690738010903281734)  #(671210942436081685)
    date_md = today.strftime("%m%d")
    date_BdY = today.strftime("%B %d, %Y")
    # The stores the birthdays.
    if "date_md" and "B_today" in db.keys():
        print("db 'Birthdays announcments' working")
    else:
        db["date_md"] = date_md
        db["B_today"] = True
    # Checks if the birthday message has been sent today.
    if (db["date_md"] == date_md and db["B_today"] == True):
        # Sees if there is a birthday today.
        if db.prefix(f"bd{date_md}") != ():
            # if there is a birthday, loop through the guilds and send a custom message.
            for guild in client.guilds:
                B_days = [] 
                print(guild.name + " " + str(guild.id)) # for testing
                
                for x in db.prefix(f"bd{date_md}"):
                    splitX = db[x].split()
                    print(str(guild.id) + "\n" + splitX[0])
                    if splitX[0] == str(guild.id):
                        print(db[splitX[1]])
                        userId = int(db[splitX[1]])
                        B_days.append(client.get_user(userId).mention)
                        
                        # The final message, orginized. 
                        birthday_box = discord.Embed(
                            title = f"Birthdays Today: {date_BdY}",
                            description = ",".join(B_days),
                            color = discord.Color.red(),
                        )
                        birthday_box.set_thumbnail(url = "https://mpng.subpng.com/20180424/wcw/kisspng-birthday-cake-frosting-icing-emoji-christmas-cak-dumpling-5adf34378c5219.2963794415245773355748.jpg")
                        #birthday_box.set_footer(" :partying_face: :birthday: Be sure to say happy birthday to them! :confetti_ball: :tada:")
                        await announcements.send(embed=birthday_box)
                        #This will be updated later
                        print(f"birthdays today:{date_BdY}")
        else:
            print(f"No Birthdays Today!")
        db["B_today"] = True
    elif (db["date_md"] == date_md and db["B_today"] == False):
        pass
    else:
        db["B_today"] = True
        db["date_md"] = date_md

我不确定代码是错误的还是什么。此外,如果有任何我可以改进的地方,无论是关于此消息的代码或格式,请告诉我。谢谢。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    KeyError 表示您试图获取字典的键,但该键不存在。例如:

    In [1]: mydict = {"key": "value"}
    
    In [2]: mydict["notakey"]
    ---------------------------------------------------------------------------
    KeyError                                  Traceback (most recent call last)
    <ipython-input-2-9a5a2e833438> in <module>
    ----> 1 mydict["notakey"]
    
    KeyError: 'notakey'
    

    因此,在这种情况下,您尝试从 repl.it 数据库(顺便说一句,这是一个字典)中获取一个不存在的键(可能是用户 ID)。

    【讨论】:

    • keyErorr: 310439746247589889 是我想要的,它会将它返回到错误中,所以似乎有东西正在访问我想要的东西,但由于某种原因它给出了错误。
    猜你喜欢
    • 1970-01-01
    • 2019-05-01
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 2018-01-10
    • 1970-01-01
    • 2021-11-10
    相关资源
    最近更新 更多