Added remaining gateway events, added IAudioChannel, added CacheModes

This commit is contained in:
RogueException
2016-10-04 07:32:26 -03:00
parent e038475ab4
commit 4678544fed
58 changed files with 1685 additions and 855 deletions

View File

@@ -1,10 +1,56 @@
namespace Discord.WebSocket
using Model = Discord.API.User;
using System.Collections.Concurrent;
namespace Discord.WebSocket
{
internal class SocketGlobalUser : SocketUser
{
internal SocketGlobalUser(DiscordSocketClient discord, ulong id)
public override bool IsBot { get; internal set; }
public override string Username { get; internal set; }
public override ushort DiscriminatorValue { get; internal set; }
public override string AvatarId { get; internal set; }
public SocketDMChannel DMChannel { get; internal set; }
internal override SocketGlobalUser GlobalUser => this;
internal override SocketPresence Presence { get; set; }
private readonly object _lockObj = new object();
private ushort _references;
private SocketGlobalUser(DiscordSocketClient discord, ulong id)
: base(discord, id)
{
}
internal static SocketGlobalUser Create(DiscordSocketClient discord, ClientState state, Model model)
{
var entity = new SocketGlobalUser(discord, model.Id);
entity.Update(state, model);
return entity;
}
internal void AddRef()
{
checked
{
lock (_lockObj)
_references++;
}
}
internal void RemoveRef(DiscordSocketClient discord)
{
lock (_lockObj)
{
if (--_references <= 0)
discord.RemoveUser(Id);
}
}
internal new SocketGlobalUser Clone() => MemberwiseClone() as SocketGlobalUser;
//Updates are only ever called from the gateway thread, thus threadsafe
internal override void Update(ClientState state, Model model)
{
base.Update(state, model);
}
}
}

View File

