geekbot/Geekbot.net/Commands/Changelog.cs

89 lines
3.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Geekbot.net.Lib;
using Newtonsoft.Json;
namespace Geekbot.net.Commands
{
public class Changelog : ModuleBase
{
private readonly DiscordSocketClient _client;
2017-12-29 01:53:50 +01:00
private readonly IErrorHandler _errorHandler;
public Changelog(IErrorHandler errorHandler, DiscordSocketClient client)
{
_errorHandler = errorHandler;
_client = client;
}
2017-12-29 01:53:50 +01:00
[Command("changelog", RunMode = RunMode.Async)]
[Alias("updates")]
[Remarks(CommandCategories.Helpers)]
[Summary("Show the latest 5 updates")]
public async Task getChangelog()
{
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.github.com");
2017-12-29 01:53:50 +01:00
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent",
"http://developer.github.com/v3/#user-agent-required");
var response = await client.GetAsync("/repos/pizzaandcoffee/geekbot.net/commits");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
var commits = JsonConvert.DeserializeObject<List<Commit>>(stringResponse);
var eb = new EmbedBuilder();
eb.WithColor(new Color(143, 165, 102));
2017-12-29 01:53:50 +01:00
eb.WithAuthor(new EmbedAuthorBuilder
{
IconUrl = _client.CurrentUser.GetAvatarUrl(),
Name = "Latest Updates",
Url = "https://geekbot.pizzaandcoffee.rocks/updates"
});
var sb = new StringBuilder();
foreach (var commit in commits.Take(10))
sb.AppendLine($"- {commit.commit.message} ({commit.commit.author.date:yyyy-MM-dd})");
eb.Description = sb.ToString();
2017-12-29 01:53:50 +01:00
eb.WithFooter(new EmbedFooterBuilder
{
Text = $"List generated from github commits on {DateTime.Now:yyyy-MM-dd}"
});
await ReplyAsync("", false, eb.Build());
}
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
2017-12-29 01:53:50 +01:00
private class Commit
{
public string sha { get; set; }
public CommitInfo commit { get; set; }
public Uri html_url { get; set; }
}
2017-12-29 01:53:50 +01:00
private class CommitInfo
{
public commitAuthor author { get; set; }
public string message { get; set; }
}
private class commitAuthor
{
public string name { get; set; }
public string email { get; set; }
public DateTimeOffset date { get; set; }
}
}
}