elixss
elixss

Reputation: 75

how to just get the ID out of the json without []?

is there a way to use something diffenrent to just get the ID without the [] in the json file, without removing or adding something? only things i know are "append" and "remove" and something like read, get, load or check didn't work. questionmarks are there where i need the right word. If i just type in f'Name: <@{json_dict["blockedUser"]>' i just get the ID in the [] and <@ and > are just there. i would love to see a solution. if possible, i would display the user.display_name instead of the mention in Name:. Thanks for the help!

    @commands.command(name='checkblock')  # checks if a user is on the list of blocked users
    async def checkblock(self, ctx):
        user = User
        with open('./bot_config/blocked_users.json', 'r') as json_file:
            json_dict = json.load(json_file)
        await ctx.send('Here are the blocked users:\n'
                       f'Name: <@{json_dict["blockedUser"]???(user.id)}>'
                       f'ID: {json_dict["blockedUser"].???(user.id)}')
        return

Here is my .json file with an ID inside.

{
    "blockedUser": [
        360881524410810380
    ]
}

Upvotes: 0

Views: 50

Answers (1)

MrSpaar
MrSpaar

Reputation: 3994

json_dict["blockedUser"] is a list, so you can loop through it's content:

@commands.command(name='checkblock')  # checks if a user is on the list of blocked users
async def checkblock(self, ctx):
    with open('bot_config/blocked_users.json') as file:
        json_dict = json.load(file)

    template = 'Name: <@{}> (ID: {})'
    member_list = '\n'.join([template.format(id, id) for id in json_dict])
  
    await ctx.send(f'Here are the blocked users:\n{member_list}')

If you want the member's display_name, you'll have to get the discord.Member object, either with:

Upvotes: 1

Related Questions