英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论