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 post
Share on other sites
  • Moved to Developer Portal

Kindest Regards,

Snap Dragon

TruckersMP Community Moderator (Forum)

 

Current Rank: 904932591_role_CommunityModerator(2).png1919628800_role_ForumModerator.png.731ed

 

339458068_role_Translator(1).png.1c7213d of the Year 2019

Some Useful Links

Spoiler

 

Player Reports: https://truckersmp.com/reports | Submit Feedback: https://truckersmp.com/feedback | Appeal your Ban: https://truckersmp.com/appeals | Recruitments: https://truckersmp.com/recruitment

Create a Support Ticket: https://truckersmp.com/support/tickets | VTC System: https://truckersmp.com/vtc | Events System: https://truckersmp.com/events

Request an Event Server: https://truckersmp.com/event-request/create | Knowledge Base System: https://truckersmp.com/knowledge-base | Meet the Team: https://truckersmp.com/team

Official Community Contributors of TruckersMP: https://truckersmp.com/contributors | Official TruckersMP Rules: https://truckersmp.com/rules

News Blog: https://truckersmp.com/blog | Get the mod: https://truckersmp.com/download

 

 

Link to post
Share on other sites

Hello.

 

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

<!--CLASS THAT HANDLES THE HTTP REQUEST-->
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;
                Console.WriteLine(status);

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

                }

                response.Close();
                label2.Text = "Online";
                listBox1.Visible = true;
                panel3.BackColor = Color.LawnGreen;
<!-- SEE BELOW THIS -->
                Player[] item = JsonConvert.DeserializeObject<Player[]>(responseFromServer);
                var countPlayers = item.Count();
                label1.Text = countPlayers.ToString() + " Online Players";
                listBox1.Items.Clear();

                foreach (Player name in item)
                    listBox1.Items.Add("   "+name.name);
                listBox1.Items.Add("");
<!-- SEE ABOVE THIS -->
            }
            else
            {
                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;
            }
        } 

                  
        <!--//GETTER-->         
		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 post
Share on other sites
  • 1 month later...

Hey, 

 

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

Sam

 

 

 

 

Link to post
Share on other sites
×
×
  • Create New...