2016-08-18 21:00:54 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using NadekoBot.Extensions;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2016-10-25 11:06:37 +00:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading;
|
2016-08-18 21:00:54 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Services.Impl
|
|
|
|
|
{
|
|
|
|
|
public class StatsService : IStatsService
|
|
|
|
|
{
|
|
|
|
|
private int messageCounter;
|
2016-09-30 03:58:44 +00:00
|
|
|
|
private ShardedDiscordClient client;
|
2016-08-18 21:00:54 +00:00
|
|
|
|
private DateTime started;
|
2016-09-08 22:22:55 +00:00
|
|
|
|
private int commandsRan = 0;
|
2016-08-18 21:00:54 +00:00
|
|
|
|
|
2016-11-10 01:02:22 +00:00
|
|
|
|
public const string BotVersion = "1.0-rc2";
|
2016-08-18 21:00:54 +00:00
|
|
|
|
|
|
|
|
|
public string Heap => Math.Round((double)GC.GetTotalMemory(false) / 1.MiB(), 2).ToString();
|
|
|
|
|
|
2016-10-25 11:06:37 +00:00
|
|
|
|
Timer carbonitexTimer { get; }
|
|
|
|
|
|
2016-08-18 21:00:54 +00:00
|
|
|
|
|
2016-09-30 03:58:44 +00:00
|
|
|
|
public StatsService(ShardedDiscordClient client, CommandHandler cmdHandler)
|
2016-08-18 21:00:54 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
this.client = client;
|
|
|
|
|
|
|
|
|
|
Reset();
|
|
|
|
|
this.client.MessageReceived += _ => Task.FromResult(messageCounter++);
|
2016-09-08 22:22:55 +00:00
|
|
|
|
cmdHandler.CommandExecuted += (_, e) => commandsRan++;
|
2016-08-18 21:00:54 +00:00
|
|
|
|
|
|
|
|
|
this.client.Disconnected += _ => Reset();
|
2016-10-25 11:06:37 +00:00
|
|
|
|
|
|
|
|
|
this.carbonitexTimer = new Timer(async (state) =>
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(NadekoBot.Credentials.CarbonKey))
|
|
|
|
|
return;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var http = new HttpClient())
|
|
|
|
|
{
|
|
|
|
|
using (var content = new FormUrlEncodedContent(
|
|
|
|
|
new Dictionary<string, string> {
|
|
|
|
|
{ "servercount", this.client.GetGuilds().Count.ToString() },
|
|
|
|
|
{ "key", NadekoBot.Credentials.CarbonKey }}))
|
|
|
|
|
{
|
|
|
|
|
content.Headers.Clear();
|
|
|
|
|
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
|
|
|
|
|
|
var res = await http.PostAsync("https://www.carbonitex.net/discord/data/botdata.php", content).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
}, null, TimeSpan.FromHours(1), TimeSpan.FromHours(1));
|
2016-08-18 21:00:54 +00:00
|
|
|
|
}
|
2016-10-14 22:03:52 +00:00
|
|
|
|
public async Task<string> Print()
|
2016-09-30 03:58:44 +00:00
|
|
|
|
{
|
2016-10-14 22:03:52 +00:00
|
|
|
|
var curUser = await client.GetCurrentUserAsync();
|
2016-11-19 12:22:49 +00:00
|
|
|
|
return $@"```css
|
2016-11-19 14:46:53 +00:00
|
|
|
|
Author: [Kwoth#2560] | Library: [Discord.Net]
|
2016-11-19 12:22:49 +00:00
|
|
|
|
Bot Version: [{BotVersion}]
|
2016-11-19 17:12:26 +00:00
|
|
|
|
Bot ID: {curUser.Id}
|
2016-11-19 18:25:00 +00:00
|
|
|
|
Owner ID(s): {string.Join(", ", NadekoBot.Credentials.OwnerIds)}
|
2016-11-19 12:22:49 +00:00
|
|
|
|
Uptime: {GetUptimeString()}
|
|
|
|
|
Servers: {client.GetGuilds().Count} | TextChannels: {client.GetGuilds().SelectMany(g => g.GetChannels().Where(c => c is ITextChannel)).Count()} | VoiceChannels: {client.GetGuilds().SelectMany(g => g.GetChannels().Where(c => c is IVoiceChannel)).Count()}
|
2016-11-19 12:24:53 +00:00
|
|
|
|
Commands Ran this session: {commandsRan}
|
2016-11-19 12:22:49 +00:00
|
|
|
|
Messages: {messageCounter} [{messageCounter / (double)GetUptime().TotalSeconds:F2}/sec] Heap: [{Heap} MB]```";
|
2016-09-30 03:58:44 +00:00
|
|
|
|
}
|
2016-08-18 21:00:54 +00:00
|
|
|
|
|
|
|
|
|
public Task Reset()
|
|
|
|
|
{
|
|
|
|
|
messageCounter = 0;
|
|
|
|
|
started = DateTime.Now;
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TimeSpan GetUptime() =>
|
|
|
|
|
DateTime.Now - started;
|
|
|
|
|
|
|
|
|
|
public string GetUptimeString()
|
|
|
|
|
{
|
|
|
|
|
var time = GetUptime();
|
|
|
|
|
return time.Days + " days, " + time.Hours + " hours, and " + time.Minutes + " minutes.";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|