Added IWebhookUser and MessageSource

This commit is contained in:
RogueException
2017-03-30 23:29:30 -03:00
parent e7401eda68
commit bf0be82d15
32 changed files with 371 additions and 96 deletions

View File

@@ -91,7 +91,7 @@ namespace Discord.Rest
var guildId = (channel as IGuildChannel)?.GuildId;
var guild = guildId != null ? await (client as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).ConfigureAwait(false) : null;
var model = await client.ApiClient.GetChannelMessageAsync(channel.Id, id, options).ConfigureAwait(false);
var author = GetAuthor(client, guild, model.Author.Value);
var author = GetAuthor(client, guild, model.Author.Value, model.WebhookId.ToNullable());
return RestMessage.Create(client, channel, author, model);
}
public static IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessageChannel channel, BaseDiscordClient client,
@@ -119,7 +119,7 @@ namespace Discord.Rest
var builder = ImmutableArray.CreateBuilder<RestMessage>();
foreach (var model in models)
{
var author = GetAuthor(client, guild, model.Author.Value);
var author = GetAuthor(client, guild, model.Author.Value, model.WebhookId.ToNullable());
builder.Add(RestMessage.Create(client, channel, author, model));
}
return builder.ToImmutable();
@@ -147,7 +147,7 @@ namespace Discord.Rest
var builder = ImmutableArray.CreateBuilder<RestMessage>();
foreach (var model in models)
{
var author = GetAuthor(client, guild, model.Author.Value);
var author = GetAuthor(client, guild, model.Author.Value, model.WebhookId.ToNullable());
builder.Add(RestMessage.Create(client, channel, author, model));
}
return builder.ToImmutable();
@@ -264,13 +264,13 @@ namespace Discord.Rest
=> new TypingNotifier(client, channel, options);
//Helpers
private static IUser GetAuthor(BaseDiscordClient client, IGuild guild, UserModel model)
private static IUser GetAuthor(BaseDiscordClient client, IGuild guild, UserModel model, ulong? webhookId)
{
IUser author = null;
if (guild != null)
author = guild.GetUserAsync(model.Id, CacheMode.CacheOnly).Result;
if (author == null)
author = RestUser.Create(client, model);
author = RestUser.Create(client, guild, model, webhookId);
return author;
}
}

View File

@@ -67,7 +67,7 @@ namespace Discord.Rest
await client.ApiClient.RemovePinAsync(msg.Channel.Id, msg.Id, options).ConfigureAwait(false);
}
public static ImmutableArray<ITag> ParseTags(string text, IMessageChannel channel, IGuild guild, ImmutableArray<IUser> userMentions)
public static ImmutableArray<ITag> ParseTags(string text, IMessageChannel channel, IGuild guild, IReadOnlyCollection<IUser> userMentions)
{
var tags = ImmutableArray.CreateBuilder<ITag>();
@@ -156,5 +156,16 @@ namespace Discord.Rest
.Where(x => x != null)
.ToImmutableArray();
}
public static MessageSource GetSource(Model msg)
{
if (msg.Type != MessageType.Default)
return MessageSource.System;
else if (msg.WebhookId.IsSpecified)
return MessageSource.Webhook;
else if (msg.Author.GetValueOrDefault()?.Bot.GetValueOrDefault(false) == true)
return MessageSource.Bot;
return MessageSource.User;
}
}
}

View File

