Added RPC Subscription funcs and Message Events
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
namespace Discord.API.Rpc
|
||||
{
|
||||
public class MessageEvent
|
||||
{
|
||||
[JsonProperty("channel_id")]
|
||||
public ulong ChannelId { get; set; }
|
||||
[JsonProperty("message")]
|
||||
public Message Message { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,13 +40,10 @@ namespace Discord
|
||||
Channel = channel;
|
||||
Author = author;
|
||||
Type = model.Type;
|
||||
|
||||
if (channel is IGuildChannel)
|
||||
{
|
||||
MentionedUsers = ImmutableArray.Create<IUser>();
|
||||
MentionedChannelIds = ImmutableArray.Create<ulong>();
|
||||
MentionedRoles = ImmutableArray.Create<IRole>();
|
||||
}
|
||||
|
||||
MentionedUsers = ImmutableArray.Create<IUser>();
|
||||
MentionedChannelIds = ImmutableArray.Create<ulong>();
|
||||
MentionedRoles = ImmutableArray.Create<IRole>();
|
||||
|
||||
Update(model, UpdateSource.Creation);
|
||||
}
|
||||
|
||||
@@ -42,23 +42,23 @@ namespace Discord.Rpc
|
||||
private readonly AsyncEvent<Func<Task>> _voiceStateUpdatedEvent = new AsyncEvent<Func<Task>>();
|
||||
|
||||
//Messages
|
||||
public event Func<Task> MessageReceived
|
||||
public event Func<ulong, IMessage, Task> MessageReceived
|
||||
{
|
||||
add { _messageReceivedEvent.Add(value); }
|
||||
remove { _messageReceivedEvent.Remove(value); }
|
||||
}
|
||||
private readonly AsyncEvent<Func<Task>> _messageReceivedEvent = new AsyncEvent<Func<Task>>();
|
||||
public event Func<Task> MessageUpdated
|
||||
private readonly AsyncEvent<Func<ulong, IMessage, Task>> _messageReceivedEvent = new AsyncEvent<Func<ulong, IMessage, Task>>();
|
||||
public event Func<ulong, IMessage, Task> MessageUpdated
|
||||
{
|
||||
add { _messageUpdatedEvent.Add(value); }
|
||||
remove { _messageUpdatedEvent.Remove(value); }
|
||||
}
|
||||
private readonly AsyncEvent<Func<Task>> _messageUpdatedEvent = new AsyncEvent<Func<Task>>();
|
||||
public event Func<Task> MessageDeleted
|
||||
private readonly AsyncEvent<Func<ulong, IMessage, Task>> _messageUpdatedEvent = new AsyncEvent<Func<ulong, IMessage, Task>>();
|
||||
public event Func<ulong, ulong, Task> MessageDeleted
|
||||
{
|
||||
add { _messageDeletedEvent.Add(value); }
|
||||
remove { _messageDeletedEvent.Remove(value); }
|
||||
}
|
||||
private readonly AsyncEvent<Func<Task>> _messageDeletedEvent = new AsyncEvent<Func<Task>>();
|
||||
private readonly AsyncEvent<Func<ulong, ulong, Task>> _messageDeletedEvent = new AsyncEvent<Func<ulong, ulong, Task>>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,6 +213,57 @@ namespace Discord.Rpc
|
||||
return result.Code;
|
||||
}
|
||||
|
||||
public async Task SubscribeGuild(ulong guildId, params RpcChannelEvent[] events)
|
||||
{
|
||||
Preconditions.AtLeast(events?.Length ?? 0, 1, nameof(events));
|
||||
for (int i = 0; i < events.Length; i++)
|
||||
await ApiClient.SendGuildSubscribeAsync(GetEventName(events[i]), guildId);
|
||||
}
|
||||
public async Task UnsubscribeGuild(ulong guildId, params RpcChannelEvent[] events)
|
||||
{
|
||||
Preconditions.AtLeast(events?.Length ?? 0, 1, nameof(events));
|
||||
for (int i = 0; i < events.Length; i++)
|
||||
await ApiClient.SendGuildUnsubscribeAsync(GetEventName(events[i]), guildId);
|
||||
}
|
||||
public async Task SubscribeChannel(ulong channelId, params RpcChannelEvent[] events)
|
||||
{
|
||||
Preconditions.AtLeast(events?.Length ?? 0, 1, nameof(events));
|
||||
for (int i = 0; i < events.Length; i++)
|
||||
await ApiClient.SendChannelSubscribeAsync(GetEventName(events[i]), channelId);
|
||||
}
|
||||
public async Task UnsubscribeChannel(ulong channelId, params RpcChannelEvent[] events)
|
||||
{
|
||||
Preconditions.AtLeast(events?.Length ?? 0, 1, nameof(events));
|
||||
for (int i = 0; i < events.Length; i++)
|
||||
await ApiClient.SendChannelUnsubscribeAsync(GetEventName(events[i]), channelId);
|
||||
}
|
||||
|
||||
private static string GetEventName(RpcGuildEvent rpcEvent)
|
||||
{
|
||||
switch (rpcEvent)
|
||||
{
|
||||
case RpcGuildEvent.GuildStatus: return "GUILD_STATUS";
|
||||
default:
|
||||
throw new InvalidOperationException($"Unknown RPC Guild Event: {rpcEvent}");
|
||||
}
|
||||
}
|
||||
private static string GetEventName(RpcChannelEvent rpcEvent)
|
||||
{
|
||||
switch (rpcEvent)
|
||||
{
|
||||
case RpcChannelEvent.VoiceStateCreate: return "VOICE_STATE_CREATE";
|
||||
case RpcChannelEvent.VoiceStateUpdate: return "VOICE_STATE_UPDATE";
|
||||
case RpcChannelEvent.VoiceStateDelete: return "VOICE_STATE_DELETE";
|
||||
case RpcChannelEvent.SpeakingStart: return "SPEAKING_START";
|
||||
case RpcChannelEvent.SpeakingStop: return "SPEAKING_STOP";
|
||||
case RpcChannelEvent.MessageCreate: return "MESSAGE_CREATE";
|
||||
case RpcChannelEvent.MessageUpdate: return "MESSAGE_UPDATE";
|
||||
case RpcChannelEvent.MessageDelete: return "MESSAGE_DELETE";
|
||||
default:
|
||||
throw new InvalidOperationException($"Unknown RPC Channel Event: {rpcEvent}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ProcessMessageAsync(string cmd, Optional<string> evnt, Optional<object> payload)
|
||||
{
|
||||
try
|
||||
@@ -291,17 +342,22 @@ namespace Discord.Rpc
|
||||
//Messages
|
||||
case "MESSAGE_CREATE":
|
||||
{
|
||||
await _messageReceivedEvent.InvokeAsync().ConfigureAwait(false);
|
||||
var data = (payload.Value as JToken).ToObject<MessageEvent>(_serializer);
|
||||
var msg = new Message(null, new User(data.Message.Author.Value), data.Message);
|
||||
await _messageReceivedEvent.InvokeAsync(data.ChannelId, msg).ConfigureAwait(false);
|
||||
}
|
||||
break;
|
||||
case "MESSAGE_UPDATE":
|
||||
{
|
||||
await _messageUpdatedEvent.InvokeAsync().ConfigureAwait(false);
|
||||
var data = (payload.Value as JToken).ToObject<MessageEvent>(_serializer);
|
||||
var msg = new Message(null, new User(data.Message.Author.Value), data.Message);
|
||||
await _messageUpdatedEvent.InvokeAsync(data.ChannelId, msg).ConfigureAwait(false);
|
||||
}
|
||||
break;
|
||||
case "MESSAGE_DELETE":
|
||||
{
|
||||
await _messageDeletedEvent.InvokeAsync().ConfigureAwait(false);
|
||||
var data = (payload.Value as JToken).ToObject<MessageEvent>(_serializer);
|
||||
await _messageDeletedEvent.InvokeAsync(data.ChannelId, data.Message.Id).ConfigureAwait(false);
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
14
src/Discord.Net/Rpc/RpcChannelEvent.cs
Normal file
14
src/Discord.Net/Rpc/RpcChannelEvent.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace Discord.Rpc
|
||||
{
|
||||
public enum RpcChannelEvent
|
||||
{
|
||||
VoiceStateCreate,
|
||||
VoiceStateUpdate,
|
||||
VoiceStateDelete,
|
||||
SpeakingStart,
|
||||
SpeakingStop,
|
||||
MessageCreate,
|
||||
MessageUpdate,
|
||||
MessageDelete
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Discord.Rpc
|
||||
{
|
||||
public enum RpcEvent
|
||||
{
|
||||
}
|
||||
}
|
||||
7
src/Discord.Net/Rpc/RpcGuildEvent.cs
Normal file
7
src/Discord.Net/Rpc/RpcGuildEvent.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Discord.Rpc
|
||||
{
|
||||
public enum RpcGuildEvent
|
||||
{
|
||||
GuildStatus
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user