This commit is contained in:
Kwoth
2016-10-22 13:08:52 +02:00
parent 5856f81bc6
commit 5063efd7e7
4 changed files with 125 additions and 1 deletions

View File

@@ -0,0 +1,87 @@
using Discord;
using Discord.Commands;
using NadekoBot.Attributes;
using NadekoBot.Services;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Searches
{
public partial class Searches
{
[Group]
public class XkcdCommands
{
private const string xkcdUrl = "https://xkcd.com";
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[Priority(1)]
public async Task Xkcd(IUserMessage msg, string arg = null)
{
var channel = (ITextChannel)msg.Channel;
if (arg?.ToLowerInvariant().Trim() == "latest")
{
using (var http = new HttpClient())
{
var res = await http.GetStringAsync($"{xkcdUrl}/info.0.json").ConfigureAwait(false);
var comic = JsonConvert.DeserializeObject<XkcdComic>(res);
var sent = await channel.SendMessageAsync($"{msg.Author.Mention} " + comic.ToString())
.ConfigureAwait(false);
await Task.Delay(10000).ConfigureAwait(false);
await sent.ModifyAsync(m => m.Content = sent.Content + $"\n`Alt:` {comic.Alt}");
}
return;
}
await Xkcd(msg, new NadekoRandom().Next(1, 1750)).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[Priority(0)]
public async Task Xkcd(IUserMessage msg, int num)
{
var channel = (ITextChannel)msg.Channel;
if (num < 1)
return;
using (var http = new HttpClient())
{
var res = await http.GetStringAsync($"{xkcdUrl}/{num}/info.0.json").ConfigureAwait(false);
var comic = JsonConvert.DeserializeObject<XkcdComic>(res);
var sent = await channel.SendMessageAsync($"{msg.Author.Mention} " + comic.ToString())
.ConfigureAwait(false);
await Task.Delay(10000).ConfigureAwait(false);
await sent.ModifyAsync(m => m.Content = sent.Content + $"\n`Alt:` {comic.Alt}");
}
}
}
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; }
public override string ToString()
=> $"`Comic:` #{Num} `Title:` {Title} `Date:` {Month}/{Year}\n{ImageLink}";
}
}
}