0

Basically I have a discord bot that auto leaves servers unless its in a database. I was wondering how I could make a command to add to that database and remove? Heres my current code

server=[878537556332015626, 
        884958773913985054, 
        869615568838357052]
@client.event
async def on_guild_join(guild):
    if guild.id in server:
        pass
    elif guild.id not in server:
         await guild.leave()

So where it says "server=[stuff here]" how could i make a command to add servers ids to that database? Maybe like ';addserver server-id' and it'd add it to that database, and also a remove command. Like ';removeserver server-id' ive attempted multiple times but it didnt work, neither did it show an error.

Or help me make this into a json file!

Ex Dev
  • 13
  • 3
  • Could you include how you implemented addserver and removeserver? For writing to json file, [see this answer](https://stackoverflow.com/a/12309296/10168590). For reading from json file, [see this answer](https://stackoverflow.com/a/20199213/10168590). – Nevus Sep 14 '21 at 05:17

1 Answers1

0

If your goal is to make a command for your bot such as ;addserver server-id, it would be quite simple. Here's an example for checking the validity of the ID and writing it to a servers.json file if it's valid:

@client.command()
async def addserver(ctx, message = None):
    if client.get_guild(int(message)): # If the bot is in a guild with this ID, returns True
        serverFile = json.loads(open('servers.json', 'r').read()) # Reading Json file to a dict
        serverFile['servers'].append(int(message)) # Appending new ID to the dict
        with open('servers.json', 'w') as writeFile:
            json.dump(serverFile, writeFile, indent=4) # Writing dict to Json file
    else:
        pass # Do whatever you want with this part, maybe an error message would be sent to the command sender or something.

And servers.json should be laid out like this:

{
    "servers": [
        
    ]
}

Reading the list of server IDs from the file would look like this:

json.loads(open('servers.json', 'r').read())['servers'] # Returns a list of IDs in the file.

And to remove an ID from the list:

@client.command()
async def removeserver(ctx, message = None):
    serverList = json.loads(open('servers.json', 'r').read())['servers']
    for id in serverList:
        if int(message) == id: 
            serverList.remove(id)
            serverDict = {}
            serverDict['servers'] = serverList
    with open('servers.json', 'w') as writeFile:
        json.dump(serverDict, writeFile, indent=4)

The part you'll likely find the most value out of is the if client.get_guild(int(message)) line, as it will let you know whether the ID is valid or not. This can be applied to new functions that could clean up your server list if need be.

Pepe Salad
  • 238
  • 2
  • 6
  • When I do the removeserver command it also removed the "servers": in the json file. – Ex Dev Sep 14 '21 at 22:14
  • Ah, my bad. You'll also need to convert the values in the `serverlist` to a dictionary value with name `servers`. The way to do this would be something like `serverDict['servers'] = serverList` and then write `serverDict` to the servers.json file. (And create an empty dict with name serverDict before doing this with `serverDict = {}`. I've edited my original answer to include this. – Pepe Salad Sep 15 '21 at 09:22
  • How would I also make it be able to add guilds my bot isnt in? Since it leaves servers if the guild id isnt in the json file i would need to make it so I would be able to add ids with `;addserver ` even if its not in the server. – Ex Dev Sep 15 '21 at 22:38