Mr Mandude
Mr Mandude

Reputation: 1

Discord.py get_user KeyError(key)

This my first time making one of these, so bare with me.

I am currently facing a problem where, using replit's database (which is a dictionary I'm pretty sure), I am trying to make a birthday loop, and I got this KeyError.

in get_raw raise KeyError(key) keyErorr: 310439746247589889

The KeyError that this is coming from is from a list I am trying to split. is two numbers split with a space, or in this case:

"bd043010" : "690738010903281734 310439746247589889"

This the first time I've seen this error, and I don't know how to solve it. The error starts after the second for loop.

@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

I am not sure if the code is wrong or what. Also if there is anything I can improve, either about the code or the formatting of this message, please tell me. Thank you.

Upvotes: 0

Views: 230

Answers (1)

Secret Agent
Secret Agent

Reputation: 429

A KeyError means you tried to get a key of a dictionary, where that key did not exist. For example:

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'

So in this case, you tried to get a key (probably a user id) from the repl.it database (which is a dictionary, btw) that didn't exist.

Upvotes: 1

Related Questions