0

I've started teaching myself Python about three weeks ago and i've been working on an RCON cog for my Ark server for Red discord bot. I have a config i am trying to format so that i can use a command to easily view all the settings for the servers.

here is the config i've created so far for the servers

{
   "clusters": {
      "pvp": {
         "joinchannel": 739270285450018927,
         "leavechannel": 851819367439400960,
         "adminlogchannel": 848933429164507156,
         "globalchatchannel": 841118994399494164,
         "servers": {
            "rag": {
               "ip": "192.168.1.201",
               "port": 27020,
               "password": "passwd",
               "chatchannel": 770891102601084928
            },
            "val": {
               "ip": "192.168.1.203",
               "port": 27024,
               "password": "passwd",
               "chatchannel": 770891102601084928
            },
            "island": {
               "ip": "192.168.1.205",
               "port": 27021,
               "password": "passwd",
               "chatchannel": 770891102601084928
            }
         }
      },
      "pve": {
         "joinchannel": 739270285450018927,
         "leavechannel": 851819367439400960,
         "adminlogchannel": 848933429164507156,
         "globalchatchannel": 841118994399494164,
         "servers": {
            "rag": {
               "ip": "192.168.1.202",
               "port": 27022,
               "password": "passwd",
               "chatchannel": 770891102601084928
            },
            "val": {
               "ip": "192.168.1.204",
               "port": 27023,
               "password": "passwd",
               "chatchannel": 770891102601084928
            },
            "island": {
               "ip": "192.168.1.206",
               "port": 27025,
               "password": "passwd",
               "chatchannel": 770891102601084928
            }
         }
      }
   }
}

Here is what i would like it to look like if i were to print the "server settings"

PvP Cluster

map: rag
ip: 192.168.1.201
port: 27020
password: psswrd
chatchannel: 770891102601084928
map: val
ip: 192.168.1.203
port: 27024
password: psswrd
chatchannel: 770891102601084928
map: island
ip: 192.168.1.205
port: 27021
password: psswrd
chatchannel: 770891102601084928

PvE Cluster

map: rag
ip: 192.168.1.202
port: 27022
password: psswrd
chatchannel: 770891102601084928
map: val
ip: 192.168.1.204
port: 27023
password: psswrd
chatchannel: 770891102601084928
map: island
ip: 192.168.1.206
port: 27025
password: psswrd
chatchannel: 770891102601084928

So far i have tried

        serverlist = []
        settings = await self.config.guild(ctx.guild).all()
        for cluster in settings["clusters"]:
            for name, server in cluster["servers"].items():
                serverlist.extend(settings[cluster]["servers"])
        await ctx.send(serverlist)

and some other probably noob logic to get it formatted how i want but with this many nested dicts i am super lost.

Eventually i'll make it into an embed. but right now i'm just trying to better understand how to manipulate dictionaries from my config to display it in the discord better.

I would like to print this all at one time as one message too.

If anyone could hold my hand a bit on this and show an example using my config it would be much appreciated :)

Vertyco
  • 11
  • 2
  • Please update your question with the code you have tried. – quamrana Jul 16 '21 at 21:24
  • @quamrana i edited it for the main one i tried with the help of a friend but other than that i am just not sure how to go about setting up the logic for how i want it displayed. the best i can imagine is a for loop within a for loop for iterating the clusters and then the servers within the clusters. – Vertyco Jul 16 '21 at 21:32

2 Answers2

0

This code outputs what you want, but I suspect you are after the information in a structured form to forward on:

types = ['pvp','pve']
maps = ['rag','val','island']
items = ['ip','port','password','chatchannel']

for t in types:
    servers = settings['clusters'][t]['servers']
    print(t,'Cluster')
    for m in maps:
        s = servers[m]
        for i in items:
            print(i,s[i])
        print()
    print()

Output as requested

quamrana
  • 37,849
  • 12
  • 53
  • 71
0

This should work:

print_all = []
for pv in settings["clusters"]:
    print_all.append(f'\n{pv.upper()} Cluster\n')
    for server in settings["clusters"][pv]["servers"]:
        print_all.append(f'map: {server}')
        for k,v in settings["clusters"][pv]["servers"][server].items():
            print_all.append(f'{k}: {v}')
        print_all.append('')
print_all = '\n'.join(print_all) # a single string
print(print_all) 
Shar
  • 448
  • 3
  • 8
  • Thanks!! got it to work, is there a way for it to gather everything up and print it all at once? i'm using this for a discord bot so it would be nice to have it print as one message :) – Vertyco Jul 16 '21 at 21:54
  • I have edited it to have everything stored in one string, ready for print – Shar Jul 16 '21 at 22:07