Add discord commands for lodestone queries

This commit is contained in:
Daniel_I_Am 2020-06-04 21:04:39 +02:00
parent 5977159678
commit ce32f19075
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
3 changed files with 219 additions and 4 deletions

View File

@ -0,0 +1,151 @@
using System;
using Discord;
using System.Text;
using Discord.Commands;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using ChaosBot.Lodestone;
using ChaosBot.Services;
using Microsoft.Extensions.Configuration;
using NLog;
namespace ChaosBot.Discord.Modules
{
public class LodestoneCommands : ModuleBase
{
private static readonly Logger _logger = Program._logger;
[Command("lodestone character")]
public async Task GetCharacterById(long id)
{
try
{
Character character = LodestoneManager.GetCharacter(id);
var sb = new StringBuilder();
var embed = new EmbedBuilder();
embed.WithColor(new Color(255, 255,0));
embed.Title = $"Character Information";
embed.Url = $"https://na.finalfantasyxiv.com/lodestone/character/{character.ID}/";
embed.ImageUrl = character.Avatar;
sb.AppendLine($"{character.Name} ({character.Server})");
/*
* Add the string to the Embed
*/
embed.Description = sb.ToString();
/*
* Reply with the Embed created above
*/
await ReplyAsync(null, false, embed.Build());
}
catch (Exception ex)
{
_logger.Error($"InfoCommands.InfoCommand: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
[Command("lodestone character")]
public async Task GetCharacter(string server, params string[] name)
{
try
{
Character character = LodestoneManager.GetCharacter(server, string.Join(" ", name));
var sb = new StringBuilder();
var embed = new EmbedBuilder();
embed.WithColor(new Color(255, 255,0));
embed.Title = $"Character Information";
embed.Url = $"https://na.finalfantasyxiv.com/lodestone/character/{character.ID}/";
embed.ImageUrl = character.Avatar;
sb.AppendLine($"{character.Name} ({character.Server})");
/*
* Add the string to the Embed
*/
embed.Description = sb.ToString();
/*
* Reply with the Embed created above
*/
await ReplyAsync(null, false, embed.Build());
}
catch (Exception ex)
{
_logger.Error($"InfoCommands.InfoCommand: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
[Command("lodestone freecompany")]
[Alias("lodestone fc")]
public async Task GetFreeCompanyById(long id)
{
try
{
FreeCompany freeCompany = LodestoneManager.GetFreeCompanyById(id.ToString());
var sb = new StringBuilder();
var embed = new EmbedBuilder();
embed.WithColor(new Color(255, 255,0));
embed.Title = $"Free Company Information";
embed.Url = $"https://na.finalfantasyxiv.com/lodestone/freecompany/{freeCompany.ID}/";
embed.ImageUrl = freeCompany.Crest.First();
sb.AppendLine($"{freeCompany.Name} ({freeCompany.Server})");
/*
* Add the string to the Embed
*/
embed.Description = sb.ToString();
/*
* Reply with the Embed created above
*/
await ReplyAsync(null, false, embed.Build());
}
catch (Exception ex)
{
_logger.Error($"InfoCommands.InfoCommand: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
[Command("lodestone freecompany")]
[Alias("lodestone fc")]
public async Task GetFreeCompany(string server, string name)
{
try
{
FreeCompany freeCompany = LodestoneManager.GetFreeCompany(server, name);
var sb = new StringBuilder();
var embed = new EmbedBuilder();
embed.WithColor(new Color(255, 255,0));
embed.Title = $"Free Company Information";
embed.Url = $"https://na.finalfantasyxiv.com/lodestone/freecompany/{freeCompany.ID}/";
embed.ImageUrl = freeCompany.Crest.First();
sb.AppendLine($"{freeCompany.Name} ({freeCompany.Server})");
/*
* Add the string to the Embed
*/
embed.Description = sb.ToString();
/*
* Reply with the Embed created above
*/
await ReplyAsync(null, false, embed.Build());
}
catch (Exception ex)
{
_logger.Error($"InfoCommands.InfoCommand: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
}
}

View File

@ -31,15 +31,20 @@ namespace ChaosBot.Services
{
private static readonly Logger _logger = Program._logger;
static HttpClient client = new HttpClient();
private static bool firstRun = true;
public static async Task<T> fetch<T>(string endpoint, Dictionary<string, string> parameters)
{
try
{
client.BaseAddress = new Uri("https://xivapi.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
if (firstRun)
{
firstRun = false;
client.BaseAddress = new Uri("https://xivapi.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}
string queryString = String.Join("&",parameters.Select(param => $"{param.Key}={param.Value}").ToArray());
HttpResponseMessage response = await client.GetAsync($"{endpoint}?{queryString}");

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ChaosBot.Lodestone;
namespace ChaosBot.Services
{
public static class LodestoneManager
{
private static readonly Dictionary<Endpoints, string> EndpointPaths = new Dictionary<Endpoints, string>
{
{Endpoints.CHARACTER_SEARCH_BY_ID, "character/%s"},
{Endpoints.CHARACTER_SEARCH, "character/search"},
{Endpoints.FREECOMPANY_SEARCH_BY_ID, "freecompany/%s"},
{Endpoints.FREECOMPANY_SEARCH, "freecompany/search"},
};
public static string GetEndpointPaths(Endpoints endpoint, params object[] parameters)
{
return string.Format(EndpointPaths.GetValueOrDefault(endpoint, string.Empty)!, parameters);
}
public static Character GetCharacter(string server, string name)
{
Dictionary<string,string> parameters = new Dictionary<string, string>
{{"name", name}, {"server", server}};
PaginationWrapper<Character> characterWrapper = LodestoneHttpProxy.fetch<PaginationWrapper<Character>>(GetEndpointPaths(Endpoints.CHARACTER_SEARCH), parameters);
return characterWrapper.Results.First();
}
public static Character GetCharacter(long id)
{
PaginationWrapper<Character> characterWrapper = LodestoneHttpProxy.fetch<PaginationWrapper<Character>>(GetEndpointPaths(Endpoints.CHARACTER_SEARCH_BY_ID, id.ToString()));
return characterWrapper.Results.First();
}
public static FreeCompany GetFreeCompany(string server, string name)
{
Dictionary<string,string> parameters = new Dictionary<string, string>
{{"name", name}, {"server", server}};
PaginationWrapper<FreeCompany> freeCompanyWrapper = LodestoneHttpProxy.fetch<PaginationWrapper<FreeCompany>>(GetEndpointPaths(Endpoints.FREECOMPANY_SEARCH), parameters);
return freeCompanyWrapper.Results.First();
}
public static FreeCompany GetFreeCompanyById(string id) {
PaginationWrapper<FreeCompany> freeCompanyWrapper = LodestoneHttpProxy.fetch<PaginationWrapper<FreeCompany>>(GetEndpointPaths(Endpoints.FREECOMPANY_SEARCH_BY_ID, id));
return freeCompanyWrapper.Results.First();}
}
public enum Endpoints
{
CHARACTER_SEARCH_BY_ID,
CHARACTER_SEARCH,
FREECOMPANY_SEARCH_BY_ID,
FREECOMPANY_SEARCH
}
}