Use greetings.dev instead of hardcoded greetings

This commit is contained in:
runebaas 2020-06-30 17:54:01 +02:00
parent 580a514ce5
commit c94d73736d
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
4 changed files with 37 additions and 1605 deletions

View file

@ -0,0 +1,11 @@
namespace Geekbot.net.Commands.Randomness.Greetings
{
public class GreetingBaseDto
{
public string Language { get; set; }
public string LanguageNative { get; set; }
public string LanguageCode { get; set; }
public string Script { get; set; }
public GreetingDto Primary { get; set; }
}
}

View file

@ -3,8 +3,8 @@ namespace Geekbot.net.Commands.Randomness.Greetings
public class GreetingDto public class GreetingDto
{ {
public string Text { get; set; } public string Text { get; set; }
public string Language { get; set; } public string Dialect { get; set; }
public string Dialect { get; set; } = null; public string Romanization { get; set; }
public string Romanization { get; set; } = null; public string[] Use { get; set; }
} }
} }

File diff suppressed because it is too large Load diff

View file

@ -1,9 +1,12 @@
using System; using System;
using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using Geekbot.net.Commands.Randomness.Cat;
using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.ErrorHandling;
using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Extensions;
using Newtonsoft.Json;
namespace Geekbot.net.Commands.Randomness.Greetings namespace Geekbot.net.Commands.Randomness.Greetings
{ {
@ -23,20 +26,20 @@ namespace Geekbot.net.Commands.Randomness.Greetings
{ {
try try
{ {
var greeting = GreetingProvider.Greetings[new Random().Next(GreetingProvider.GreetingCount - 1)]; var greeting = await GetRandomGreeting();
var eb = new EmbedBuilder(); var eb = new EmbedBuilder();
eb.Title = greeting.Text; eb.Title = greeting.Primary.Text;
eb.AddInlineField("Language", greeting.Language); eb.AddInlineField("Language", greeting.Language);
if (greeting.Dialect != null) if (greeting.Primary.Dialect != null)
{ {
eb.AddInlineField("Dialect", greeting.Dialect); eb.AddInlineField("Dialect", greeting.Primary.Dialect);
} }
if (greeting.Romanization != null) if (greeting.Primary.Romanization != null)
{ {
eb.AddInlineField("Roman", greeting.Romanization); eb.AddInlineField("Roman", greeting.Primary.Romanization);
} }
await ReplyAsync(string.Empty, false, eb.Build()); await ReplyAsync(string.Empty, false, eb.Build());
@ -46,5 +49,18 @@ namespace Geekbot.net.Commands.Randomness.Greetings
await _errorHandler.HandleCommandException(e, Context); await _errorHandler.HandleCommandException(e, Context);
} }
} }
private async Task<GreetingBaseDto> GetRandomGreeting()
{
using var client = new HttpClient
{
BaseAddress = new Uri("https://api.greetings.dev")
};
var response = await client.GetAsync("/v1/greeting");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<GreetingBaseDto>(stringResponse);
}
} }
} }