@@ -1,5 +1,4 @@
using Discord.Rest;
using System.Diagnostics;
using System.Diagnostics;
using Model = Discord.API.User;
namespace Discord.WebSocket
@@ -7,17 +6,30 @@ namespace Discord.WebSocket
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class SocketGroupUser : SocketUser, IGroupUser
{
internal SocketGroupUser(DiscordSocketClient discord, ulong id)
: base(discord, id)
public SocketGroupChannel Channel { get; }
internal override SocketGlobalUser GlobalUser { get; }
public override bool IsBot { get { return GlobalUser.IsBot; } internal set { GlobalUser.IsBot = value; } }
public override string Username { get { return GlobalUser.Username; } internal set { GlobalUser.Username = value; } }
public override ushort DiscriminatorValue { get { return GlobalUser.DiscriminatorValue; } internal set { GlobalUser.DiscriminatorValue = value; } }
public override string AvatarId { get { return GlobalUser.AvatarId; } internal set { GlobalUser.AvatarId = value; } }
internal override SocketPresence Presence { get { return GlobalUser.Presence; } set { GlobalUser.Presence = value; } }
internal SocketGroupUser(SocketGroupChannel channel, SocketGlobalUser globalUser)
: base(channel.Discord, globalUser.Id)
{
Channel = channel;
GlobalUser = globalUser;
}
internal new static SocketGroupUser Create(DiscordSocketClient discord, Model model)
internal static SocketGroupUser Create(SocketGroupChannel channel, ClientState state, Model model)
{
var entity = new SocketGroupUser(discord, model.Id);
entity.Update(model);
var entity = new SocketGroupUser(channel, channel.Discord.GetOrCreateUser(state, model));
entity.Update(state, model);
return entity;
}
internal new SocketGroupUser Clone() => MemberwiseClone() as SocketGroupUser;
//IVoiceState
bool IVoiceState.IsDeafened => false;
bool IVoiceState.IsMuted => false;

View File

@@ -9,46 +9,76 @@ using PresenceModel = Discord.API.Presence;
namespace Discord.WebSocket
{
internal class SocketGuildUser : SocketUser, IGuildUser
public class SocketGuildUser : SocketUser, IGuildUser
{
private long? _joinedAtTicks;
private ImmutableArray<ulong> _roleIds;
internal override SocketGlobalUser GlobalUser { get; }
public SocketGuild Guild { get; }
public string Nickname { get; private set; }
public ulong GuildId { get; private set; }
public override bool IsBot { get { return GlobalUser.IsBot; } internal set { GlobalUser.IsBot = value; } }
public override string Username { get { return GlobalUser.Username; } internal set { GlobalUser.Username = value; } }
public override ushort DiscriminatorValue { get { return GlobalUser.DiscriminatorValue; } internal set { GlobalUser.DiscriminatorValue = value; } }
public override string AvatarId { get { return GlobalUser.AvatarId; } internal set { GlobalUser.AvatarId = value; } }
internal override SocketPresence Presence { get { return GlobalUser.Presence; } set { GlobalUser.Presence = value; } }
public IReadOnlyCollection<ulong> RoleIds => _roleIds;
public SocketVoiceState? VoiceState => Guild.GetVoiceState(Id);
public bool IsSelfDeafened => VoiceState?.IsSelfDeafened ?? false;
public bool IsSelfMuted => VoiceState?.IsSelfMuted ?? false;
public bool IsSuppressed => VoiceState?.IsSuppressed ?? false;
public SocketVoiceChannel VoiceChannel => VoiceState?.VoiceChannel;
public bool IsDeafened => VoiceState?.IsDeafened ?? false;
public bool IsMuted => VoiceState?.IsMuted ?? false;
public string VoiceSessionId => VoiceState?.VoiceSessionId ?? "";
public DateTimeOffset? JoinedAt => DateTimeUtils.FromTicks(_joinedAtTicks);
internal SocketGuildUser(DiscordSocketClient discord, ulong id)
: base(discord, id)
internal SocketGuildUser(SocketGuild guild, SocketGlobalUser globalUser)
: base(guild.Discord, globalUser.Id)
{
Guild = guild;
GlobalUser = globalUser;
}
internal static SocketGuildUser Create(DiscordSocketClient discord, Model model)
internal static SocketGuildUser Create(SocketGuild guild, ClientState state, Model model)
{
var entity = new SocketGuildUser(discord, model.User.Id);
entity.Update(model);
var entity = new SocketGuildUser(guild, guild.Discord.GetOrCreateUser(state, model.User));
entity.Update(state, model);
return entity;
}
internal void Update(Model model)
internal static SocketGuildUser Create(SocketGuild guild, ClientState state, PresenceModel model)
{
var entity = new SocketGuildUser(guild, guild.Discord.GetOrCreateUser(state, model.User));
entity.Update(state, model);
return entity;
}
internal void Update(ClientState state, Model model)
{
base.Update(state, model.User);
_joinedAtTicks = model.JoinedAt.UtcTicks;
if (model.Nick.IsSpecified)
Nickname = model.Nick.Value;
UpdateRoles(model.Roles);
}
internal override void Update(ClientState state, PresenceModel model)
{
base.Update(state, model);
if (model.Roles.IsSpecified)
UpdateRoles(model.Roles.Value);
if (model.Nick.IsSpecified)
Nickname = model.Nick.Value;
}
private void UpdateRoles(ulong[] roleIds)
{
var roles = ImmutableArray.CreateBuilder<ulong>(roleIds.Length + 1);
roles.Add(GuildId);
roles.Add(Guild.Id);
for (int i = 0; i < roleIds.Length; i++)
roles.Add(roleIds[i]);
_roleIds = roles.ToImmutable();
}
public override async Task UpdateAsync()
=> Update(await UserHelper.GetAsync(this, Discord));
public Task ModifyAsync(Action<ModifyGuildMemberParams> func)
=> UserHelper.ModifyAsync(this, Discord, func);
public Task KickAsync()
@@ -59,16 +89,17 @@ namespace Discord.WebSocket
throw new NotImplementedException(); //TODO: Impl
}
internal new SocketGuildUser Clone() => MemberwiseClone() as SocketGuildUser;
//IGuildUser
ulong IGuildUser.GuildId => Guild.Id;
IReadOnlyCollection<ulong> IGuildUser.RoleIds => RoleIds;
//IUser
Task<IDMChannel> IUser.GetDMChannelAsync(CacheMode mode)
=> Task.FromResult<IDMChannel>(GlobalUser.DMChannel);
//IVoiceState
bool IVoiceState.IsDeafened => false;
bool IVoiceState.IsMuted => false;
bool IVoiceState.IsSelfDeafened => false;
bool IVoiceState.IsSelfMuted => false;
bool IVoiceState.IsSuppressed => false;
IVoiceChannel IVoiceState.VoiceChannel => null;
string IVoiceState.VoiceSessionId => null;
IVoiceChannel IVoiceState.VoiceChannel => VoiceChannel;
}
}

