Lots of model updates
This commit is contained in:
parent
265647ca7c
commit
12791a0c3e
@ -6,7 +6,7 @@ public class GuildCreateData : GuildData{
|
||||
public required uint MemberCount { get; init; }
|
||||
public required VoiceStateData[] VoiceStates { get; init; }
|
||||
public required GuildMemberDataWithUser[] Members { get; init; }
|
||||
public required ChannelData[] Channels { get; init; }
|
||||
public required GuildChannelData[] Channels { get; init; }
|
||||
|
||||
public override void OnDeserialized()
|
||||
{
|
||||
|
||||
@ -1,7 +1,11 @@
|
||||
namespace Discord.API;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
public class GuildMemberDataWithUser : GuildMemberData
|
||||
namespace Discord.API;
|
||||
|
||||
public class GuildMemberDataWithUser : GuildMemberData, IId
|
||||
{
|
||||
[JsonIgnore]
|
||||
public ulong Id => User.Id;
|
||||
public required UserData User { get; init; }
|
||||
|
||||
public override void OnDeserialized()
|
||||
|
||||
@ -2,7 +2,7 @@ using System.Text.Json.Serialization;
|
||||
|
||||
namespace Discord.API;
|
||||
|
||||
public sealed class UserData : IJsonOnDeserialized
|
||||
public sealed class UserData : IJsonOnDeserialized, IId
|
||||
{
|
||||
public required ulong Id { get; init; }
|
||||
public required string Username { get; init; }
|
||||
|
||||
@ -40,12 +40,15 @@ public abstract class DiscordCollection<D, T> : IReadOnlyDictionary<Snowflake, T
|
||||
}
|
||||
}
|
||||
|
||||
internal void UpdateSingle(D data){
|
||||
internal T UpdateSingle(D data){
|
||||
if(_dict.TryGetValue(data.Id, out T? obj)){
|
||||
obj.Update(data);
|
||||
}else{
|
||||
_dict.Add(data.Id, CreateInstance(data));
|
||||
obj = CreateInstance(data);
|
||||
_dict.Add(data.Id, obj);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
protected abstract T CreateInstance(D data);
|
||||
|
||||
@ -6,6 +6,8 @@ namespace Discord.Model;
|
||||
|
||||
public class Guild : BaseType<GuildData>
|
||||
{
|
||||
private UserCollection Users {get; }
|
||||
|
||||
public string Name { get; private set; }
|
||||
public ulong? AfkChannelId { get; private set; }
|
||||
public int AfkTimeout { get; private set; }
|
||||
@ -15,8 +17,19 @@ public class Guild : BaseType<GuildData>
|
||||
public string? Description { get; private set; }
|
||||
public int NsfwLevel { get; private set; }
|
||||
|
||||
internal Guild(GuildData data) : base(data.Id){
|
||||
public GuildChannelCollection Channels {get; }
|
||||
public GuildMemberCollection Members {get; }
|
||||
|
||||
internal Guild(GuildCreateData data, UserCollection users) : base(data.Id){
|
||||
Roles = new(data.Roles.Length);
|
||||
Users = users;
|
||||
Channels = new(data.Channels.Length);
|
||||
Channels.Update(data.Channels);
|
||||
Members = new GuildMemberCollection(data.Members.Length, users);
|
||||
Members.Update(data.Members);
|
||||
foreach(VoiceStateData vt_data in data.VoiceStates){
|
||||
Members[vt_data.UserId].UpdateVoiceState(vt_data);
|
||||
}
|
||||
Update(data);
|
||||
}
|
||||
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Reflection.Metadata.Ecma335;
|
||||
using Discord.API;
|
||||
using Serilog;
|
||||
|
||||
@ -37,4 +38,19 @@ public class GuildChannel : BaseType<GuildChannelData>
|
||||
ParentId = data.ParentId;
|
||||
Type = (ChannelType) data.Type;
|
||||
}
|
||||
|
||||
public static GuildChannel Create(GuildChannelData data)
|
||||
=> (ChannelType)data.Type switch {
|
||||
ChannelType.GuildText
|
||||
or ChannelType.GuildAnnouncement
|
||||
or ChannelType.GuildForum
|
||||
=> new GuildTextChannel(data),
|
||||
ChannelType.GuildVoice
|
||||
or ChannelType.GuildMedia
|
||||
or ChannelType.GuildStageVoice
|
||||
=> new GuildVoiceChannel(data),
|
||||
ChannelType.GuildCategory
|
||||
=> new GuildChannel(data),
|
||||
_ => throw new ArgumentException("Invalid channel type", nameof(data))
|
||||
};
|
||||
}
|
||||
|
||||
13
Discord.Model/Types/GuildChannelCollection.cs
Normal file
13
Discord.Model/Types/GuildChannelCollection.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using Discord.API;
|
||||
|
||||
namespace Discord.Model;
|
||||
|
||||
public class GuildChannelCollection : DiscordCollection<GuildChannelData, GuildChannel>
|
||||
{
|
||||
public GuildChannelCollection(int capacity) : base(capacity)
|
||||
{
|
||||
}
|
||||
|
||||
protected override GuildChannel CreateInstance(GuildChannelData data)
|
||||
=> GuildChannel.Create(data);
|
||||
}
|
||||
@ -3,27 +3,37 @@ using Discord.API;
|
||||
|
||||
namespace Discord.Model;
|
||||
|
||||
public class GuildMember
|
||||
public class GuildMember : BaseType<GuildMemberDataWithUser>
|
||||
{
|
||||
public Snowflake Id => User.Id;
|
||||
public override Snowflake Id => User.Id;
|
||||
public User User {get; }
|
||||
public string? Nick { get; private set; }
|
||||
public Snowflake[] Roles { get; private set; }
|
||||
public DateTime JoinedAt { get; private set; }
|
||||
public bool Deaf { get; private set; }
|
||||
public bool Mute { get; private set; }
|
||||
public VoiceState? VoiceState {get; private set; }
|
||||
|
||||
internal GuildMember(GuildMemberData data, User user){
|
||||
internal GuildMember(GuildMemberDataWithUser data, User user){
|
||||
User = user;
|
||||
Update(data);
|
||||
Update(data); // This double-updates in some cases, but it's mostly fine //TODO
|
||||
}
|
||||
|
||||
[MemberNotNull(nameof(Roles))]
|
||||
public void Update(GuildMemberData data){
|
||||
internal override void Update(GuildMemberDataWithUser data){
|
||||
User.Update(data.User);
|
||||
Nick = data.Nick;
|
||||
Roles = data.Roles.Select(num => (Snowflake)num).ToArray();
|
||||
JoinedAt = data.JoinedAt;
|
||||
Deaf = data.Deaf;
|
||||
Mute = data.Mute;
|
||||
}
|
||||
|
||||
internal void UpdateVoiceState(VoiceStateData data){
|
||||
if(VoiceState is null){
|
||||
VoiceState = new VoiceState(data);
|
||||
}else{
|
||||
VoiceState.Update(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
15
Discord.Model/Types/GuildMemberCollection.cs
Normal file
15
Discord.Model/Types/GuildMemberCollection.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using Discord.API;
|
||||
|
||||
namespace Discord.Model;
|
||||
|
||||
public class GuildMemberCollection : DiscordCollection<GuildMemberDataWithUser, GuildMember>
|
||||
{
|
||||
private UserCollection Users {get; }
|
||||
public GuildMemberCollection(int capacity, UserCollection users) : base(capacity)
|
||||
{
|
||||
this.Users = users;
|
||||
}
|
||||
|
||||
protected override GuildMember CreateInstance(GuildMemberDataWithUser data)
|
||||
=> new GuildMember(data, Users.UpdateSingle(data.User));
|
||||
}
|
||||
@ -3,21 +3,19 @@ using Discord.API;
|
||||
|
||||
namespace Discord.Model;
|
||||
|
||||
public class User
|
||||
public class User : BaseType<UserData>
|
||||
{
|
||||
public Snowflake Id {get;}
|
||||
public string Username {get; private set;}
|
||||
public string Discriminator {get; private set;}
|
||||
public string? GlobalName {get; private set;}
|
||||
|
||||
internal User(UserData data){
|
||||
this.Id = data.Id;
|
||||
internal User(UserData data) : base(data.Id) {
|
||||
Update(data);
|
||||
}
|
||||
|
||||
[MemberNotNull(nameof(Username))]
|
||||
[MemberNotNull(nameof(Discriminator))]
|
||||
internal void Update(UserData data){
|
||||
internal override void Update(UserData data){
|
||||
this.Username = data.Username;
|
||||
this.Discriminator = data.Discriminator;
|
||||
this.GlobalName = data.GlobalName;
|
||||
|
||||
13
Discord.Model/Types/UserCollection.cs
Normal file
13
Discord.Model/Types/UserCollection.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using Discord.API;
|
||||
|
||||
namespace Discord.Model;
|
||||
|
||||
public class UserCollection : DiscordCollection<UserData, User>
|
||||
{
|
||||
public UserCollection(int capacity) : base(capacity)
|
||||
{
|
||||
}
|
||||
|
||||
protected override User CreateInstance(UserData data)
|
||||
=> new User(data);
|
||||
}
|
||||
@ -2,10 +2,11 @@
|
||||
|
||||
namespace Discord.Model;
|
||||
|
||||
public class VoiceState
|
||||
public class VoiceState : BaseType<VoiceStateData>
|
||||
{
|
||||
public override Snowflake Id => UserId;
|
||||
public ulong? ChannelId { get; private set; }
|
||||
public ulong UserId { get; private set; }
|
||||
public Snowflake UserId { get; private set; }
|
||||
public string? SessionId { get; private set; }
|
||||
public bool Mute { get; private set; }
|
||||
public bool Deaf { get; private set; }
|
||||
@ -21,7 +22,7 @@ public class VoiceState
|
||||
Update(data);
|
||||
}
|
||||
|
||||
public void Update(VoiceStateData data){
|
||||
internal override void Update(VoiceStateData data){
|
||||
ChannelId = data.ChannelId;
|
||||
SessionId = data.SessionId;
|
||||
Mute = data.Mute;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user