discord.py get\u user key错误(key)

k2arahey  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(304)

这是我第一次做这样的东西,光着身子。
我现在面临一个问题,使用replit的数据库(我很确定这是一个字典),我试图创建一个birthday循环,但是我得到了这个keyerror。
in get_raw raise KeyError(key) keyErorr: 310439746247589889 这个关键错误来自我试图拆分的列表。两个数字用空格分开,或者在这种情况下: "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

我不确定代码是不是错了。另外,如果有什么我可以改进,无论是关于代码或格式的信息,请告诉我。谢谢您。

k5hmc34c

k5hmc34c1#

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)。

相关问题