diff --git a/src/NadekoBot/Modules/Searches/Commands/XkcdCommands.cs b/src/NadekoBot/Modules/Searches/Commands/XkcdCommands.cs new file mode 100644 index 00000000..1d13a372 --- /dev/null +++ b/src/NadekoBot/Modules/Searches/Commands/XkcdCommands.cs @@ -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(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(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}"; + } + } +} diff --git a/src/NadekoBot/Resources/CommandStrings.Designer.cs b/src/NadekoBot/Resources/CommandStrings.Designer.cs index 2be25e4d..d497209f 100644 --- a/src/NadekoBot/Resources/CommandStrings.Designer.cs +++ b/src/NadekoBot/Resources/CommandStrings.Designer.cs @@ -7160,6 +7160,33 @@ namespace NadekoBot.Resources { } } + /// + /// Looks up a localized string similar to xkcd. + /// + public static string xkcd_cmd { + get { + return ResourceManager.GetString("xkcd_cmd", resourceCulture); + } + } + + /// + /// 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 "latest" will get the latest one.. + /// + public static string xkcd_desc { + get { + return ResourceManager.GetString("xkcd_desc", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to `{0}xkcd` or `{0}xkcd 1400` or `{0}xkcd latest`. + /// + public static string xkcd_usage { + get { + return ResourceManager.GetString("xkcd_usage", resourceCulture); + } + } + /// /// Looks up a localized string similar to yomama ym. /// diff --git a/src/NadekoBot/Resources/CommandStrings.resx b/src/NadekoBot/Resources/CommandStrings.resx index e20fb202..6b127fc5 100644 --- a/src/NadekoBot/Resources/CommandStrings.resx +++ b/src/NadekoBot/Resources/CommandStrings.resx @@ -2502,4 +2502,13 @@ `{0}unmute @Someone` + + xkcd + + + Shows a XKCD comic. No arguments will retrieve random one. Number argument will retrieve a specific comic, and "latest" will get the latest one. + + + `{0}xkcd` or `{0}xkcd 1400` or `{0}xkcd latest` + \ No newline at end of file diff --git a/src/NadekoBot/project.json b/src/NadekoBot/project.json index bd846dfc..615336df 100644 --- a/src/NadekoBot/project.json +++ b/src/NadekoBot/project.json @@ -56,7 +56,8 @@ }, "configurations": { "GlobalNadeko": { - "buildOptions": {"define": ["GLOBAL_NADEKO"]} + "buildOptions": { "define": [ "GLOBAL_NADEKO" ] }, + "compilationOptions": { "optimize": true } } } }