Fix rankcheck
This commit is contained in:
parent
c660a0e970
commit
f48fd77c77
@ -10,6 +10,7 @@ using System.Text;
|
|||||||
using ChaosBot.Discord.PreConditions;
|
using ChaosBot.Discord.PreConditions;
|
||||||
using ChaosBot.Lodestone;
|
using ChaosBot.Lodestone;
|
||||||
using ChaosBot.Models;
|
using ChaosBot.Models;
|
||||||
|
using ChaosBot.Repositories;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace ChaosBot.Discord.Modules.Admin
|
namespace ChaosBot.Discord.Modules.Admin
|
||||||
@ -24,100 +25,50 @@ namespace ChaosBot.Discord.Modules.Admin
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
await Context.Channel.TriggerTypingAsync();
|
await Context.Channel.TriggerTypingAsync();
|
||||||
|
|
||||||
List<LodestoneRank> ranks = await GetRank();
|
|
||||||
|
|
||||||
var sb = new StringBuilder();
|
List<LodestoneRankedUser> rankedUsers = await GetRanksFromEndpoint(Context.Guild.Id);
|
||||||
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**");
|
List<StringBuilder> embedContent = new List<StringBuilder>{new StringBuilder()};
|
||||||
if(ranks.FindAll(x => x.IngameRole == ERole.Recruit).Any())
|
|
||||||
|
if (rankedUsers.Count(u => u.ShouldBeRole != null) > 0)
|
||||||
{
|
{
|
||||||
foreach (var lsId in ranks.FindAll(x => x.IngameRole == ERole.Recruit))
|
foreach (LodestoneRankedUser rankedUser in rankedUsers)
|
||||||
{
|
{
|
||||||
if (DateTimeOffset.Now >= lsId.RecruitDate)
|
try
|
||||||
{
|
{
|
||||||
if(lsId.DiscordId != null)
|
// Skip users that shouldn't be upgraded
|
||||||
sb.AppendLine(string.Format("{0}{1}", lsId.DisplayName, (lsId.DiscordId != null ? $", linked to <@{lsId.DiscordId}>)" : "")));
|
if (rankedUser.ShouldBeRole == null) continue;
|
||||||
else
|
if (rankedUser.ShouldBeRole == rankedUser.IngameRole) continue;
|
||||||
{
|
|
||||||
ulong lodeId = Convert.ToUInt64(lsId.LodestoneId);
|
if (embedContent.Last().Length >= 1800)
|
||||||
using (ChaosbotContext dbContext = new ChaosbotContext())
|
embedContent.Add(new StringBuilder());
|
||||||
{
|
|
||||||
IQueryable<LodestoneCharacter> ctxlsChars = dbContext.LodestoneCharacter;
|
|
||||||
List<LodestoneCharacter> lsChar = ctxlsChars
|
|
||||||
.Where(x => x.DiscordGuildId.Equals(Context.Guild.Id))
|
|
||||||
.Where(x => x.LodestoneId.Equals(lodeId)).ToList();
|
|
||||||
|
|
||||||
if (lsChar.Any())
|
embedContent.Last().Append($"{rankedUser.IngameRole} {rankedUser.DisplayName}");
|
||||||
{
|
if (rankedUser.DiscordId != null)
|
||||||
sb.AppendLine(string.Format("{0}{1}", lsId.DisplayName, $", linked to <@{lsChar.First().DiscordUserId.ToString()}>"));
|
embedContent.Last().Append($" [<@{rankedUser.DiscordId}>]");
|
||||||
}
|
embedContent.Last().AppendLine($" -> {rankedUser.ShouldBeRole}");
|
||||||
else
|
}
|
||||||
{
|
catch (Exception ex)
|
||||||
sb.AppendLine(string.Format("{0}", lsId.DisplayName));
|
{
|
||||||
}
|
LoggingFacade.Exception(ex);
|
||||||
}
|
embedContent.Last().AppendLine($"Something went wrong checking {rankedUser.DisplayName}");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
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))
|
embedContent.Last().AppendLine("No new promotions found!");
|
||||||
{
|
|
||||||
if (DateTimeOffset.Now >= lsId.MemberDate)
|
|
||||||
{
|
|
||||||
if(lsId.DiscordId != null)
|
|
||||||
sb.AppendLine(string.Format("{0}{1}", lsId.DisplayName, (lsId.DiscordId != null ? $", linked to <@{lsId.DiscordId}>)" : "")));
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ulong lodeId = Convert.ToUInt64(lsId.LodestoneId);
|
|
||||||
using (ChaosbotContext dbContext = new ChaosbotContext())
|
|
||||||
{
|
|
||||||
IQueryable<LodestoneCharacter> ctxlsChars = dbContext.LodestoneCharacter;
|
|
||||||
List<LodestoneCharacter> lsChar = ctxlsChars
|
|
||||||
.Where(x => x.DiscordGuildId.Equals(Context.Guild.Id))
|
|
||||||
.Where(x => x.LodestoneId.Equals(lodeId)).ToList();
|
|
||||||
|
|
||||||
if (lsChar.Any())
|
|
||||||
{
|
|
||||||
sb.AppendLine(string.Format("{0}{1}", lsId.DisplayName, $", linked to <@{lsChar.First().DiscordUserId.ToString()}>"));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
sb.AppendLine(string.Format("{0}", lsId.DisplayName));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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();
|
|
||||||
|
|
||||||
/*
|
foreach (StringBuilder builder in embedContent)
|
||||||
* Reply with the Embed created above
|
{
|
||||||
*/
|
EmbedBuilder embedBuilder = new EmbedBuilder();
|
||||||
await ReplyAsync(null, false, embed.Build());
|
|
||||||
|
embedBuilder.Title = "Pending Promotions";
|
||||||
|
embedBuilder.Description = builder.ToString();
|
||||||
|
|
||||||
|
await ReplyAsync(null, false, embedBuilder.Build());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -125,28 +76,28 @@ namespace ChaosBot.Discord.Modules.Admin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<LodestoneRank>> GetRank()
|
public async Task<List<LodestoneRankedUser>> GetRanksFromEndpoint(ulong discordGuildId)
|
||||||
{
|
{
|
||||||
string response = null;
|
string response = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (var client = new HttpClient())
|
using HttpClient client = new HttpClient();
|
||||||
{
|
|
||||||
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13;
|
|
||||||
|
|
||||||
var result = await client.GetAsync("https://www.ffxivhelix.com/rapi/clogiclodestone/v1/users");
|
HttpResponseMessage result =
|
||||||
result.EnsureSuccessStatusCode();
|
await client.GetAsync(
|
||||||
|
ConfigurationRepository.GetValue<string>("Lodestone:RankCheck:Url", discordGuildId));
|
||||||
response = await result.Content.ReadAsStringAsync();
|
result.EnsureSuccessStatusCode();
|
||||||
}
|
|
||||||
|
response = await result.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
return JsonConvert.DeserializeObject<LodestoneRankApi>(response).Data;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LoggingFacade.Exception(ex);
|
LoggingFacade.Exception(ex);
|
||||||
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
return JsonConvert.DeserializeObject<List<LodestoneRank>>(JsonConvert.SerializeObject(JsonConvert.DeserializeObject<LodestoneRankApi>(response).Data));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,166 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Globalization;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
using Newtonsoft.Json.Converters;
|
|
||||||
|
|
||||||
namespace ChaosBot.Lodestone
|
|
||||||
{
|
|
||||||
public partial class LodestoneRankApi
|
|
||||||
{
|
|
||||||
[JsonProperty("success")]
|
|
||||||
public bool Success { get; set; }
|
|
||||||
|
|
||||||
[JsonProperty("error")]
|
|
||||||
public object Error { get; set; }
|
|
||||||
|
|
||||||
[JsonProperty("data")]
|
|
||||||
public List<LodestoneRank> Data { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial class LodestoneRank
|
|
||||||
{
|
|
||||||
[JsonProperty("lodestoneId")]
|
|
||||||
[JsonConverter(typeof(ParseStringConverter))]
|
|
||||||
public long LodestoneId { get; set; }
|
|
||||||
|
|
||||||
[JsonProperty("ingameRole")]
|
|
||||||
public ERole IngameRole { get; set; }
|
|
||||||
|
|
||||||
[JsonProperty("firstSeen")]
|
|
||||||
public DateTimeOffset FirstSeen { get; set; }
|
|
||||||
|
|
||||||
[JsonProperty("RecruitDate")]
|
|
||||||
public DateTimeOffset RecruitDate { get; set; }
|
|
||||||
|
|
||||||
[JsonProperty("MemberDate")]
|
|
||||||
public DateTimeOffset MemberDate { get; set; }
|
|
||||||
|
|
||||||
[JsonProperty("displayName")]
|
|
||||||
public string DisplayName { get; set; }
|
|
||||||
|
|
||||||
[JsonProperty("shouldBeRole")]
|
|
||||||
public ERole? ShouldBeRole { get; set; }
|
|
||||||
|
|
||||||
[JsonProperty("discordId")]
|
|
||||||
public string DiscordId { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum ERole { Council, Initiate, Member, Inactive, Mentor, Recruit };
|
|
||||||
|
|
||||||
public partial class LodestoneRankApi
|
|
||||||
{
|
|
||||||
public static LodestoneRankApi FromJson(string json) => JsonConvert.DeserializeObject<LodestoneRankApi>(json, ChaosBot.Lodestone.Converter.Settings);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Serialize
|
|
||||||
{
|
|
||||||
public static string ToJson(this LodestoneRankApi self) => JsonConvert.SerializeObject(self, ChaosBot.Lodestone.Converter.Settings);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static class Converter
|
|
||||||
{
|
|
||||||
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
|
|
||||||
{
|
|
||||||
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
|
|
||||||
DateParseHandling = DateParseHandling.None,
|
|
||||||
Converters =
|
|
||||||
{
|
|
||||||
ERoleConverter.Singleton,
|
|
||||||
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class ERoleConverter : JsonConverter
|
|
||||||
{
|
|
||||||
public override bool CanConvert(Type t) => t == typeof(ERole) || t == typeof(ERole?);
|
|
||||||
|
|
||||||
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
|
|
||||||
{
|
|
||||||
if (reader.TokenType == JsonToken.Null) return null;
|
|
||||||
var value = serializer.Deserialize<string>(reader);
|
|
||||||
switch (value)
|
|
||||||
{
|
|
||||||
case "Council":
|
|
||||||
return ERole.Council;
|
|
||||||
case "Initiate":
|
|
||||||
return ERole.Initiate;
|
|
||||||
case "Member":
|
|
||||||
return ERole.Member;
|
|
||||||
case "Inactive":
|
|
||||||
return ERole.Inactive;
|
|
||||||
case "Mentor":
|
|
||||||
return ERole.Mentor;
|
|
||||||
case "Recruit":
|
|
||||||
return ERole.Recruit;
|
|
||||||
}
|
|
||||||
throw new Exception("Cannot unmarshal type ERole");
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
|
|
||||||
{
|
|
||||||
if (untypedValue == null)
|
|
||||||
{
|
|
||||||
serializer.Serialize(writer, null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var value = (ERole)untypedValue;
|
|
||||||
switch (value)
|
|
||||||
{
|
|
||||||
case ERole.Council:
|
|
||||||
serializer.Serialize(writer, "Council");
|
|
||||||
return;
|
|
||||||
case ERole.Initiate:
|
|
||||||
serializer.Serialize(writer, "Initiate");
|
|
||||||
return;
|
|
||||||
case ERole.Member:
|
|
||||||
serializer.Serialize(writer, "Member");
|
|
||||||
return;
|
|
||||||
case ERole.Inactive:
|
|
||||||
serializer.Serialize(writer, "Inactive");
|
|
||||||
return;
|
|
||||||
case ERole.Mentor:
|
|
||||||
serializer.Serialize(writer, "Mentor");
|
|
||||||
return;
|
|
||||||
case ERole.Recruit:
|
|
||||||
serializer.Serialize(writer, "Recruit");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
throw new Exception("Cannot marshal type ERole");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly ERoleConverter Singleton = new ERoleConverter();
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class ParseStringConverter : JsonConverter
|
|
||||||
{
|
|
||||||
public override bool CanConvert(Type t) => t == typeof(long) || t == typeof(long?);
|
|
||||||
|
|
||||||
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
|
|
||||||
{
|
|
||||||
if (reader.TokenType == JsonToken.Null) return null;
|
|
||||||
var value = serializer.Deserialize<string>(reader);
|
|
||||||
long l;
|
|
||||||
if (Int64.TryParse(value, out l))
|
|
||||||
{
|
|
||||||
return l;
|
|
||||||
}
|
|
||||||
throw new Exception("Cannot unmarshal type long");
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
|
|
||||||
{
|
|
||||||
if (untypedValue == null)
|
|
||||||
{
|
|
||||||
serializer.Serialize(writer, null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var value = (long)untypedValue;
|
|
||||||
serializer.Serialize(writer, value.ToString());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly ParseStringConverter Singleton = new ParseStringConverter();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
79
ChaosBot/Lodestone/LodestoneRankedUser.cs
Normal file
79
ChaosBot/Lodestone/LodestoneRankedUser.cs
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Converters;
|
||||||
|
|
||||||
|
namespace ChaosBot.Lodestone
|
||||||
|
{
|
||||||
|
public class LodestoneRankApi
|
||||||
|
{
|
||||||
|
[JsonProperty("success")]
|
||||||
|
public bool Success { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("error")]
|
||||||
|
public object Error { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("data")]
|
||||||
|
public List<LodestoneRankedUser> Data { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LodestoneRankedUser
|
||||||
|
{
|
||||||
|
[JsonProperty("lodestoneId")]
|
||||||
|
[JsonConverter(typeof(ParseStringToLongConverter))]
|
||||||
|
public long LodestoneId { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("ingameRole")]
|
||||||
|
public string IngameRole { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("firstSeen")]
|
||||||
|
public DateTimeOffset FirstSeen { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("RecruitDate")]
|
||||||
|
public DateTimeOffset RecruitDate { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("MemberDate")]
|
||||||
|
public DateTimeOffset MemberDate { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("displayName")]
|
||||||
|
public string DisplayName { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("shouldBeRole")]
|
||||||
|
public string ShouldBeRole { get; set; } //TODO: Check if this still needs to be nullable
|
||||||
|
|
||||||
|
[JsonProperty("discordId")]
|
||||||
|
public string DiscordId { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class ParseStringToLongConverter : JsonConverter
|
||||||
|
{
|
||||||
|
public override bool CanConvert(Type t) => t == typeof(long) || t == typeof(long?);
|
||||||
|
|
||||||
|
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
|
||||||
|
{
|
||||||
|
if (reader.TokenType == JsonToken.Null) return null;
|
||||||
|
var value = serializer.Deserialize<string>(reader);
|
||||||
|
long l;
|
||||||
|
if (Int64.TryParse(value, out l))
|
||||||
|
{
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
throw new Exception("Cannot unmarshal type long");
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
|
||||||
|
{
|
||||||
|
if (untypedValue == null)
|
||||||
|
{
|
||||||
|
serializer.Serialize(writer, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var value = (long)untypedValue;
|
||||||
|
serializer.Serialize(writer, value.ToString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static readonly ParseStringToLongConverter Singleton = new ParseStringToLongConverter();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user