Chronoxx
Chronoxx

Reputation: 107

Discord py and JSON

I wanted to add a functionality to my bot which is to create custom uniques codes for people to invite their friend. (So we can track who invites who even with a vanity URL)

The creation of the codes works well, now I want to store everything in a JSON.

I thought of that structure (1 File per Guild):

{
"<Member ID>" :
    {
        "invites": 0
        "Fakes": 0
        "Left":0
        "Invited": []
    }
}

First I wanted to know if the structure is good (or if I missed anything). And second to know if I won't have any concurrency problems. I need to update the JSON every time a member do /verify, or leave the server. But I need to open the file, modify, then write the file again.. if two people do the command at the same time, what happen ?

If there is concurrency problem, would a database like SQLite3 solve the problem ? And what tables should I use in this case?

Thank you very much for your help

Upvotes: 1

Views: 220

Answers (1)

RMA Dev
RMA Dev

Reputation: 186

If it were me, depending how many guilds the bot is in and how many members you'd be tracking, I'd opt for a database that supports concurrency. I would try to stay away from using a JSON file, as not only will that not support concurrency, but JSON files are also prone to corruption.

I like SQLite due to it's ease of use and performance, but you could run into concurrency issues there, too.

Do with that information what you will, but no matter which DB you wind up going with, I'd personally go with a table for each guild and each row would be something like Member ID (Primary Key) | Invites | Fakes | Left | Invited with member IDs invited separated by comma or something like that.

Upvotes: 1

Related Questions