Z
Z
Zakhar2020-08-06 12:55:53
Python
Zakhar, 2020-08-06 12:55:53

How to make it so that the JSON file is updated on the host (I use the Heroku host) - discord.py rewrite?

I am using a library and JSON file to change prefixes on different servers.
That is, if my bot enters some server, then the ID of this server and the default prefix are entered in the JSON file. and when the prefix is ​​changed on the server, then the prefix in the JSON file should change to the one specified by the user on the server.

If you run the bot locally, then everything is OK, everything works, but if you upload it all to GitHub and then to the host, then when the user then there are no changes in the file.

Here is the code for changing the prefix:

def get_prefix(bot, message):
    with open('prefixserv.json','r') as f:
        prefixserv= json.load(f)

    return prefixserv[str(message.guild.id)]


bot=commands.Bot(command_prefix=get_prefix)



@bot.event
async def on_guild_join(guild):
    with open('prefixserv.json','r') as f:
        prefixserv= json.load(f)

    prefixserv[str(guild.id)]= '.'

    with open('prefixserv.json','w') as f:
        json.dump(prefixserv, f, indent=4)

@bot.event
async def on_giuld_remove(guild):
    with open('prefixserv.json','r') as f:
        prefixserv= json.load(f)

    prefixserv.pop(str(guild.id))

    with open('prefixserv.json','w') as f:
        json.dump(prefixserv, f, indent=4)

@bot.command(aliases=['change_prefix','new_prefix','nprefix'])
async def prefix(ctx,prefix):
    with open('prefixserv.json', 'r') as f:
        prefixserv = json.load(f)

    prefixserv[str(ctx.guild.id)] = prefix

    with open('prefixserv.json', 'w') as f:
        json.dump(prefixserv, f, indent=4)

    await ctx.send(f'Префикс был изменен на {prefix}')

JSON file:
{
    "732623824293724221": "/",
    "615807835162411029": "."
}


The only way I've found so far is to use a database.
Is it possible to do without a database?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-08-06
@xzartsust

What didn't you like about the database?
1) speed
2) compactness
3) ease of use
4) data is not stored in files
5) data integrity,
etc.
if you plan to promote the bot in the future, then json simply cannot cope with a large amount of information - I advise you all- rethink your decision.
if it's your problem, first make sure heroku has automatic file updates enabled:
5f2bd6a909517668219084.png
I'm not entirely sure that the file can be updated at all

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question