Brough .wiki command to 2018 standards
This commit is contained in:
parent
13fa7bd17b
commit
86b015115a
2 changed files with 135 additions and 46 deletions
|
@ -136,11 +136,11 @@ public partial class Searches : EllieModule<SearchesService>
|
||||||
}
|
}
|
||||||
|
|
||||||
var eb = _sender.CreateEmbed()
|
var eb = _sender.CreateEmbed()
|
||||||
.WithOkColor()
|
.WithOkColor()
|
||||||
.WithTitle(GetText(strs.time_new))
|
.WithTitle(GetText(strs.time_new))
|
||||||
.WithDescription(Format.Code(data.Time.ToString(Culture)))
|
.WithDescription(Format.Code(data.Time.ToString(Culture)))
|
||||||
.AddField(GetText(strs.location), string.Join('\n', data.Address.Split(", ")), true)
|
.AddField(GetText(strs.location), string.Join('\n', data.Address.Split(", ")), true)
|
||||||
.AddField(GetText(strs.timezone), data.TimeZoneName, true);
|
.AddField(GetText(strs.timezone), data.TimeZoneName, true);
|
||||||
|
|
||||||
await Response().Embed(eb).SendAsync();
|
await Response().Embed(eb).SendAsync();
|
||||||
}
|
}
|
||||||
|
@ -162,14 +162,16 @@ public partial class Searches : EllieModule<SearchesService>
|
||||||
|
|
||||||
await Response()
|
await Response()
|
||||||
.Embed(_sender.CreateEmbed()
|
.Embed(_sender.CreateEmbed()
|
||||||
.WithOkColor()
|
.WithOkColor()
|
||||||
.WithTitle(movie.Title)
|
.WithTitle(movie.Title)
|
||||||
.WithUrl($"https://www.imdb.com/title/{movie.ImdbId}/")
|
.WithUrl($"https://www.imdb.com/title/{movie.ImdbId}/")
|
||||||
.WithDescription(movie.Plot.TrimTo(1000))
|
.WithDescription(movie.Plot.TrimTo(1000))
|
||||||
.AddField("Rating", movie.ImdbRating, true)
|
.AddField("Rating", movie.ImdbRating, true)
|
||||||
.AddField("Genre", movie.Genre, true)
|
.AddField("Genre", movie.Genre, true)
|
||||||
.AddField("Year", movie.Year, true)
|
.AddField("Year", movie.Year, true)
|
||||||
.WithImageUrl(Uri.IsWellFormedUriString(movie.Poster, UriKind.Absolute) ? movie.Poster : null))
|
.WithImageUrl(Uri.IsWellFormedUriString(movie.Poster, UriKind.Absolute)
|
||||||
|
? movie.Poster
|
||||||
|
: null))
|
||||||
.SendAsync();
|
.SendAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -244,9 +246,9 @@ public partial class Searches : EllieModule<SearchesService>
|
||||||
|
|
||||||
await Response()
|
await Response()
|
||||||
.Embed(_sender.CreateEmbed()
|
.Embed(_sender.CreateEmbed()
|
||||||
.WithOkColor()
|
.WithOkColor()
|
||||||
.AddField(GetText(strs.original_url), $"<{query}>")
|
.AddField(GetText(strs.original_url), $"<{query}>")
|
||||||
.AddField(GetText(strs.short_url), $"<{shortLink}>"))
|
.AddField(GetText(strs.short_url), $"<{shortLink}>"))
|
||||||
.SendAsync();
|
.SendAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -266,13 +268,13 @@ public partial class Searches : EllieModule<SearchesService>
|
||||||
}
|
}
|
||||||
|
|
||||||
var embed = _sender.CreateEmbed()
|
var embed = _sender.CreateEmbed()
|
||||||
.WithOkColor()
|
.WithOkColor()
|
||||||
.WithTitle(card.Name)
|
.WithTitle(card.Name)
|
||||||
.WithDescription(card.Description)
|
.WithDescription(card.Description)
|
||||||
.WithImageUrl(card.ImageUrl)
|
.WithImageUrl(card.ImageUrl)
|
||||||
.AddField(GetText(strs.store_url), card.StoreUrl, true)
|
.AddField(GetText(strs.store_url), card.StoreUrl, true)
|
||||||
.AddField(GetText(strs.cost), card.ManaCost, true)
|
.AddField(GetText(strs.cost), card.ManaCost, true)
|
||||||
.AddField(GetText(strs.types), card.Types, true);
|
.AddField(GetText(strs.types), card.Types, true);
|
||||||
|
|
||||||
await Response().Embed(embed).SendAsync();
|
await Response().Embed(embed).SendAsync();
|
||||||
}
|
}
|
||||||
|
@ -331,10 +333,10 @@ public partial class Searches : EllieModule<SearchesService>
|
||||||
{
|
{
|
||||||
var item = items[0];
|
var item = items[0];
|
||||||
return _sender.CreateEmbed()
|
return _sender.CreateEmbed()
|
||||||
.WithOkColor()
|
.WithOkColor()
|
||||||
.WithUrl(item.Permalink)
|
.WithUrl(item.Permalink)
|
||||||
.WithTitle(item.Word)
|
.WithTitle(item.Word)
|
||||||
.WithDescription(item.Definition);
|
.WithDescription(item.Definition);
|
||||||
})
|
})
|
||||||
.SendAsync();
|
.SendAsync();
|
||||||
return;
|
return;
|
||||||
|
@ -402,11 +404,11 @@ public partial class Searches : EllieModule<SearchesService>
|
||||||
{
|
{
|
||||||
var model = items.First();
|
var model = items.First();
|
||||||
var embed = _sender.CreateEmbed()
|
var embed = _sender.CreateEmbed()
|
||||||
.WithDescription(ctx.User.Mention)
|
.WithDescription(ctx.User.Mention)
|
||||||
.AddField(GetText(strs.word), model.Word, true)
|
.AddField(GetText(strs.word), model.Word, true)
|
||||||
.AddField(GetText(strs._class), model.WordType, true)
|
.AddField(GetText(strs._class), model.WordType, true)
|
||||||
.AddField(GetText(strs.definition), model.Definition)
|
.AddField(GetText(strs.definition), model.Definition)
|
||||||
.WithOkColor();
|
.WithOkColor();
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(model.Example))
|
if (!string.IsNullOrWhiteSpace(model.Example))
|
||||||
embed.AddField(GetText(strs.example), model.Example);
|
embed.AddField(GetText(strs.example), model.Example);
|
||||||
|
@ -432,22 +434,36 @@ public partial class Searches : EllieModule<SearchesService>
|
||||||
}
|
}
|
||||||
|
|
||||||
[Cmd]
|
[Cmd]
|
||||||
public async Task Wiki([Leftover] string query = null)
|
public async Task Wiki([Leftover] string query)
|
||||||
{
|
{
|
||||||
query = query?.Trim();
|
query = query?.Trim();
|
||||||
|
|
||||||
if (!await ValidateQuery(query))
|
if (!await ValidateQuery(query))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
using var http = _httpFactory.CreateClient();
|
var maybeRes = await _service.GetWikipediaPageAsync(query);
|
||||||
var result = await http.GetStringAsync(
|
if (!maybeRes.TryPickT0(out var res, out var error))
|
||||||
"https://en.wikipedia.org//w/api.php?action=query&format=json&prop=info&redirects=1&formatversion=2&inprop=url&titles="
|
{
|
||||||
+ Uri.EscapeDataString(query));
|
await HandleErrorAsync(error);
|
||||||
var data = JsonConvert.DeserializeObject<WikipediaApiModel>(result);
|
return;
|
||||||
if (data.Query.Pages[0].Missing || string.IsNullOrWhiteSpace(data.Query.Pages[0].FullUrl))
|
}
|
||||||
await Response().Error(strs.wiki_page_not_found).SendAsync();
|
|
||||||
else
|
var data = res.Data;
|
||||||
await Response().Text(data.Query.Pages[0].FullUrl).SendAsync();
|
await Response().Text(data.Url).SendAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<IUserMessage> HandleErrorAsync(ErrorType error)
|
||||||
|
{
|
||||||
|
var errorKey = error switch
|
||||||
|
{
|
||||||
|
ErrorType.ApiKeyMissing => strs.api_key_missing,
|
||||||
|
ErrorType.InvalidInput => strs.invalid_input,
|
||||||
|
ErrorType.NotFound => strs.not_found,
|
||||||
|
ErrorType.Unknown => strs.error_occured,
|
||||||
|
_ => strs.error_occured,
|
||||||
|
};
|
||||||
|
|
||||||
|
return Response().Error(errorKey).SendAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Cmd]
|
[Cmd]
|
||||||
|
@ -481,10 +497,10 @@ public partial class Searches : EllieModule<SearchesService>
|
||||||
await Response()
|
await Response()
|
||||||
.Embed(
|
.Embed(
|
||||||
_sender.CreateEmbed()
|
_sender.CreateEmbed()
|
||||||
.WithOkColor()
|
.WithOkColor()
|
||||||
.AddField("Username", usr.ToString())
|
.AddField("Username", usr.ToString())
|
||||||
.AddField("Avatar Url", avatarUrl)
|
.AddField("Avatar Url", avatarUrl)
|
||||||
.WithThumbnailUrl(avatarUrl.ToString()))
|
.WithThumbnailUrl(avatarUrl.ToString()))
|
||||||
.SendAsync();
|
.SendAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
using EllieBot.Modules.Searches.Common;
|
using EllieBot.Modules.Searches.Common;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using OneOf.Types;
|
||||||
using SixLabors.Fonts;
|
using SixLabors.Fonts;
|
||||||
using SixLabors.ImageSharp;
|
using SixLabors.ImageSharp;
|
||||||
using SixLabors.ImageSharp.Drawing.Processing;
|
using SixLabors.ImageSharp.Drawing.Processing;
|
||||||
|
@ -454,4 +455,76 @@ public class SearchesService : IEService
|
||||||
|
|
||||||
return gamesMap[key];
|
return gamesMap[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<OneOf.OneOf<WikipediaReply, ErrorType>> GetWikipediaPageAsync(string query)
|
||||||
|
{
|
||||||
|
query = query.Trim();
|
||||||
|
if (string.IsNullOrEmpty(query))
|
||||||
|
{
|
||||||
|
return ErrorType.InvalidInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = await _c.GetOrAddAsync($"wikipedia_{query}",
|
||||||
|
async _ =>
|
||||||
|
{
|
||||||
|
using var http = _httpFactory.CreateClient();
|
||||||
|
http.DefaultRequestHeaders.Clear();
|
||||||
|
|
||||||
|
return await http.GetStringAsync(
|
||||||
|
"https://en.wikipedia.org/w/api.php?action=query"
|
||||||
|
+ "&format=json"
|
||||||
|
+ "&prop=info"
|
||||||
|
+ "&redirects=1"
|
||||||
|
+ "&formatversion=2"
|
||||||
|
+ "&inprop=url"
|
||||||
|
+ "&titles="
|
||||||
|
+ Uri.EscapeDataString(query));
|
||||||
|
},
|
||||||
|
TimeSpan.FromHours(1))
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
|
var data = JsonConvert.DeserializeObject<WikipediaApiModel>(result);
|
||||||
|
|
||||||
|
if (data.Query.Pages is null || !data.Query.Pages.Any() || data.Query.Pages.First().Missing)
|
||||||
|
{
|
||||||
|
return ErrorType.NotFound;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.Information("Sending wikipedia url for: {Query}", query);
|
||||||
|
|
||||||
|
return new WikipediaReply
|
||||||
|
{
|
||||||
|
Data = new()
|
||||||
|
{
|
||||||
|
Url = data.Query.Pages[0].FullUrl,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Log.Error(ex, "Error retrieving wikipedia data for: '{Query}'", query);
|
||||||
|
|
||||||
|
return ErrorType.Unknown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum ErrorType
|
||||||
|
{
|
||||||
|
InvalidInput,
|
||||||
|
NotFound,
|
||||||
|
Unknown,
|
||||||
|
ApiKeyMissing
|
||||||
|
}
|
||||||
|
|
||||||
|
public class WikipediaReply
|
||||||
|
{
|
||||||
|
public class Info
|
||||||
|
{
|
||||||
|
public required string Url { get; init; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public required Info Data { get; init; }
|
||||||
}
|
}
|
Loading…
Reference in a new issue