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}";
}
}
}

View File

@ -7160,6 +7160,33 @@ namespace NadekoBot.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to xkcd.
/// </summary>
public static string xkcd_cmd {
get {
return ResourceManager.GetString("xkcd_cmd", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Shows a XKCD comic. No arguments will retrieve random one. Number argument will retrieve a specific comic, and &quot;latest&quot; will get the latest one..
/// </summary>
public static string xkcd_desc {
get {
return ResourceManager.GetString("xkcd_desc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to `{0}xkcd` or `{0}xkcd 1400` or `{0}xkcd latest`.
/// </summary>
public static string xkcd_usage {
get {
return ResourceManager.GetString("xkcd_usage", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to yomama ym.
/// </summary>

View File

@ -2502,4 +2502,13 @@
<data name="unmute_usage" xml:space="preserve">
<value>`{0}unmute @Someone`</value>
</data>
<data name="xkcd_cmd" xml:space="preserve">
<value>xkcd</value>
</data>
<data name="xkcd_desc" xml:space="preserve">
<value>Shows a XKCD comic. No arguments will retrieve random one. Number argument will retrieve a specific comic, and "latest" will get the latest one.</value>
</data>
<data name="xkcd_usage" xml:space="preserve">
<value>`{0}xkcd` or `{0}xkcd 1400` or `{0}xkcd latest`</value>
</data>
</root>

View File

@ -56,7 +56,8 @@
},
"configurations": {
"GlobalNadeko": {
"buildOptions": {"define": ["GLOBAL_NADEKO"]}
"buildOptions": { "define": [ "GLOBAL_NADEKO" ] },
"compilationOptions": { "optimize": true }
}
}
}