mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2025-06-08 02:14:42 -04:00
Fix bugs in previous PR
This commit is contained in:
parent
79e43c4144
commit
ff15257d23
5 changed files with 28 additions and 24 deletions
|
@ -5,7 +5,7 @@ using System.Linq;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Models
|
namespace DiscordChatExporter.Core.Models
|
||||||
{
|
{
|
||||||
// https://discordapp.string.IsNullOrWhiteSpace(com/developers/docs/resources/guild#guild-object
|
// https://discordapp.com/developers/docs/resources/guild#guild-object
|
||||||
|
|
||||||
public partial class Guild : IHasId
|
public partial class Guild : IHasId
|
||||||
{
|
{
|
||||||
|
@ -38,21 +38,20 @@ namespace DiscordChatExporter.Core.Models
|
||||||
public partial class Guild
|
public partial class Guild
|
||||||
{
|
{
|
||||||
public static string GetUserColor(Guild guild, User user) =>
|
public static string GetUserColor(Guild guild, User user) =>
|
||||||
guild.Members.GetValueOrDefault(user.Id, null)?.Roles
|
guild.Members.GetValueOrDefault(user.Id, null)
|
||||||
?.Select(r => guild.Roles
|
?.Roles
|
||||||
.Where(role => r == role.Id)
|
.Select(r => guild.Roles.FirstOrDefault(role => r == role.Id))
|
||||||
.FirstOrDefault()
|
.Where(r => r != null)
|
||||||
)?.Where(r => r.Color != Color.Black)?
|
.Where(r => r.Color != Color.Black)
|
||||||
.Aggregate<Role, Role?>(null, (a, b) => (a?.Position ?? 0) > b.Position? a : b)?
|
.Aggregate<Role, Role?>(null, (a, b) => (a?.Position ?? 0) > b.Position ? a : b)
|
||||||
.ColorAsHex ?? "";
|
?.ColorAsHex ?? "";
|
||||||
|
|
||||||
public static string GetUserNick(Guild guild, User user) => guild.Members.GetValueOrDefault(user.Id)?.Nick ?? user.Name;
|
public static string GetUserNick(Guild guild, User user) => guild.Members.GetValueOrDefault(user.Id)?.Nick ?? user.Name;
|
||||||
|
|
||||||
public static string GetIconUrl(string id, string? iconHash)
|
public static string GetIconUrl(string id, string? iconHash) =>
|
||||||
{
|
!string.IsNullOrWhiteSpace(iconHash)
|
||||||
return !string.IsNullOrWhiteSpace(iconHash)
|
|
||||||
? $"https://cdn.discordapp.com/icons/{id}/{iconHash}.png"
|
? $"https://cdn.discordapp.com/icons/{id}/{iconHash}.png"
|
||||||
: "https://cdn.discordapp.com/embed/avatars/0.png";
|
: "https://cdn.discordapp.com/embed/avatars/0.png";
|
||||||
}
|
|
||||||
|
|
||||||
public static Guild DirectMessages { get; } = new Guild("@me", "Direct Messages", Array.Empty<Role>(), null);
|
public static Guild DirectMessages { get; } = new Guild("@me", "Direct Messages", Array.Empty<Role>(), null);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,12 @@ using System.Linq;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Models
|
namespace DiscordChatExporter.Core.Models
|
||||||
{
|
{
|
||||||
|
// https://discordapp.com/developers/docs/resources/guild#guild-member-object
|
||||||
|
|
||||||
public class Member
|
public class Member
|
||||||
{
|
{
|
||||||
public string UserId { get; }
|
public string UserId { get; }
|
||||||
|
|
||||||
public string? Nick { get; }
|
public string? Nick { get; }
|
||||||
|
|
||||||
public IReadOnlyList<string> Roles { get; }
|
public IReadOnlyList<string> Roles { get; }
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
|
using System.Drawing;
|
||||||
using System.Drawing;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Models
|
namespace DiscordChatExporter.Core.Models
|
||||||
{
|
{
|
||||||
|
@ -13,7 +12,8 @@ namespace DiscordChatExporter.Core.Models
|
||||||
|
|
||||||
public Color Color { get; }
|
public Color Color { get; }
|
||||||
|
|
||||||
public string ColorAsHex => $"#{(Color.ToArgb() & 0xffffff):X6}";
|
public string ColorAsHex => $"#{Color.ToArgb() & 0xffffff:X6}";
|
||||||
|
|
||||||
public string ColorAsRgb => $"{Color.R}, {Color.G}, {Color.B}";
|
public string ColorAsRgb => $"{Color.R}, {Color.G}, {Color.B}";
|
||||||
|
|
||||||
public int Position { get; }
|
public int Position { get; }
|
||||||
|
|
|
@ -25,7 +25,7 @@ namespace DiscordChatExporter.Core.Services
|
||||||
{
|
{
|
||||||
var userId = ParseUser(json["user"]!).Id;
|
var userId = ParseUser(json["user"]!).Id;
|
||||||
var nick = json["nick"]?.Value<string>();
|
var nick = json["nick"]?.Value<string>();
|
||||||
var roles = json["roles"]!.Select(jt => jt.Value<string>()).ToArray();
|
var roles = (json["roles"] ?? Enumerable.Empty<JToken>()).Select(j => j.Value<string>()).ToArray();
|
||||||
|
|
||||||
return new Member(userId, nick, roles);
|
return new Member(userId, nick, roles);
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ namespace DiscordChatExporter.Core.Services
|
||||||
var id = json["id"]!.Value<string>();
|
var id = json["id"]!.Value<string>();
|
||||||
var name = json["name"]!.Value<string>();
|
var name = json["name"]!.Value<string>();
|
||||||
var iconHash = json["icon"]!.Value<string>();
|
var iconHash = json["icon"]!.Value<string>();
|
||||||
var roles = json["roles"]!.Select(ParseRole).ToList();
|
var roles = (json["roles"] ?? Enumerable.Empty<JToken>()).Select(ParseRole).ToArray();
|
||||||
|
|
||||||
return new Guild(id, name, roles, iconHash);
|
return new Guild(id, name, roles, iconHash);
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,6 +45,7 @@ namespace DiscordChatExporter.Core.Services
|
||||||
{
|
{
|
||||||
return (await GetApiResponseAsync(token, route, true))!;
|
return (await GetApiResponseAsync(token, route, true))!;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<JToken?> GetApiResponseAsync(AuthToken token, string route, bool errorOnFail)
|
private async Task<JToken?> GetApiResponseAsync(AuthToken token, string route, bool errorOnFail)
|
||||||
{
|
{
|
||||||
using var response = await _httpPolicy.ExecuteAsync(async () =>
|
using var response = await _httpPolicy.ExecuteAsync(async () =>
|
||||||
|
@ -61,7 +62,7 @@ namespace DiscordChatExporter.Core.Services
|
||||||
// We throw our own exception here because default one doesn't have status code
|
// We throw our own exception here because default one doesn't have status code
|
||||||
if (!response.IsSuccessStatusCode)
|
if (!response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
if(errorOnFail) throw new HttpErrorStatusCodeException(response.StatusCode, response.ReasonPhrase);
|
if (errorOnFail) throw new HttpErrorStatusCodeException(response.StatusCode, response.ReasonPhrase);
|
||||||
else return null;
|
else return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,7 +85,7 @@ namespace DiscordChatExporter.Core.Services
|
||||||
public async Task<Member?> GetGuildMemberAsync(AuthToken token, string guildId, string userId)
|
public async Task<Member?> GetGuildMemberAsync(AuthToken token, string guildId, string userId)
|
||||||
{
|
{
|
||||||
var response = await GetApiResponseAsync(token, $"guilds/{guildId}/members/{userId}", false);
|
var response = await GetApiResponseAsync(token, $"guilds/{guildId}/members/{userId}", false);
|
||||||
if(response == null) return null;
|
if (response == null) return null;
|
||||||
var member = ParseMember(response);
|
var member = ParseMember(response);
|
||||||
|
|
||||||
return member;
|
return member;
|
||||||
|
@ -113,10 +114,11 @@ namespace DiscordChatExporter.Core.Services
|
||||||
if (!response.HasValues)
|
if (!response.HasValues)
|
||||||
yield break;
|
yield break;
|
||||||
|
|
||||||
foreach (var guild in response.Select(ParseGuild))
|
// Get full guild object
|
||||||
|
foreach (var guildId in response.Select(j => j["id"]!.Value<string>()))
|
||||||
{
|
{
|
||||||
yield return guild;
|
yield return await GetGuildAsync(token, guildId);
|
||||||
afterId = guild.Id;
|
afterId = guildId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue