Adding RankCheck command

This commit is contained in:
Sean "Solao Bajiuik" Stoves 2020-08-06 20:24:15 -04:00
parent 0af2ebbd96
commit 9cdab68c64

View File

@ -0,0 +1,110 @@
using System;
using Discord;
using Discord.Commands;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text;
using Antlr4.Runtime.Misc;
using ChaosBot.Discord.PreConditions;
using ChaosBot.Lodestone;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using NLog;
namespace ChaosBot.Discord.Modules.Admin
{
public class RankCheck : ModuleBase
{
private static readonly ILogger _logger = Program.Logger;
[Command("rankCheck")]
[Alias("rc")]
[CheckCommandPerm("Admin")]
public async Task rankCheck()
{
try
{
List<LodestoneRank> ranks = await GetRank();
var sb = new StringBuilder();
var embed = new EmbedBuilder();
embed.WithColor(new Color(255, 255, 0));
embed.Title = $"Pending Promotions";
sb.AppendLine();
sb.AppendLine();
sb.AppendLine($"**Recruits Pending Promotion to Initiate**");
if(ranks.FindAll(x => x.IngameRole == ERole.Recruit).Any())
{
foreach (var lsID in ranks.FindAll(x => x.IngameRole == ERole.Recruit))
{
if ((lsID.ShouldBeRole != lsID.IngameRole) && (lsID.ShouldBeRole != null))
sb.AppendLine(string.Format("{0} {1}", lsID.DisplayName, $"linked to <@{lsID.DiscordId}>"));
}
}
else
sb.AppendLine($"None at this time.");
sb.AppendLine();
sb.AppendLine($"**Initiates Pending Promotion to Member**");
if (ranks.FindAll(x => x.IngameRole == ERole.Initiate).Any())
{
foreach (var lsID in ranks.FindAll(x => x.IngameRole == ERole.Initiate))
{
if ((lsID.ShouldBeRole != lsID.IngameRole) && (lsID.ShouldBeRole != null))
sb.AppendLine(string.Format("{0} {1}", lsID.DisplayName, $"linked to <@{lsID.DiscordId}>"));
}
}
else
sb.AppendLine($"None at this time.");
sb.AppendLine();
sb.AppendLine($"Report Generated by {Context.User.Mention} at {DateTime.Now.ToString("dddd, dd MMMM yyyy h:mm tt")}.");
/*
* 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($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
public async Task<List<LodestoneRank>> GetRank()
{
string response = null;
try
{
using (var client = new HttpClient())
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13;
var result = await client.GetAsync("https://www.ffxivhelix.com/rapi/clogiclodestone/v1/users");
result.EnsureSuccessStatusCode();
response = await result.Content.ReadAsStringAsync();
}
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
return JsonConvert.DeserializeObject<List<LodestoneRank>>(JsonConvert.SerializeObject(JsonConvert.DeserializeObject<LodestoneRankApi>(response).Data));
}
}
}