NadekoBot/NadekoBot.Modules.Searches/MemegenCommands.cs

78 lines
2.4 KiB
C#
Raw Normal View History

2016-06-29 19:02:43 +00:00
using Newtonsoft.Json;
using System.Collections.Generic;
2017-02-24 16:10:44 +00:00
using System.Collections.Immutable;
2016-06-29 19:02:43 +00:00
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http;
2017-02-23 03:37:24 +00:00
using System.Text;
2017-02-24 16:10:44 +00:00
using Discord.Commands;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes;
using NadekoBot.Extensions;
2016-06-29 19:02:43 +00:00
namespace NadekoBot.Modules.Searches
2016-06-29 19:02:43 +00:00
{
public partial class Searches
2016-06-29 19:02:43 +00:00
{
2017-02-24 16:10:44 +00:00
[Group]
public class MemegenCommands : NadekoSubmodule
2016-06-29 19:02:43 +00:00
{
2017-02-24 16:10:44 +00:00
private static readonly ImmutableDictionary<char, string> _map = new Dictionary<char, string>()
{
{'?', "~q"},
{'%', "~p"},
{'#', "~h"},
{'/', "~s"},
{' ', "-"},
{'-', "--"},
{'_', "__"},
{'"', "''"}
2016-10-13 00:00:58 +00:00
2017-02-24 16:10:44 +00:00
}.ToImmutableDictionary();
2016-10-13 00:00:58 +00:00
2017-02-24 16:10:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
public async Task Memelist()
{
2017-02-24 16:10:44 +00:00
var handler = new HttpClientHandler
{
AllowAutoRedirect = false
};
2016-06-29 19:02:43 +00:00
2017-02-24 16:10:44 +00:00
using (var http = new HttpClient(handler))
{
var rawJson = await http.GetStringAsync("https://memegen.link/api/templates/").ConfigureAwait(false);
var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(rawJson)
.Select(kvp => Path.GetFileName(kvp.Value));
2017-02-23 03:37:24 +00:00
2017-02-24 16:10:44 +00:00
await Context.Channel.SendTableAsync(data, x => $"{x,-17}", 3).ConfigureAwait(false);
}
}
2017-02-23 03:37:24 +00:00
2017-02-24 16:10:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
public async Task Memegen(string meme, string topText, string botText)
2017-02-23 03:37:24 +00:00
{
2017-02-24 16:10:44 +00:00
var top = Replace(topText);
var bot = Replace(botText);
await Context.Channel.SendMessageAsync($"http://memegen.link/{meme}/{top}/{bot}.jpg")
.ConfigureAwait(false);
2017-02-23 03:37:24 +00:00
}
2017-02-24 16:10:44 +00:00
private static string Replace(string input)
{
var sb = new StringBuilder();
foreach (var c in input)
{
string tmp;
if (_map.TryGetValue(c, out tmp))
sb.Append(tmp);
else
sb.Append(c);
}
return sb.ToString();
}
2017-02-23 03:37:24 +00:00
}
2016-06-29 19:02:43 +00:00
}
2017-02-24 16:10:44 +00:00
}