Home > OS >  Discord Bot Python Databases
Discord Bot Python Databases

Time:06-15

I currently have made a discord bot that has an economy data base (in json format). However, I do not want this economy information to carry between servers (when its added to multiple servers) but instead want a new json file for each server. Can anyone tell me how a discord bot can tell if its in a new server and how to create a new json file for that said server (without me adding it to my file).

If there is other methods without making a new file for each server, I am open to hear any ideas as well :)

CodePudding user response:

discord.py has a on_guild_join event you can monitor to detect new guilds. You could make a dictionary that uses the Guild ID as a key, and the JSON object as a value for example.

@client.event
def on_guild_join(guild):
    guild_db = economy_db.get(str(guild.id))
    if not guild_db:  # If the guild hasn't been visited yet
        economy_db[str(guild.id)] = dict()
        # Do your stuff to create a new economic system
    else:
        # In case the guild already exists in the database
        # Do stuff here
  • Related