0
votes

First time here, I am making a small discord.py bot as a project to experiment with python/apis a little. My goal is to print in discord specific data from an api when asked. here is the code in question.

@client.command()
async def otherusers(ctx, player):
    rs = requests.get(apiLink + "/checkban?name=" + str(player))
    if rs.status_code == 200:
        rs = rs.json()
        embed = discord.Embed(title="Other users for" + str(player), description="""User is known as: """ + str(rs["usedNames"]))
        await ctx.send(embed=embed)

here is an example of the API request

{"id":1536171865,"avatar":"https://secure.download.dm.origin.com/production/avatar/prod/userAvatar/41472001/208x208.PNG","name":"_7cV","vban":{"A1 Army of One":{"bannedUntil":null,"reason":"ping >1000"}},"ingame":[],"otherNames":{"updateTimestamp":"2022-07-08T10:10:50.939000","usedNames":["ABCDE123","ABCDE1234","ABCDE12345","ABCDE1234567"]}}

If I change the string to str(rs["otherNames"]) it does function but I would like to only include the usernames, if I put str(rs["usedNames"]) and request on discord it gives me an error on PyCharm.

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'usedNames'

Thanks in advance :)

1

1 Answers

0
votes

Alright so as far as I can tell, the return from your API request where the "usedNames" key is located is nested. Try:

str(rs["otherNames"]["usedNames"])

I should note this will return ["ABCDE123","ABCDE1234","ABCDE12345","ABCDE1234567"] in the example which you gave. You might want to format the list of other usernames for your final product.

I hope that helped:)