NadekoBot/NadekoBot.Modules.Searches/XkcdCommands.cs

86 lines
4.0 KiB
C#
Raw Normal View History

2016-10-22 11:08:52 +00:00
using Discord;
using Discord.Commands;
2016-12-09 03:22:23 +00:00
using NadekoBot.Extensions;
2016-10-22 11:08:52 +00:00
using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common;
using NadekoBot.Common.Attributes;
2016-10-22 11:08:52 +00:00
namespace NadekoBot.Modules.Searches
{
public partial class Searches
{
[Group]
2017-02-24 16:10:44 +00:00
public class XkcdCommands : NadekoSubmodule
2016-10-22 11:08:52 +00:00
{
2017-02-24 16:10:44 +00:00
private const string _xkcdUrl = "https://xkcd.com";
2016-10-22 11:08:52 +00:00
[NadekoCommand, Usage, Description, Aliases]
[Priority(0)]
2016-12-16 18:43:57 +00:00
public async Task Xkcd(string arg = null)
2016-10-22 11:08:52 +00:00
{
if (arg?.ToLowerInvariant().Trim() == "latest")
{
using (var http = new HttpClient())
{
2017-02-24 16:10:44 +00:00
var res = await http.GetStringAsync($"{_xkcdUrl}/info.0.json").ConfigureAwait(false);
2016-10-22 11:08:52 +00:00
var comic = JsonConvert.DeserializeObject<XkcdComic>(res);
2017-03-14 07:43:27 +00:00
var embed = new EmbedBuilder().WithColor(NadekoBot.OkColor)
.WithImageUrl(comic.ImageLink)
.WithAuthor(eab => eab.WithName(comic.Title).WithUrl($"{_xkcdUrl}/{comic.Num}").WithIconUrl("http://xkcd.com/s/919f27.ico"))
.AddField(efb => efb.WithName(GetText("comic_number")).WithValue(comic.Num.ToString()).WithIsInline(true))
.AddField(efb => efb.WithName(GetText("date")).WithValue($"{comic.Month}/{comic.Year}").WithIsInline(true));
var sent = await Context.Channel.EmbedAsync(embed)
2016-10-22 11:08:52 +00:00
.ConfigureAwait(false);
await Task.Delay(10000).ConfigureAwait(false);
2017-03-14 07:43:27 +00:00
await sent.ModifyAsync(m => m.Embed = embed.AddField(efb => efb.WithName("Alt").WithValue(comic.Alt.ToString()).WithIsInline(false)).Build());
2016-10-22 11:08:52 +00:00
}
return;
}
2016-12-16 18:43:57 +00:00
await Xkcd(new NadekoRandom().Next(1, 1750)).ConfigureAwait(false);
2016-10-22 11:08:52 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[Priority(1)]
2016-12-16 18:43:57 +00:00
public async Task Xkcd(int num)
2016-10-22 11:08:52 +00:00
{
if (num < 1)
return;
using (var http = new HttpClient())
{
2017-02-24 16:10:44 +00:00
var res = await http.GetStringAsync($"{_xkcdUrl}/{num}/info.0.json").ConfigureAwait(false);
2016-10-22 11:08:52 +00:00
var comic = JsonConvert.DeserializeObject<XkcdComic>(res);
2016-12-09 03:22:23 +00:00
var embed = new EmbedBuilder().WithColor(NadekoBot.OkColor)
2016-12-16 18:43:57 +00:00
.WithImageUrl(comic.ImageLink)
2017-02-24 16:10:44 +00:00
.WithAuthor(eab => eab.WithName(comic.Title).WithUrl($"{_xkcdUrl}/{num}").WithIconUrl("http://xkcd.com/s/919f27.ico"))
.AddField(efb => efb.WithName(GetText("comic_number")).WithValue(comic.Num.ToString()).WithIsInline(true))
.AddField(efb => efb.WithName(GetText("date")).WithValue($"{comic.Month}/{comic.Year}").WithIsInline(true));
2016-12-16 18:43:57 +00:00
var sent = await Context.Channel.EmbedAsync(embed)
2016-10-22 11:08:52 +00:00
.ConfigureAwait(false);
await Task.Delay(10000).ConfigureAwait(false);
2017-01-03 15:02:02 +00:00
await sent.ModifyAsync(m => m.Embed = embed.AddField(efb => efb.WithName("Alt").WithValue(comic.Alt.ToString()).WithIsInline(false)).Build());
2016-10-22 11:08:52 +00:00
}
}
}
public class XkcdComic
{
public int Num { get; set; }
public string Month { get; set; }
public string Year { get; set; }
[JsonProperty("safe_title")]
public string Title { get; set; }
[JsonProperty("img")]
public string ImageLink { get; set; }
public string Alt { get; set; }
}
}
}