View File

@@ -3,7 +3,7 @@
namespace Discord.WebSocket
{
//TODO: C#7 Candidate for record type
internal struct SocketPresence : IPresence
public struct SocketPresence : IPresence
{
public Game? Game { get; }
public UserStatus Status { get; }
@@ -13,11 +13,11 @@ namespace Discord.WebSocket
Game = game;
Status = status;
}
internal SocketPresence Create(Model model)
internal static SocketPresence Create(Model model)
{
return new SocketPresence(model.Game != null ? Discord.Game.Create(model.Game) : (Game?)null, model.Status);
}
public SocketPresence Clone() => this;
internal SocketPresence Clone() => this;
}
}

View File

@@ -11,21 +11,28 @@ namespace Discord.WebSocket
public string Email { get; private set; }
public bool IsVerified { get; private set; }
public bool IsMfaEnabled { get; private set; }
internal override SocketGlobalUser GlobalUser { get; }
internal SocketSelfUser(DiscordSocketClient discord, ulong id)
: base(discord, id)
public override bool IsBot { get { return GlobalUser.IsBot; } internal set { GlobalUser.IsBot = value; } }
public override string Username { get { return GlobalUser.Username; } internal set { GlobalUser.Username = value; } }
public override ushort DiscriminatorValue { get { return GlobalUser.DiscriminatorValue; } internal set { GlobalUser.DiscriminatorValue = value; } }
public override string AvatarId { get { return GlobalUser.AvatarId; } internal set { GlobalUser.AvatarId = value; } }
internal override SocketPresence Presence { get { return GlobalUser.Presence; } set { GlobalUser.Presence = value; } }
internal SocketSelfUser(DiscordSocketClient discord, SocketGlobalUser globalUser)
: base(discord, globalUser.Id)
{
Status = UserStatus.Online;
GlobalUser = globalUser;
}
internal new static SocketSelfUser Create(DiscordSocketClient discord, Model model)
internal static SocketSelfUser Create(DiscordSocketClient discord, ClientState state, Model model)
{
var entity = new SocketSelfUser(discord, model.Id);
entity.Update(model);
var entity = new SocketSelfUser(discord, discord.GetOrCreateSelfUser(state, model));
entity.Update(state, model);
return entity;
}
internal override void Update(Model model)
internal override void Update(ClientState state, Model model)
{
base.Update(model);
base.Update(state, model);
if (model.Email.IsSpecified)
Email = model.Email.Value;
@@ -34,12 +41,13 @@ namespace Discord.WebSocket
if (model.MfaEnabled.IsSpecified)
IsMfaEnabled = model.MfaEnabled.Value;
}
public override async Task UpdateAsync()
=> Update(await UserHelper.GetAsync(this, Discord));
public Task ModifyAsync(Action<ModifyCurrentUserParams> func)
=> UserHelper.ModifyAsync(this, Discord, func);
internal new SocketSelfUser Clone() => MemberwiseClone() as SocketSelfUser;
//ISelfUser
Task ISelfUser.ModifyStatusAsync(Action<ModifyPresenceParams> func) { throw new NotSupportedException(); }
}
}

View File

