133 lines
5.1 KiB
C#
133 lines
5.1 KiB
C#
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.Net.Http.Headers;
|
|
using System.Text;
|
|
using ChaosBot.Discord.PreConditions;
|
|
using ChaosBot.Lodestone;
|
|
using ChaosBot.Models;
|
|
using ChaosBot.Repositories;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace ChaosBot.Discord.Modules.Admin
|
|
{
|
|
public class RankCheck : ModuleBase
|
|
{
|
|
[Command("rankCheck")]
|
|
[Alias("rc")]
|
|
[CheckCommandPerm("Admin")]
|
|
public async Task Check()
|
|
{
|
|
try
|
|
{
|
|
await Context.Channel.TriggerTypingAsync();
|
|
|
|
List<LodestoneRankedUser> rankedUsers = await GetRanksFromEndpoint(Context.Guild.Id);
|
|
|
|
Dictionary<string, List<LodestoneRankedUser>> promotionLists = new Dictionary<string, List<LodestoneRankedUser>>();
|
|
|
|
EmbedBuilder embedBuilder = new EmbedBuilder {Title = "Pending Promotions"};
|
|
|
|
if (rankedUsers.Count(u => u.ShouldBeRole != null) > 0)
|
|
{
|
|
foreach (LodestoneRankedUser rankedUser in rankedUsers)
|
|
{
|
|
try
|
|
{
|
|
// Skip users that shouldn't be upgraded
|
|
if (rankedUser.ShouldBeRole == null) continue;
|
|
if (rankedUser.ShouldBeRole == rankedUser.IngameRole) continue;
|
|
|
|
string key = $"{rankedUser.IngameRole} Pending Promotion to {rankedUser.ShouldBeRole}";
|
|
|
|
if (!promotionLists.TryGetValue(key, out List<LodestoneRankedUser> rankedUserList))
|
|
{
|
|
promotionLists.Add(key, new List<LodestoneRankedUser>());
|
|
}
|
|
|
|
if (rankedUserList == null)
|
|
{
|
|
promotionLists.Remove(key);
|
|
rankedUserList = new List<LodestoneRankedUser>();
|
|
promotionLists.Add(key, rankedUserList);
|
|
}
|
|
rankedUserList.Add(rankedUser);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LoggingFacade.Exception(ex);
|
|
await ReplyAsync($"Something went wrong checking {rankedUser.DisplayName}");
|
|
}
|
|
}
|
|
|
|
StringBuilder embedContentBuilder = new StringBuilder();
|
|
|
|
foreach (KeyValuePair<string, List<LodestoneRankedUser>> promotionList in promotionLists)
|
|
{
|
|
embedContentBuilder.AppendLine($"**{promotionList.Key}**");
|
|
foreach (LodestoneRankedUser rankedUser in promotionList.Value)
|
|
{
|
|
string line = $"{rankedUser.DisplayName}";
|
|
string suffix = "";
|
|
if (rankedUser.DiscordId != null)
|
|
suffix = $", linked to <@{rankedUser.DiscordId}>";
|
|
embedContentBuilder.AppendLine(line+suffix);
|
|
}
|
|
|
|
embedContentBuilder.AppendLine();
|
|
}
|
|
|
|
string timezoneAbbreviation = TimeZoneInfo.Local.DisplayName.Split(' ').First();
|
|
embedContentBuilder.AppendLine($"Reported Generated by <@{Context.User.Id}> at {DateTime.Now} {timezoneAbbreviation}");
|
|
|
|
embedBuilder.Description = embedContentBuilder.ToString();
|
|
}
|
|
else
|
|
{
|
|
embedBuilder.Description = "No new promotions found!";
|
|
}
|
|
|
|
await ReplyAsync(null, false, embedBuilder.Build());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LoggingFacade.Exception(ex);
|
|
}
|
|
}
|
|
|
|
public async Task<List<LodestoneRankedUser>> GetRanksFromEndpoint(ulong discordGuildId)
|
|
{
|
|
string response = null;
|
|
|
|
try
|
|
{
|
|
using HttpClient client = new HttpClient();
|
|
|
|
Configuration config = new Configuration();
|
|
string endpoint =config.GetValue<string>("Lodestone:ChaosBotApi:Url");
|
|
string apiToken = config.GetValue<string>("Lodestone:ChaosBotApi:ApiToken");
|
|
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiToken);
|
|
HttpResponseMessage result =
|
|
await client.GetAsync(endpoint);
|
|
result.EnsureSuccessStatusCode();
|
|
|
|
response = await result.Content.ReadAsStringAsync();
|
|
|
|
return JsonConvert.DeserializeObject<LodestoneRankApi>(response).Data;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LoggingFacade.Exception(ex);
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|