如何在D#+中获取我的Discord客户端的运行时间?

huangapple go评论62阅读模式
英文:

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

问题

如何获取D#+中的DiscordClient运行时间,以及如何修复连接终止错误?

基本上,运行时间是机器人在线的时间。

我想制作一个命令,尝试获取机器人的运行时间,并将其发送到DiscordEmbed(嵌入式消息)中。

非常感谢您的回复!

英文:

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!

答案1

得分: 1

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

英文:

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.

huangapple
  • 本文由 发表于 2023年5月24日 20:23:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定