mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-03-07 07:26:13 +00:00
Add support for Guild Member object & included data (#279)
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
namespace DiscordChatExporter.Core.Models
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
|
||||
namespace DiscordChatExporter.Core.Models
|
||||
{
|
||||
// https://discordapp.string.IsNullOrWhiteSpace(com/developers/docs/resources/guild#guild-object
|
||||
|
||||
@@ -10,13 +15,19 @@
|
||||
|
||||
public string? IconHash { get; }
|
||||
|
||||
public IReadOnlyList<Role> Roles { get; }
|
||||
|
||||
public Dictionary<string, Member?> Members { get; }
|
||||
|
||||
public string IconUrl { get; }
|
||||
|
||||
public Guild(string id, string name, string? iconHash)
|
||||
public Guild(string id, string name, IReadOnlyList<Role> roles, string? iconHash)
|
||||
{
|
||||
Id = id;
|
||||
Name = name;
|
||||
IconHash = iconHash;
|
||||
Roles = roles;
|
||||
Members = new Dictionary<string, Member?>();
|
||||
|
||||
IconUrl = GetIconUrl(id, iconHash);
|
||||
}
|
||||
@@ -26,6 +37,16 @@
|
||||
|
||||
public partial class Guild
|
||||
{
|
||||
public static string GetUserColor(Guild guild, User user) =>
|
||||
guild.Members.GetValueOrDefault(user.Id, null)?.Roles
|
||||
?.Select(r => guild.Roles
|
||||
.Where(role => r == role.Id)
|
||||
.FirstOrDefault()
|
||||
)?.Where(r => r.Color != Color.Black)?
|
||||
.Aggregate<Role, Role?>(null, (a, b) => (a?.Position ?? 0) > b.Position? a : b)?
|
||||
.ColorAsHex ?? "";
|
||||
public static string GetUserNick(Guild guild, User user) => guild.Members.GetValueOrDefault(user.Id)?.Nick ?? user.Name;
|
||||
|
||||
public static string GetIconUrl(string id, string? iconHash)
|
||||
{
|
||||
return !string.IsNullOrWhiteSpace(iconHash)
|
||||
@@ -33,6 +54,6 @@
|
||||
: "https://cdn.discordapp.com/embed/avatars/0.png";
|
||||
}
|
||||
|
||||
public static Guild DirectMessages { get; } = new Guild("@me", "Direct Messages", null);
|
||||
public static Guild DirectMessages { get; } = new Guild("@me", "Direct Messages", Array.Empty<Role>(), null);
|
||||
}
|
||||
}
|
||||
20
DiscordChatExporter.Core.Models/Member.cs
Normal file
20
DiscordChatExporter.Core.Models/Member.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace DiscordChatExporter.Core.Models
|
||||
{
|
||||
public class Member
|
||||
{
|
||||
public string UserId { get; }
|
||||
public string? Nick { get; }
|
||||
|
||||
public IReadOnlyList<string> Roles { get; }
|
||||
|
||||
public Member(string userId, string? nick, IReadOnlyList<string> roles)
|
||||
{
|
||||
UserId = userId;
|
||||
Nick = nick;
|
||||
Roles = roles;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
namespace DiscordChatExporter.Core.Models
|
||||
|
||||
using System.Drawing;
|
||||
|
||||
namespace DiscordChatExporter.Core.Models
|
||||
{
|
||||
// https://discordapp.com/developers/docs/topics/permissions#role-object
|
||||
|
||||
@@ -8,10 +11,19 @@
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public Role(string id, string name)
|
||||
public Color Color { get; }
|
||||
|
||||
public string ColorAsHex => $"#{(Color.ToArgb() & 0xffffff):X6}";
|
||||
public string ColorAsRgb => $"{Color.R}, {Color.G}, {Color.B}";
|
||||
|
||||
public int Position { get; }
|
||||
|
||||
public Role(string id, string name, Color color, int position)
|
||||
{
|
||||
Id = id;
|
||||
Name = name;
|
||||
Color = color;
|
||||
Position = position;
|
||||
}
|
||||
|
||||
public override string ToString() => Name;
|
||||
@@ -19,6 +31,6 @@
|
||||
|
||||
public partial class Role
|
||||
{
|
||||
public static Role CreateDeletedRole(string id) => new Role(id, "deleted-role");
|
||||
public static Role CreateDeletedRole(string id) => new Role(id, "deleted-role", Color.Black, -1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user