Files
nadekobot/src/NadekoBot/Modules/Searches/MemegenCommands.cs
Kwoth d5fd6aae8e - More code cleanup and codestyle updates
- Fixed some possible nullref exceptions
- Methods signatures now have up to 3 parameters before breakaing down each parameter in a separate line
- Method invocations have the same rule, except the first parameter will be in the same line as the invocation to prevent some ugliness when passing lambas as arguments
- Applied many more codestyles
- Extensions folder fully reformatted
2021-12-26 17:28:39 +01:00

96 lines
2.9 KiB
C#

using System.Collections.Immutable;
using System.Text;
using Newtonsoft.Json;
namespace NadekoBot.Modules.Searches;
public partial class Searches
{
[Group]
public class MemegenCommands : NadekoSubmodule
{
private class MemegenTemplate
{
public string Name { get; set; }
public string Id { get; set; }
}
private static readonly ImmutableDictionary<char, string> _map = new Dictionary<char, string>()
{
{'?', "~q"},
{'%', "~p"},
{'#', "~h"},
{'/', "~s"},
{' ', "-"},
{'-', "--"},
{'_', "__"},
{'"', "''"}
}.ToImmutableDictionary();
private readonly IHttpClientFactory _httpFactory;
public MemegenCommands(IHttpClientFactory factory)
=> _httpFactory = factory;
[NadekoCommand, Aliases]
public async Task Memelist(int page = 1)
{
if (--page < 0)
return;
using var http = _httpFactory.CreateClient("memelist");
var res = await http.GetAsync("https://api.memegen.link/templates/")
.ConfigureAwait(false);
var rawJson = await res.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<List<MemegenTemplate>>(rawJson);
await ctx.SendPaginatedConfirmAsync(page, curPage =>
{
var templates = string.Empty;
foreach (var template in data.Skip(curPage * 15).Take(15))
{
templates += $"**{template.Name}:**\n key: `{template.Id}`\n";
}
var embed = _eb.Create()
.WithOkColor()
.WithDescription(templates);
return embed;
}, data.Count, 15).ConfigureAwait(false);
}
[NadekoCommand, Aliases]
public async Task Memegen(string meme, [Leftover] string memeText = null)
{
var memeUrl = $"http://api.memegen.link/{meme}";
if (!string.IsNullOrWhiteSpace(memeText))
{
var memeTextArray = memeText.Split(';');
foreach(var text in memeTextArray)
{
var newText = Replace(text);
memeUrl += $"/{newText}";
}
}
memeUrl += ".png";
await ctx.Channel.SendMessageAsync(memeUrl)
.ConfigureAwait(false);
}
private static string Replace(string input)
{
var sb = new StringBuilder();
foreach (var c in input)
{
if (_map.TryGetValue(c, out var tmp))
sb.Append(tmp);
else
sb.Append(c);
}
return sb.ToString();
}
}
}