Concrete class prototype

This commit is contained in:
RogueException
2016-09-22 21:15:37 -03:00
parent ab42129eb9
commit 6319933ed0
394 changed files with 3648 additions and 3224 deletions

View File

@@ -0,0 +1,35 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using Model = Discord.API.Connection;
namespace Discord
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class RestConnection : IConnection
{
public string Id { get; }
public string Type { get; }
public string Name { get; }
public bool IsRevoked { get; }
public IReadOnlyCollection<ulong> IntegrationIds { get; }
internal RestConnection(string id, string type, string name, bool isRevoked, IReadOnlyCollection<ulong> integrationIds)
{
Id = id;
Type = type;
Name = name;
IsRevoked = isRevoked;
IntegrationIds = integrationIds;
}
internal static RestConnection Create(Model model)
{
return new RestConnection(model.Id, model.Type, model.Name, model.Revoked, model.Integrations.ToImmutableArray());
}
public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id}, Type = {Type}{(IsRevoked ? ", Revoked" : "")})";
}
}

View File

@@ -0,0 +1,29 @@
using System.Diagnostics;
using Model = Discord.API.User;
namespace Discord.Rest
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class RestGroupUser : RestUser, IGroupUser
{
internal RestGroupUser(DiscordRestClient discord, ulong id)
: base(discord, id)
{
}
internal new static RestGroupUser Create(DiscordRestClient discord, Model model)
{
var entity = new RestGroupUser(discord, model.Id);
entity.Update(model);
return entity;
}
//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;
}
}

View File

@@ -0,0 +1,74 @@
using Discord.API.Rest;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Threading.Tasks;
using Model = Discord.API.GuildMember;
namespace Discord.Rest
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class RestGuildUser : RestUser, IGuildUser, IUpdateable
{
private long? _joinedAtTicks;
private ImmutableArray<ulong> _roleIds;
public string Nickname { get; private set; }
public ulong GuildId { get; private set; }
public IReadOnlyCollection<ulong> RoleIds => _roleIds;
public DateTimeOffset? JoinedAt => DateTimeUtils.FromTicks(_joinedAtTicks);
internal RestGuildUser(DiscordRestClient discord, ulong id)
: base(discord, id)
{
}
internal static RestGuildUser Create(DiscordRestClient discord, Model model)
{
var entity = new RestGuildUser(discord, model.User.Id);
entity.Update(model);
return entity;
}
internal void Update(Model model)
{
_joinedAtTicks = model.JoinedAt.UtcTicks;
if (model.Nick.IsSpecified)
Nickname = model.Nick.Value;
UpdateRoles(model.Roles);
}
private void UpdateRoles(ulong[] roleIds)
{
var roles = ImmutableArray.CreateBuilder<ulong>(roleIds.Length + 1);
roles.Add(GuildId);
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()
=> UserHelper.KickAsync(this, Discord);
public ChannelPermissions GetPermissions(IGuildChannel channel)
{
throw new NotImplementedException(); //TODO: Impl
}
//IGuildUser
IReadOnlyCollection<ulong> IGuildUser.RoleIds => RoleIds;
//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;
}
}

View File

@@ -0,0 +1,45 @@
using Discord.API.Rest;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Model = Discord.API.User;
namespace Discord.Rest
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class RestSelfUser : RestUser, ISelfUser
{
public string Email { get; private set; }
public bool IsVerified { get; private set; }
public bool IsMfaEnabled { get; private set; }
internal RestSelfUser(DiscordRestClient discord, ulong id)
: base(discord, id)
{
}
internal new static RestSelfUser Create(DiscordRestClient discord, Model model)
{
var entity = new RestSelfUser(discord, model.Id);
entity.Update(model);
return entity;
}
internal override void Update(Model model)
{
base.Update(model);
if (model.Email.IsSpecified)
Email = model.Email.Value;
if (model.Verified.IsSpecified)
IsVerified = model.Verified.Value;
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);
Task ISelfUser.ModifyStatusAsync(Action<ModifyPresenceParams> func) { throw new NotSupportedException(); }
}
}

View File

@@ -0,0 +1,51 @@
using System.Diagnostics;
using System.Threading.Tasks;
using Model = Discord.API.User;
namespace Discord.Rest
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class RestUser : RestEntity<ulong>, IUser, IUpdateable
{
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 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 => UserStatus.Unknown;
internal RestUser(DiscordRestClient discord, ulong id)
: base(discord, id)
{
}
internal static RestUser Create(DiscordRestClient discord, Model model)
{
var entity = new RestUser(discord, model.Id);
entity.Update(model);
return entity;
}
internal virtual void Update(Model model)
{
if (model.Avatar.IsSpecified)
AvatarId = model.Avatar.Value;
if (model.Discriminator.IsSpecified)
DiscriminatorValue = ushort.Parse(model.Discriminator.Value);
if (model.Bot.IsSpecified)
IsBot = model.Bot.Value;
if (model.Username.IsSpecified)
Username = model.Username.Value;
}
public virtual async Task UpdateAsync()
=> Update(await UserHelper.GetAsync(this, Discord));
public Task<IDMChannel> CreateDMChannelAsync()
=> UserHelper.CreateDMChannelAsync(this, Discord);
IDMChannel IUser.GetCachedDMChannel() => null;
}
}

View File

@@ -0,0 +1,53 @@
using Discord.API.Rest;
using System.Threading.Tasks;
using Model = Discord.API.User;
using MemberModel = Discord.API.GuildMember;
using System;
namespace Discord.Rest
{
internal static class UserHelper
{
public static async Task<Model> GetAsync(IUser user, DiscordRestClient client)
{
return await client.ApiClient.GetUserAsync(user.Id);
}
public static async Task<Model> GetAsync(ISelfUser user, DiscordRestClient client)
{
var model = await client.ApiClient.GetMyUserAsync();
if (model.Id != user.Id)
throw new InvalidOperationException("Unable to update this object using a different token.");
return model;
}
public static async Task<MemberModel> GetAsync(IGuildUser user, DiscordRestClient client)
{
return await client.ApiClient.GetGuildMemberAsync(user.GuildId, user.Id);
}
public static async Task ModifyAsync(ISelfUser user, DiscordRestClient client, Action<ModifyCurrentUserParams> func)
{
if (user.Id != client.CurrentUser.Id)
throw new InvalidOperationException("Unable to modify this object using a different token.");
var args = new ModifyCurrentUserParams();
func(args);
await client.ApiClient.ModifySelfAsync(args);
}
public static async Task ModifyAsync(IGuildUser user, DiscordRestClient client, Action<ModifyGuildMemberParams> func)
{
var args = new ModifyGuildMemberParams();
func(args);
await client.ApiClient.ModifyGuildMemberAsync(user.GuildId, user.Id, args);
}
public static async Task KickAsync(IGuildUser user, DiscordRestClient client)
{
await client.ApiClient.RemoveGuildMemberAsync(user.GuildId, user.Id);
}
public static async Task<IDMChannel> CreateDMChannelAsync(IUser user, DiscordRestClient client)
{
var args = new CreateDMChannelParams(user.Id);
return RestDMChannel.Create(client, await client.ApiClient.CreateDMChannelAsync(args));
}
}
}