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

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
×
×
  • Create New...