Skip to content
Advertisement

discord.py : Sort message ID in a JSON file by user

I’m currently in the process of making a kind of ticket system. I want to do that through a reaction.

When a user clicks on the reaction, a message is sent to a specific channel. This message ID is saved in a JSON file and should be assigned to the user who clicked on the reaction. But if I save the user and the message individually in the JSON file, I don’t know which user the message belongs to. How can I assign the message ID to a user?

My Code:

@client.event
async def on_raw_reaction_add(reaction):
    ticketmess = 716263100638035969
    ticketlog = client.get_channel(716265124771397662)
    user = client.get_user(reaction.user_id)


    if reaction.message_id == ticketmess and reaction.emoji.id == 680827797165572117:
        with open(r'./tickets.json', 'r')as f:
            ticketchannels = json.load(f)
        if not f"{reaction.user_id}" in ticketchannels:

            ticketmess = await ticketlog.send('Ein User braucht Hilfe!')

            with open(r'./tickets.json', 'r')as f:
                ticketmessages = json.load(f)

                ticketmessages[f"{reaction.user_id}"] = f'{user} in {ticketmess.id}'

                with open(r'./tickets.json', 'w') as f:
                    json.dump(ticketmessages, f, indent=4)

Advertisement

Answer

The code would look something like:

@client.event
async def on_raw_reaction_add(reaction):
    ticketmsg = 716263100638035969
    ticketlog = client.get_channel(716265124771397662)
    user = client.get_user(reaction.user_id)

    if reaction.message_id == ticketmsg and reaction.emoji.id == 680827797165572117:

        # you already have loaded it in once, you don't need to read it again
        with open("tickets.json", "r") as fp:
            data = json.load(fp) 

        if f"{reaction.user_id}" not in data.keys():
            ticketmsg = await ticketlog.send("Ein User braucht Hilfe!")

            # you can just assign the message id to the user, no need for a string
            data[f"{reaction.user_id}"] = ticketmsg.id

            with open("tickets.json", "w+") as fp:
                json.dump(data, fp, sort_keys=True, indent=4) # kwargs are for beautification

You don’t need the f"{user} in {ticketmsg.id}" because you can get the user from the key. Besides, usernames can change, so putting it in the file would prove to be not very useful.
If you wish to get the user’s name, just load in the keys from the json and use client.get_user(ID_HERE).

If you want to search for a user based off of a specific message ID, you can iterate through a dictionary like so:

my_dict = {"key": "value", "foo": "bar"}

for k, v in my_dict.items():
    if v == "bar":
        print(k) # selecting the key based off of its value

# output:
# foo

I’m hoping I understood your question correctly, if not, I’ll be happy to edit my answer after some clarification.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement