StarAssassin64 0 Posted January 13 Share Posted January 13 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
ASIR [CZE] 820 Posted January 13 Share Posted January 13 Hi, I think you will be more successful with your question in this section: https://forum.truckersmp.com/index.php?/forum/198-developer-portal/ Best Regards //ASIR Link to post Share on other sites
Snap Dragon 1126 Posted January 14 Share Posted January 14 Moved to Developer Portal Link to post Share on other sites
VSOUSA14 41 Posted January 18 Share Posted January 18 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. Link to post Share on other sites
SamN 28 Posted February 19 Share Posted February 19 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
Freazy_TMP 135 Posted February 20 Share Posted February 20 Good Work! Link to post Share on other sites
Recommended Posts