@@ -0,0 +1,34 @@
using System;
using Model = Discord.API.User;
using PresenceModel = Discord.API.Presence;
namespace Discord.WebSocket
{
public class SocketSimpleUser : SocketUser
{
public override bool IsBot { get; internal set; }
public override string Username { get; internal set; }
public override ushort DiscriminatorValue { get; internal set; }
public override string AvatarId { get; internal set; }
internal override SocketPresence Presence { get { return new SocketPresence(null, UserStatus.Offline); } set { } }
internal override SocketGlobalUser GlobalUser { get { throw new NotSupportedException(); } }
internal SocketSimpleUser(DiscordSocketClient discord, ulong id)
: base(discord, id)
{
}
internal static SocketSimpleUser Create(DiscordSocketClient discord, ClientState state, Model model)
{
var entity = new SocketSimpleUser(discord, model.Id);
entity.Update(state, model);
return entity;
}
internal override void Update(ClientState state, PresenceModel model)
{
}
internal new SocketSimpleUser Clone() => MemberwiseClone() as SocketSimpleUser;
}
}

View File

@@ -1,33 +1,30 @@
using Discord.Rest;
using System.Threading.Tasks;
using Model = Discord.API.User;
using PresenceModel = Discord.API.Presence;
namespace Discord.WebSocket
{
public class SocketUser : SocketEntity<ulong>, IUser
public abstract class SocketUser : SocketEntity<ulong>, IUser
{
public bool IsBot { get; private set; }
public string Username { get; private set; }
public ushort DiscriminatorValue { get; private set; }
public string AvatarId { get; private set; }
public abstract bool IsBot { get; internal set; }
public abstract string Username { get; internal set; }
public abstract ushort DiscriminatorValue { get; internal set; }
public abstract string AvatarId { get; internal set; }
internal abstract SocketGlobalUser GlobalUser { get; }
internal abstract SocketPresence Presence { get; set; }
public string AvatarUrl => API.CDN.GetUserAvatarUrl(Id, AvatarId);
public string Discriminator => DiscriminatorValue.ToString("D4");
public string Mention => MentionUtils.MentionUser(Id);
public virtual Game? Game => null;
public virtual UserStatus Status { get; internal set; }
public Game? Game => Presence.Game;
public UserStatus Status => Presence.Status;
internal SocketUser(DiscordSocketClient discord, ulong id)
: base(discord, id)
{
}
internal static SocketUser Create(DiscordSocketClient discord, Model model)
{
var entity = new SocketUser(discord, model.Id);
entity.Update(model);
return entity;
}
internal virtual void Update(Model model)
internal virtual void Update(ClientState state, Model model)
{
if (model.Avatar.IsSpecified)
AvatarId = model.Avatar.Value;
@@ -38,13 +35,22 @@ namespace Discord.WebSocket
if (model.Username.IsSpecified)
Username = model.Username.Value;
}
internal virtual void Update(ClientState state, PresenceModel model)
{
Presence = SocketPresence.Create(model);
}
public virtual async Task UpdateAsync()
=> Update(await UserHelper.GetAsync(this, Discord));
public Task<IDMChannel> CreateDMChannelAsync()
public Task<RestDMChannel> CreateDMChannelAsync()
=> UserHelper.CreateDMChannelAsync(this, Discord);
IDMChannel IUser.GetCachedDMChannel() => null;
public override string ToString() => $"{Username}#{Discriminator}";
private string DebuggerDisplay => $"{Username}#{Discriminator} (Id{(IsBot ? ", Bot" : "")})";
internal SocketUser Clone() => MemberwiseClone() as SocketUser;
//IUser
Task<IDMChannel> IUser.GetDMChannelAsync(CacheMode mode)
=> Task.FromResult<IDMChannel>(GlobalUser.DMChannel);
async Task<IDMChannel> IUser.CreateDMChannelAsync()
=> await CreateDMChannelAsync();
}
}

View File

@@ -47,7 +47,7 @@ namespace Discord.WebSocket
return new SocketVoiceState(voiceChannel, model.SessionId, model.SelfMute, model.SelfDeaf, model.Suppress);
}
public SocketVoiceState Clone() => this;
internal SocketVoiceState Clone() => this;
IVoiceChannel IVoiceState.VoiceChannel => VoiceChannel;
}