简体   繁体   中英

Discord Bot Python Databases

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 :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM