Jump to content

How can I work the server stats into a discord command for a custom discord bot? (Development of Discord Bot)


Recommended Posts

So, I am working on a discord bot for my VTC, which we want on the commands to get the TMP server stats. We have the location for the API, and when we do the print command (we are using discord.py) It'll print all the server stats, but once I get it to send the messages, It'll only get the information about the last server. Is there a way where I can get it to give all the information like the print command.


Link to comment
Share on other sites



I never used python, but you can try to create getters and then use an array where you get the getters and the response from server, and then make a loop to print all the servers:

Something like this (This code below is in C#)

public void UpdateServerInfo()
            svCon = "http://" + ipSRV + "/players.json";
            WebRequest request = WebRequest.Create(svCon);
            request.Credentials = CredentialCache.DefaultCredentials;
            ipServ = isExist(svCon);
            if (ipServ)
                WebResponse response = request.GetResponse();
                status = ((HttpWebResponse)response).StatusDescription;

                using (Stream dataStream = response.GetResponseStream())
                    StreamReader reader = new StreamReader(dataStream);
                    responseFromServer = reader.ReadToEnd();


                label2.Text = "Online";
                listBox1.Visible = true;
                panel3.BackColor = Color.LawnGreen;
                Player[] item = JsonConvert.DeserializeObject<Player[]>(responseFromServer);
                var countPlayers = item.Count();
                label1.Text = countPlayers.ToString() + " Online Players";

                foreach (Player name in item)
                    listBox1.Items.Add("   "+name.name);
                label2.Text = "Offline";
                panel3.BackColor = Color.Red;
                label1.Text = "Visit our discord our teamspeak for more information";
                button2.Visible = false;
                listBox1.Visible = false;
                panel4.Visible = false;
                label4.Visible = false;

		public class Player
            public string endpoint { get; set; }
            public int id { get; set; }
            public string[] identifiers { get; set; }
            public string name { get; set; }
            public int ping { get; set; }


In this C# programm, i am printing out each player into a listBox. In your Discord bot you can make something similar.

01011001 01101111 01110101 00100000 01101100 01101111 01110011 01110100 00100000 00110001 00110000 00100000 01110011 01100101 01100011 01101111 01101110 01100100 01110011 00100000 01110010 01100101 01100001 01100100 01101001 01101110 01100111 00100000 01110100 01101000 01101001 01110011 00101110  :D

Link to comment
Share on other sites

  • 1 month later...



Firstly, if you have little experience with Python, I recommend you learn some of the basics first. This can be done many ways from YouTube videos to interactive platforms/websites and it's down to your personal preference.


When making an API request, be careful not to "block" your bot. More information about the recommended way of making an API request in discord.py can be found in the documentation.

As you've stated above, you'd like the bot to display TMP's server stats which can be found using the following gateway: https://api.truckersmp.com/v2/servers. The response is in JSON, which functions similarly to a Python dictionary.


Now that we have the response, you'll notice the API contains a list named "response". Each list item is a server in which it's information are is a dictionary format.

Therefore, to get all servers then, loop through the response list items (each server) and deal with the data you'd like. Useful pieces of information to look at for each server include: name, maxplayers and players though you can deal with whatever information you'd like.


I hope this helped without just giving away the code (which I don't think would have helped).






Link to comment
Share on other sites

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.