@@ -13,6 +13,7 @@ namespace Discord.Rest
public IMessageChannel Channel { get; }
public IUser Author { get; }
public MessageSource Source { get; }
public string Content { get; private set; }
@@ -26,16 +27,15 @@ namespace Discord.Rest
public virtual IReadOnlyCollection<ulong> MentionedRoleIds => ImmutableArray.Create<ulong>();
public virtual IReadOnlyCollection<RestUser> MentionedUsers => ImmutableArray.Create<RestUser>();
public virtual IReadOnlyCollection<ITag> Tags => ImmutableArray.Create<ITag>();
public virtual ulong? WebhookId => null;
public bool IsWebhook => WebhookId != null;
public DateTimeOffset Timestamp => DateTimeUtils.FromTicks(_timestampTicks);
internal RestMessage(BaseDiscordClient discord, ulong id, IMessageChannel channel, IUser author)
internal RestMessage(BaseDiscordClient discord, ulong id, IMessageChannel channel, IUser author, MessageSource source)
: base(discord, id)
{
Channel = channel;
Author = author;
Source = source;
}
internal static RestMessage Create(BaseDiscordClient discord, IMessageChannel channel, IUser author, Model model)
{

View File

@@ -9,7 +9,7 @@ namespace Discord.Rest
public MessageType Type { get; private set; }
internal RestSystemMessage(BaseDiscordClient discord, ulong id, IMessageChannel channel, IUser author)
: base(discord, id, channel, author)
: base(discord, id, channel, author, MessageSource.System)
{
}
internal new static RestSystemMessage Create(BaseDiscordClient discord, IMessageChannel channel, IUser author, Model model)

View File

@@ -13,7 +13,6 @@ namespace Discord.Rest
{
private bool _isMentioningEveryone, _isTTS, _isPinned;
private long? _editedTimestampTicks;
private ulong? _webhookId;
private ImmutableArray<Attachment> _attachments;
private ImmutableArray<Embed> _embeds;
private ImmutableArray<ITag> _tags;
@@ -21,7 +20,6 @@ namespace Discord.Rest
public override bool IsTTS => _isTTS;
public override bool IsPinned => _isPinned;
public override ulong? WebhookId => _webhookId;
public override DateTimeOffset? EditedTimestamp => DateTimeUtils.FromTicks(_editedTimestampTicks);
public override IReadOnlyCollection<Attachment> Attachments => _attachments;
public override IReadOnlyCollection<Embed> Embeds => _embeds;
@@ -31,13 +29,13 @@ namespace Discord.Rest
public override IReadOnlyCollection<ITag> Tags => _tags;
public IReadOnlyDictionary<Emoji, ReactionMetadata> Reactions => _reactions.ToDictionary(x => x.Emoji, x => new ReactionMetadata { ReactionCount = x.Count, IsMe = x.Me });
internal RestUserMessage(BaseDiscordClient discord, ulong id, IMessageChannel channel, IUser author)
: base(discord, id, channel, author)
internal RestUserMessage(BaseDiscordClient discord, ulong id, IMessageChannel channel, IUser author, MessageSource source)
: base(discord, id, channel, author, source)
{
}
internal new static RestUserMessage Create(BaseDiscordClient discord, IMessageChannel channel, IUser author, Model model)
internal static new RestUserMessage Create(BaseDiscordClient discord, IMessageChannel channel, IUser author, Model model)
{
var entity = new RestUserMessage(discord, model.Id, channel, author);
var entity = new RestUserMessage(discord, model.Id, channel, author, MessageHelper.GetSource(model));
entity.Update(model);
return entity;
}
@@ -54,8 +52,6 @@ namespace Discord.Rest
_editedTimestampTicks = model.EditedTimestamp.Value?.UtcTicks;
if (model.MentionEveryone.IsSpecified)
_isMentioningEveryone = model.MentionEveryone.Value;
if (model.WebhookId.IsSpecified)
_webhookId = model.WebhookId.Value;
if (model.Attachments.IsSpecified)
{

View File

@@ -13,21 +13,26 @@ namespace Discord.Rest
public ushort DiscriminatorValue { get; private set; }
public string AvatarId { get; private set; }
public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> CDN.GetUserAvatarUrl(Id, AvatarId, size, format);
public DateTimeOffset CreatedAt => DateTimeUtils.FromSnowflake(Id);
public string Discriminator => DiscriminatorValue.ToString("D4");
public string Mention => MentionUtils.MentionUser(Id);
public virtual Game? Game => null;
public virtual UserStatus Status => UserStatus.Offline;
public virtual bool IsWebhook => false;
internal RestUser(BaseDiscordClient discord, ulong id)
: base(discord, id)
{
}
internal static RestUser Create(BaseDiscordClient discord, Model model)
=> Create(discord, null, model, null);
internal static RestUser Create(BaseDiscordClient discord, IGuild guild, Model model, ulong? webhookId)
{
var entity = new RestUser(discord, model.Id);
RestUser entity;
if (webhookId.HasValue)
entity = new RestWebhookUser(discord, guild, model.Id, webhookId.Value);
else
entity = new RestUser(discord, model.Id);
entity.Update(model);
return entity;
}
@@ -52,6 +57,9 @@ namespace Discord.Rest
public Task<RestDMChannel> CreateDMChannelAsync(RequestOptions options = null)
=> UserHelper.CreateDMChannelAsync(this, Discord, options);
public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> CDN.GetUserAvatarUrl(Id, AvatarId, size, format);
public override string ToString() => $"{Username}#{Discriminator}";
private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})";

View File

@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Threading.Tasks;
using Model = Discord.API.User;
namespace Discord.Rest
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class RestWebhookUser : RestUser, IWebhookUser
{
public ulong WebhookId { get; }
internal IGuild Guild { get; }
public override bool IsWebhook => true;
public ulong GuildId => Guild.Id;
internal RestWebhookUser(BaseDiscordClient discord, IGuild guild, ulong id, ulong webhookId)
: base(discord, id)
{
Guild = guild;
WebhookId = webhookId;
}
internal static RestWebhookUser Create(BaseDiscordClient discord, IGuild guild, Model model, ulong webhookId)
{
var entity = new RestWebhookUser(discord, guild, model.Id, webhookId);
entity.Update(model);
return entity;
}
//IGuildUser
IGuild IGuildUser.Guild
{
get
{
if (Guild != null)
return Guild;
throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object.");
}
}
IReadOnlyCollection<ulong> IGuildUser.RoleIds => ImmutableArray.Create<ulong>();
DateTimeOffset? IGuildUser.JoinedAt => null;
string IGuildUser.Nickname => null;
GuildPermissions IGuildUser.GuildPermissions => GuildPermissions.Webhook;
ChannelPermissions IGuildUser.GetPermissions(IGuildChannel channel) => Permissions.ToChannelPerms(channel, GuildPermissions.Webhook.RawValue);
Task IGuildUser.KickAsync(RequestOptions options)
{
throw new NotSupportedException("Webhook users cannot be kicked.");
}
Task IGuildUser.ModifyAsync(Action<GuildUserProperties> func, RequestOptions options)
{
throw new NotSupportedException("Webhook users cannot be modified.");
}
Task IGuildUser.AddRoleAsync(IRole role, RequestOptions options)
{
throw new NotSupportedException("Roles are not supported on webhook users.");
}
Task IGuildUser.AddRolesAsync(IEnumerable<IRole> roles, RequestOptions options)
{
throw new NotSupportedException("Roles are not supported on webhook users.");
}
Task IGuildUser.RemoveRoleAsync(IRole role, RequestOptions options)
{
throw new NotSupportedException("Roles are not supported on webhook users.");
}
Task IGuildUser.RemoveRolesAsync(IEnumerable<IRole> roles, RequestOptions options)
{
throw new NotSupportedException("Roles are not supported on webhook users.");
}
//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;
}
}