diff --git a/ChaosBot/Discord/Modules/LodestoneCommands.cs b/ChaosBot/Discord/Modules/LodestoneCommands.cs new file mode 100644 index 0000000..c55d547 --- /dev/null +++ b/ChaosBot/Discord/Modules/LodestoneCommands.cs @@ -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}]>."); + } + } + } +} \ No newline at end of file diff --git a/ChaosBot/Services/LodestoneHttpProxy.cs b/ChaosBot/Services/LodestoneHttpProxy.cs index 644f820..380b1fa 100644 --- a/ChaosBot/Services/LodestoneHttpProxy.cs +++ b/ChaosBot/Services/LodestoneHttpProxy.cs @@ -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 fetch(string endpoint, Dictionary 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}"); diff --git a/ChaosBot/Services/LodestoneManager.cs b/ChaosBot/Services/LodestoneManager.cs new file mode 100644 index 0000000..d74f0b4 --- /dev/null +++ b/ChaosBot/Services/LodestoneManager.cs @@ -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 EndpointPaths = new Dictionary + { + {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 parameters = new Dictionary + {{"name", name}, {"server", server}}; + + PaginationWrapper characterWrapper = LodestoneHttpProxy.fetch>(GetEndpointPaths(Endpoints.CHARACTER_SEARCH), parameters); + return characterWrapper.Results.First(); + } + + public static Character GetCharacter(long id) + { + PaginationWrapper characterWrapper = LodestoneHttpProxy.fetch>(GetEndpointPaths(Endpoints.CHARACTER_SEARCH_BY_ID, id.ToString())); + return characterWrapper.Results.First(); + } + + public static FreeCompany GetFreeCompany(string server, string name) + { + Dictionary parameters = new Dictionary + {{"name", name}, {"server", server}}; + + PaginationWrapper freeCompanyWrapper = LodestoneHttpProxy.fetch>(GetEndpointPaths(Endpoints.FREECOMPANY_SEARCH), parameters); + return freeCompanyWrapper.Results.First(); + } + + public static FreeCompany GetFreeCompanyById(string id) { + PaginationWrapper freeCompanyWrapper = LodestoneHttpProxy.fetch>(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 + } +} \ No newline at end of file