Remove RPC from main distribution (#925)
This commit is contained in:
75
experiment/Discord.Net.Rpc/Entities/Messages/RpcMessage.cs
Normal file
75
experiment/Discord.Net.Rpc/Entities/Messages/RpcMessage.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using Discord.Rest;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Threading.Tasks;
|
||||
using Model = Discord.API.Rpc.Message;
|
||||
|
||||
namespace Discord.Rpc
|
||||
{
|
||||
public abstract class RpcMessage : RpcEntity<ulong>, IMessage
|
||||
{
|
||||
private long _timestampTicks;
|
||||
|
||||
public IMessageChannel Channel { get; }
|
||||
public RpcUser Author { get; }
|
||||
public MessageSource Source { get; }
|
||||
|
||||
public string Content { get; private set; }
|
||||
public Color AuthorColor { get; private set; }
|
||||
|
||||
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
|
||||
public virtual bool IsTTS => false;
|
||||
public virtual bool IsPinned => false;
|
||||
public virtual bool IsBlocked => false;
|
||||
public virtual DateTimeOffset? EditedTimestamp => null;
|
||||
public virtual IReadOnlyCollection<Attachment> Attachments => ImmutableArray.Create<Attachment>();
|
||||
public virtual IReadOnlyCollection<Embed> Embeds => ImmutableArray.Create<Embed>();
|
||||
public virtual IReadOnlyCollection<ulong> MentionedChannelIds => ImmutableArray.Create<ulong>();
|
||||
public virtual IReadOnlyCollection<ulong> MentionedRoleIds => ImmutableArray.Create<ulong>();
|
||||
public virtual IReadOnlyCollection<ulong> MentionedUserIds => ImmutableArray.Create<ulong>();
|
||||
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 RpcMessage(DiscordRpcClient discord, ulong id, RestVirtualMessageChannel channel, RpcUser author, MessageSource source)
|
||||
: base(discord, id)
|
||||
{
|
||||
Channel = channel;
|
||||
Author = author;
|
||||
Source = source;
|
||||
}
|
||||
internal static RpcMessage Create(DiscordRpcClient discord, ulong channelId, Model model)
|
||||
{
|
||||
//model.ChannelId is always 0, needs to be passed from the event
|
||||
if (model.Type == MessageType.Default)
|
||||
return RpcUserMessage.Create(discord, channelId, model);
|
||||
else
|
||||
return RpcSystemMessage.Create(discord, channelId, model);
|
||||
}
|
||||
internal virtual void Update(Model model)
|
||||
{
|
||||
if (model.Timestamp.IsSpecified)
|
||||
_timestampTicks = model.Timestamp.Value.UtcTicks;
|
||||
|
||||
if (model.Content.IsSpecified)
|
||||
Content = model.Content.Value;
|
||||
if (model.AuthorColor.IsSpecified)
|
||||
AuthorColor = new Color(Convert.ToUInt32(model.AuthorColor.Value.Substring(1), 16));
|
||||
}
|
||||
|
||||
public Task DeleteAsync(RequestOptions options = null)
|
||||
=> MessageHelper.DeleteAsync(this, Discord, options);
|
||||
|
||||
public override string ToString() => Content;
|
||||
|
||||
//IMessage
|
||||
IMessageChannel IMessage.Channel => Channel;
|
||||
MessageType IMessage.Type => MessageType.Default;
|
||||
IUser IMessage.Author => Author;
|
||||
IReadOnlyCollection<IAttachment> IMessage.Attachments => Attachments;
|
||||
IReadOnlyCollection<IEmbed> IMessage.Embeds => Embeds;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using Discord.Rest;
|
||||
using System.Diagnostics;
|
||||
using Model = Discord.API.Rpc.Message;
|
||||
|
||||
namespace Discord.Rpc
|
||||
{
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
public class RpcSystemMessage : RpcMessage, ISystemMessage
|
||||
{
|
||||
public MessageType Type { get; private set; }
|
||||
|
||||
internal RpcSystemMessage(DiscordRpcClient discord, ulong id, RestVirtualMessageChannel channel, RpcUser author)
|
||||
: base(discord, id, channel, author, MessageSource.System)
|
||||
{
|
||||
}
|
||||
internal new static RpcSystemMessage Create(DiscordRpcClient discord, ulong channelId, Model model)
|
||||
{
|
||||
var entity = new RpcSystemMessage(discord, model.Id,
|
||||
RestVirtualMessageChannel.Create(discord, channelId),
|
||||
RpcUser.Create(discord, model.Author.Value, model.WebhookId.ToNullable()));
|
||||
entity.Update(model);
|
||||
return entity;
|
||||
}
|
||||
internal override void Update(Model model)
|
||||
{
|
||||
base.Update(model);
|
||||
|
||||
Type = model.Type;
|
||||
}
|
||||
|
||||
private string DebuggerDisplay => $"{Author}: {Content} ({Id}, {Type})";
|
||||
}
|
||||
}
|
||||
128
experiment/Discord.Net.Rpc/Entities/Messages/RpcUserMessage.cs
Normal file
128
experiment/Discord.Net.Rpc/Entities/Messages/RpcUserMessage.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
using Discord.Rest;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using Model = Discord.API.Rpc.Message;
|
||||
|
||||
namespace Discord.Rpc
|
||||
{
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
public class RpcUserMessage : RpcMessage, IUserMessage
|
||||
{
|
||||
private bool _isMentioningEveryone, _isTTS, _isPinned, _isBlocked;
|
||||
private long? _editedTimestampTicks;
|
||||
private ulong? _webhookId;
|
||||
private ImmutableArray<Attachment> _attachments;
|
||||
private ImmutableArray<Embed> _embeds;
|
||||
private ImmutableArray<ITag> _tags;
|
||||
|
||||
public override bool IsTTS => _isTTS;
|
||||
public override bool IsPinned => _isPinned;
|
||||
public override bool IsBlocked => _isBlocked;
|
||||
public override ulong? WebhookId => _webhookId;
|
||||
public override DateTimeOffset? EditedTimestamp => DateTimeUtils.FromTicks(_editedTimestampTicks);
|
||||
public override IReadOnlyCollection<Attachment> Attachments => _attachments;
|
||||
public override IReadOnlyCollection<Embed> Embeds => _embeds;
|
||||
public override IReadOnlyCollection<ulong> MentionedChannelIds => MessageHelper.FilterTagsByKey(TagType.ChannelMention, _tags);
|
||||
public override IReadOnlyCollection<ulong> MentionedRoleIds => MessageHelper.FilterTagsByKey(TagType.RoleMention, _tags);
|
||||
public override IReadOnlyCollection<ulong> MentionedUserIds => MessageHelper.FilterTagsByKey(TagType.UserMention, _tags);
|
||||
public override IReadOnlyCollection<ITag> Tags => _tags;
|
||||
public IReadOnlyDictionary<IEmote, ReactionMetadata> Reactions => ImmutableDictionary.Create<IEmote, ReactionMetadata>();
|
||||
|
||||
internal RpcUserMessage(DiscordRpcClient discord, ulong id, RestVirtualMessageChannel channel, RpcUser author, MessageSource source)
|
||||
: base(discord, id, channel, author, source)
|
||||
{
|
||||
}
|
||||
internal new static RpcUserMessage Create(DiscordRpcClient discord, ulong channelId, Model model)
|
||||
{
|
||||
var entity = new RpcUserMessage(discord, model.Id,
|
||||
RestVirtualMessageChannel.Create(discord, channelId),
|
||||
RpcUser.Create(discord, model.Author.Value, model.WebhookId.ToNullable()),
|
||||
MessageHelper.GetSource(model));
|
||||
entity.Update(model);
|
||||
return entity;
|
||||
}
|
||||
|
||||
internal override void Update(Model model)
|
||||
{
|
||||
base.Update(model);
|
||||
|
||||
if (model.IsTextToSpeech.IsSpecified)
|
||||
_isTTS = model.IsTextToSpeech.Value;
|
||||
if (model.Pinned.IsSpecified)
|
||||
_isPinned = model.Pinned.Value;
|
||||
if (model.EditedTimestamp.IsSpecified)
|
||||
_editedTimestampTicks = model.EditedTimestamp.Value?.UtcTicks;
|
||||
if (model.MentionEveryone.IsSpecified)
|
||||
_isMentioningEveryone = model.MentionEveryone.Value;
|
||||
if (model.WebhookId.IsSpecified)
|
||||
_webhookId = model.WebhookId.Value;
|
||||
|
||||
if (model.IsBlocked.IsSpecified)
|
||||
_isBlocked = model.IsBlocked.Value;
|
||||
|
||||
if (model.Attachments.IsSpecified)
|
||||
{
|
||||
var value = model.Attachments.Value;
|
||||
if (value.Length > 0)
|
||||
{
|
||||
var attachments = ImmutableArray.CreateBuilder<Attachment>(value.Length);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
attachments.Add(Attachment.Create(value[i]));
|
||||
_attachments = attachments.ToImmutable();
|
||||
}
|
||||
else
|
||||
_attachments = ImmutableArray.Create<Attachment>();
|
||||
}
|
||||
|
||||
if (model.Embeds.IsSpecified)
|
||||
{
|
||||
var value = model.Embeds.Value;
|
||||
if (value.Length > 0)
|
||||
{
|
||||
var embeds = ImmutableArray.CreateBuilder<Embed>(value.Length);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
embeds.Add(value[i].ToEntity());
|
||||
_embeds = embeds.ToImmutable();
|
||||
}
|
||||
else
|
||||
_embeds = ImmutableArray.Create<Embed>();
|
||||
}
|
||||
|
||||
if (model.Content.IsSpecified)
|
||||
{
|
||||
var text = model.Content.Value;
|
||||
_tags = MessageHelper.ParseTags(text, null, null, ImmutableArray.Create<IUser>());
|
||||
model.Content = text;
|
||||
}
|
||||
}
|
||||
|
||||
public Task ModifyAsync(Action<MessageProperties> func, RequestOptions options)
|
||||
=> MessageHelper.ModifyAsync(this, Discord, func, options);
|
||||
|
||||
public Task AddReactionAsync(IEmote emote, RequestOptions options = null)
|
||||
=> MessageHelper.AddReactionAsync(this, emote, Discord, options);
|
||||
public Task RemoveReactionAsync(IEmote emote, IUser user, RequestOptions options = null)
|
||||
=> MessageHelper.RemoveReactionAsync(this, user, emote, Discord, options);
|
||||
public Task RemoveAllReactionsAsync(RequestOptions options = null)
|
||||
=> MessageHelper.RemoveAllReactionsAsync(this, Discord, options);
|
||||
public Task<IReadOnlyCollection<IUser>> GetReactionUsersAsync(IEmote emote, int limit = 100, ulong? afterUserId = null, RequestOptions options = null)
|
||||
=> MessageHelper.GetReactionUsersAsync(this, emote, x => { x.Limit = limit; x.AfterUserId = afterUserId ?? Optional.Create<ulong>(); }, Discord, options);
|
||||
|
||||
public Task PinAsync(RequestOptions options)
|
||||
=> MessageHelper.PinAsync(this, Discord, options);
|
||||
public Task UnpinAsync(RequestOptions options)
|
||||
=> MessageHelper.UnpinAsync(this, Discord, options);
|
||||
|
||||
public string Resolve(int startIndex, TagHandling userHandling = TagHandling.Name, TagHandling channelHandling = TagHandling.Name,
|
||||
TagHandling roleHandling = TagHandling.Name, TagHandling everyoneHandling = TagHandling.Ignore, TagHandling emojiHandling = TagHandling.Name)
|
||||
=> MentionUtils.Resolve(this, startIndex, userHandling, channelHandling, roleHandling, everyoneHandling, emojiHandling);
|
||||
public string Resolve(TagHandling userHandling = TagHandling.Name, TagHandling channelHandling = TagHandling.Name,
|
||||
TagHandling roleHandling = TagHandling.Name, TagHandling everyoneHandling = TagHandling.Ignore, TagHandling emojiHandling = TagHandling.Name)
|
||||
=> MentionUtils.Resolve(this, 0, userHandling, channelHandling, roleHandling, everyoneHandling, emojiHandling);
|
||||
|
||||
private string DebuggerDisplay => $"{Author}: {Content} ({Id}{(Attachments.Count > 0 ? $", {Attachments.Count} Attachments" : "")})";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user