Ability to change wikipedia instance and make !wiki nicer

This commit is contained in:
runebaas 2018-04-28 17:38:45 +02:00
parent f5fd9ba017
commit b97fca787c
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
4 changed files with 47 additions and 15 deletions

View file

@ -139,6 +139,24 @@ namespace Geekbot.net.Commands
}
}
[Command("wiki", RunMode = RunMode.Async)]
[Remarks(CommandCategories.Admin)]
[Summary("Change the wikipedia instance (use lang code in xx.wikipedia.org)")]
public async Task setWikiLanguage([Summary("language")] string languageRaw)
{
try
{
var language = languageRaw.ToLower();
_redis.HashSet($"{Context.Guild.Id}:Settings", new[] {new HashEntry("WikiLang", language) });
await ReplyAsync($"Now using the {language} wikipedia");
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
[Command("lang", RunMode = RunMode.Async)]
[Remarks(CommandCategories.Admin)]
[Summary("Change the bots language")]

View file

@ -5,9 +5,9 @@ using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.Net;
using Geekbot.net.Lib;
using HtmlAgilityPack;
using StackExchange.Redis;
using WikipediaApi;
using WikipediaApi.Page;
@ -17,11 +17,13 @@ namespace Geekbot.net.Commands
{
private readonly IErrorHandler _errorHandler;
private readonly IWikipediaClient _wikipediaClient;
private readonly IDatabase _redis;
public Wikipedia(IErrorHandler errorHandler, IWikipediaClient wikipediaClient)
public Wikipedia(IErrorHandler errorHandler, IWikipediaClient wikipediaClient, IDatabase redis)
{
_errorHandler = errorHandler;
_wikipediaClient = wikipediaClient;
_redis = redis;
}
[Command("wiki", RunMode = RunMode.Async)]
@ -31,7 +33,12 @@ namespace Geekbot.net.Commands
{
try
{
var article = await _wikipediaClient.GetPreview(articleName.Replace(" ", "_"));
var wikiLang = _redis.HashGet($"{Context.Guild.Id}:Settings", "WikiLang").ToString();
if (string.IsNullOrEmpty(wikiLang))
{
wikiLang = "en";
}
var article = await _wikipediaClient.GetPreview(articleName.Replace(" ", "_"), wikiLang);
if (article.Type != PageTypes.Standard)
{
@ -58,10 +65,20 @@ namespace Geekbot.net.Commands
var eb = new EmbedBuilder
{
Title = article.Title,
Description = article.Extract,
Description = article.Description,
ImageUrl = article.Thumbnail?.Source.ToString(),
Url = article.ContentUrls.Desktop.Page.ToString()
Url = article.ContentUrls.Desktop.Page.ToString(),
Color = new Color(246,246,246),
Timestamp = article.Timestamp,
Footer = new EmbedFooterBuilder
{
Text = "Last Edit",
IconUrl = "http://icons.iconarchive.com/icons/sykonist/popular-sites/256/Wikipedia-icon.png"
}
};
eb.AddField("Description", article.Extract);
if (article.Coordinates != null) eb.AddField("Coordinates", $"{article.Coordinates.lat} Lat {article.Coordinates.lon} Lon");
await ReplyAsync("", false, eb.Build());
}
catch (HttpRequestException)
@ -86,7 +103,7 @@ namespace Geekbot.net.Commands
var split = node.InnerText.Split(',');
var title = split.First();
var desc = string.Join(",", split.Skip(1));
sb.AppendLine($"**{title}** - {desc}");
sb.AppendLine($"**{title}** -{desc}");
}
return sb.ToString();

View file

@ -5,6 +5,6 @@ namespace WikipediaApi
{
public interface IWikipediaClient
{
Task<PagePreview> GetPreview(string pageName);
Task<PagePreview> GetPreview(string pageName, string language = "en");
}
}

View file

@ -9,17 +9,14 @@ namespace WikipediaApi
public class WikipediaClient : IWikipediaClient
{
private readonly HttpClient _httpClient;
public WikipediaClient(string language = "en")
public WikipediaClient()
{
_httpClient = new HttpClient
{
BaseAddress = new Uri($"https://{language}.wikipedia.org")
};
_httpClient = new HttpClient();
}
public async Task<PagePreview> GetPreview(string pageName)
public async Task<PagePreview> GetPreview(string pageName, string language = "en")
{
var response = await _httpClient.GetAsync($"/api/rest_v1/page/summary/{pageName}");
var response = await _httpClient.GetAsync($"https://{language}.wikipedia.org/api/rest_v1/page/summary/{pageName}");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();