Search code examples
discorddsharp+

How do i get my DiscordClient's uptime in D#+?


Does anyone know how to get the DiscordClient Uptime in D#+ And how to fix the Connection terminated error?

Basically the Uptime is the time that the bot was on for.

And i wanna make a command that will try to get the bot's uptime and send it in an DiscordEmbed (embed)

A reply would be soo much appreciated thanks!


Solution

  • The easiest way to do this is to add a DateTime variable to your main async that connects your bot.

    public static DateTime startTime = DateTime.Now;
    
    public static async Task MainAsync()
    {
      var discord = new DiscordClient(Settings.ClientSettings);
      //provide all your settings for the discord client here
      //Call the discord.Ready event to reset the startTime to when the bot connects
      discord.Ready += async (discordClient, readyEventArgs) =>
      {
        startTime = DateTime.Now; //This will set the startTime to the ms the bot connects
      };
      await discord.ConnectAsync();
      await Task.Delay(-1);
    }
    

    You can now call this with the within the allowed scope of your class by using ClassName.startTime and get the time since this with DateTime.Now - ClassName.startTime. You can then manipulate the way this is shown however you'd like.