Started converting websocket and rpc classes
This commit is contained in:
45
src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs
Normal file
45
src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Model = Discord.API.Channel;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
public abstract class SocketChannel : SocketEntity<ulong>, IChannel
|
||||
{
|
||||
internal SocketChannel(DiscordSocketClient discord, ulong id)
|
||||
: base(discord, id)
|
||||
{
|
||||
}
|
||||
internal static SocketChannel Create(DiscordSocketClient discord, Model model)
|
||||
{
|
||||
switch (model.Type)
|
||||
{
|
||||
case ChannelType.Text:
|
||||
return SocketTextChannel.Create(discord, model);
|
||||
case ChannelType.Voice:
|
||||
return SocketVoiceChannel.Create(discord, model);
|
||||
case ChannelType.DM:
|
||||
return SocketDMChannel.Create(discord, model);
|
||||
case ChannelType.Group:
|
||||
return SocketGroupChannel.Create(discord, model);
|
||||
default:
|
||||
throw new InvalidOperationException($"Unexpected channel type: {model.Type}");
|
||||
}
|
||||
}
|
||||
|
||||
//IChannel
|
||||
IReadOnlyCollection<IUser> IChannel.CachedUsers => ImmutableArray.Create<IUser>();
|
||||
|
||||
IUser IChannel.GetCachedUser(ulong id)
|
||||
=> null;
|
||||
Task<IUser> IChannel.GetUserAsync(ulong id)
|
||||
=> Task.FromResult<IUser>(null);
|
||||
IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync()
|
||||
=> ImmutableArray.Create<IReadOnlyCollection<IUser>>().ToAsyncEnumerable();
|
||||
}
|
||||
}
|
||||
@@ -1,77 +1,138 @@
|
||||
using System.Collections.Generic;
|
||||
using Discord.Rest;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MessageModel = Discord.API.Message;
|
||||
using Model = Discord.API.Channel;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
internal class DMChannel : IDMChannel, ISocketChannel, ISocketMessageChannel, ISocketPrivateChannel
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
public class SocketDMChannel : SocketChannel, IDMChannel
|
||||
{
|
||||
private readonly MessageManager _messages;
|
||||
private readonly MessageCache _messages;
|
||||
|
||||
public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
|
||||
public new SocketDMUser Recipient => base.Recipient as SocketDMUser;
|
||||
public IReadOnlyCollection<ISocketUser> Users => ImmutableArray.Create<ISocketUser>(Discord.CurrentUser, Recipient);
|
||||
IReadOnlyCollection<ISocketUser> ISocketPrivateChannel.Recipients => ImmutableArray.Create(Recipient);
|
||||
public SocketUser Recipient { get; private set; }
|
||||
|
||||
public SocketDMChannel(DiscordSocketClient discord, SocketDMUser recipient, Model model)
|
||||
: base(discord, recipient, model)
|
||||
public IReadOnlyCollection<SocketUser> Users => ImmutableArray.Create(Discord.CurrentUser, Recipient);
|
||||
|
||||
internal SocketDMChannel(DiscordSocketClient discord, ulong id, ulong recipientId)
|
||||
: base(discord, id)
|
||||
{
|
||||
Recipient = new SocketUser(Discord, recipientId);
|
||||
if (Discord.MessageCacheSize > 0)
|
||||
_messages = new MessageCache(Discord, this);
|
||||
else
|
||||
_messages = new MessageManager(Discord, this);
|
||||
}
|
||||
internal new static SocketDMChannel Create(DiscordSocketClient discord, Model model)
|
||||
{
|
||||
var entity = new SocketDMChannel(discord, model.Id, model.Recipients.Value[0].Id);
|
||||
entity.Update(model);
|
||||
return entity;
|
||||
}
|
||||
internal void Update(Model model)
|
||||
{
|
||||
Recipient.Update(model.Recipients.Value[0]);
|
||||
}
|
||||
|
||||
public override Task<IUser> GetUserAsync(ulong id) => Task.FromResult<IUser>(GetUser(id));
|
||||
public override Task<IReadOnlyCollection<IUser>> GetUsersAsync() => Task.FromResult<IReadOnlyCollection<IUser>>(Users);
|
||||
public ISocketUser GetUser(ulong id)
|
||||
public Task CloseAsync()
|
||||
=> ChannelHelper.DeleteAsync(this, Discord);
|
||||
|
||||
public SocketUser GetUser(ulong id)
|
||||
{
|
||||
var currentUser = Discord.CurrentUser;
|
||||
if (id == Recipient.Id)
|
||||
return Recipient;
|
||||
else if (id == currentUser.Id)
|
||||
return currentUser;
|
||||
else if (id == Discord.CurrentUser.Id)
|
||||
return Discord.CurrentUser as SocketSelfUser;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public override async Task<IMessage> GetMessageAsync(ulong id)
|
||||
public SocketMessage GetMessage(ulong id)
|
||||
=> _messages?.Get(id);
|
||||
public async Task<IMessage> GetMessageAsync(ulong id, bool allowDownload = true)
|
||||
{
|
||||
return await _messages.DownloadAsync(id).ConfigureAwait(false);
|
||||
IMessage msg = _messages?.Get(id);
|
||||
if (msg == null && allowDownload)
|
||||
msg = await ChannelHelper.GetMessageAsync(this, Discord, id);
|
||||
return msg;
|
||||
}
|
||||
public override async Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit)
|
||||
public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch)
|
||||
=> ChannelHelper.GetMessagesAsync(this, Discord, limit: limit);
|
||||
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
|
||||
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit);
|
||||
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
|
||||
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit);
|
||||
public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync()
|
||||
=> ChannelHelper.GetPinnedMessagesAsync(this, Discord);
|
||||
|
||||
public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS)
|
||||
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS);
|
||||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS)
|
||||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS);
|
||||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS)
|
||||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS);
|
||||
|
||||
public Task DeleteMessagesAsync(IEnumerable<IMessage> messages)
|
||||
=> ChannelHelper.DeleteMessagesAsync(this, Discord, messages);
|
||||
|
||||
public IDisposable EnterTypingState()
|
||||
=> ChannelHelper.EnterTypingState(this, Discord);
|
||||
|
||||
internal SocketMessage AddMessage(SocketUser author, MessageModel model)
|
||||
{
|
||||
return await _messages.DownloadAsync(null, Direction.Before, limit).ConfigureAwait(false);
|
||||
}
|
||||
public override async Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit)
|
||||
{
|
||||
return await _messages.DownloadAsync(fromMessageId, dir, limit).ConfigureAwait(false);
|
||||
}
|
||||
public ISocketMessage CreateMessage(ISocketUser author, MessageModel model)
|
||||
{
|
||||
return _messages.Create(author, model);
|
||||
}
|
||||
public ISocketMessage AddMessage(ISocketUser author, MessageModel model)
|
||||
{
|
||||
var msg = _messages.Create(author, model);
|
||||
var msg = SocketMessage.Create(Discord, author, model);
|
||||
_messages.Add(msg);
|
||||
return msg;
|
||||
}
|
||||
public ISocketMessage GetMessage(ulong id)
|
||||
{
|
||||
return _messages.Get(id);
|
||||
}
|
||||
public ISocketMessage RemoveMessage(ulong id)
|
||||
internal SocketMessage RemoveMessage(ulong id)
|
||||
{
|
||||
return _messages.Remove(id);
|
||||
}
|
||||
|
||||
public SocketDMChannel Clone() => MemberwiseClone() as SocketDMChannel;
|
||||
|
||||
IMessage IMessageChannel.GetCachedMessage(ulong id) => GetMessage(id);
|
||||
ISocketUser ISocketMessageChannel.GetUser(ulong id, bool skipCheck) => GetUser(id);
|
||||
ISocketChannel ISocketChannel.Clone() => Clone();
|
||||
public override string ToString() => $"@{Recipient}";
|
||||
private string DebuggerDisplay => $"@{Recipient} ({Id}, DM)";
|
||||
|
||||
//IDMChannel
|
||||
IUser IDMChannel.Recipient => Recipient;
|
||||
|
||||
//IPrivateChannel
|
||||
IReadOnlyCollection<IUser> IPrivateChannel.Recipients => ImmutableArray.Create<IUser>(Recipient);
|
||||
|
||||
//IMessageChannel
|
||||
IReadOnlyCollection<IMessage> IMessageChannel.CachedMessages => ImmutableArray.Create<IMessage>();
|
||||
IMessage IMessageChannel.GetCachedMessage(ulong id) => null;
|
||||
|
||||
async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id)
|
||||
=> await GetMessageAsync(id);
|
||||
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit)
|
||||
=> GetMessagesAsync(limit);
|
||||
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit)
|
||||
=> GetMessagesAsync(fromMessageId, dir, limit);
|
||||
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync()
|
||||
=> await GetPinnedMessagesAsync().ConfigureAwait(false);
|
||||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS)
|
||||
=> await SendFileAsync(filePath, text, isTTS);
|
||||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS)
|
||||
=> await SendFileAsync(stream, filename, text, isTTS);
|
||||
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS)
|
||||
=> await SendMessageAsync(text, isTTS);
|
||||
IDisposable IMessageChannel.EnterTypingState()
|
||||
=> EnterTypingState();
|
||||
|
||||
//IChannel
|
||||
IReadOnlyCollection<IUser> IChannel.CachedUsers => Users;
|
||||
|
||||
IUser IChannel.GetCachedUser(ulong id)
|
||||
=> GetUser(id);
|
||||
Task<IUser> IChannel.GetUserAsync(ulong id)
|
||||
=> Task.FromResult<IUser>(GetUser(id));
|
||||
IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync()
|
||||
=> ImmutableArray.Create<IReadOnlyCollection<IUser>>().ToAsyncEnumerable();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
using Discord.Rest;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MessageModel = Discord.API.Message;
|
||||
@@ -11,134 +14,124 @@ using VoiceStateModel = Discord.API.VoiceState;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
internal class SocketGroupChannel : IGroupChannel, ISocketChannel, ISocketMessageChannel, ISocketPrivateChannel
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
public class SocketGroupChannel : SocketChannel, IGroupChannel
|
||||
{
|
||||
internal override bool IsAttached => true;
|
||||
private readonly MessageCache _messages;
|
||||
|
||||
private readonly MessageManager _messages;
|
||||
private ConcurrentDictionary<ulong, VoiceState> _voiceStates;
|
||||
private string _iconId;
|
||||
private ConcurrentDictionary<ulong, SocketGroupUser> _users;
|
||||
private ConcurrentDictionary<ulong, SocketVoiceState> _voiceStates;
|
||||
|
||||
public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
|
||||
public IReadOnlyCollection<ISocketUser> Users
|
||||
=> _users.Select(x => x.Value as ISocketUser).Concat(ImmutableArray.Create(Discord.CurrentUser)).ToReadOnlyCollection(() => _users.Count + 1);
|
||||
public new IReadOnlyCollection<ISocketUser> Recipients => _users.Select(x => x.Value as ISocketUser).ToReadOnlyCollection(_users);
|
||||
public string Name { get; private set; }
|
||||
|
||||
public SocketGroupChannel(DiscordSocketClient discord, Model model)
|
||||
: base(discord, model)
|
||||
public IReadOnlyCollection<SocketGroupUser> Users => _users.ToReadOnlyCollection();
|
||||
public IReadOnlyCollection<SocketGroupUser> Recipients
|
||||
=> _users.Select(x => x.Value).Where(x => x.Id != Discord.CurrentUser.Id).ToReadOnlyCollection(() => _users.Count - 1);
|
||||
|
||||
internal SocketGroupChannel(DiscordSocketClient discord, ulong id)
|
||||
: base(discord, id)
|
||||
{
|
||||
if (Discord.MessageCacheSize > 0)
|
||||
_messages = new MessageCache(Discord, this);
|
||||
else
|
||||
_messages = new MessageManager(Discord, this);
|
||||
_voiceStates = new ConcurrentDictionary<ulong, VoiceState>(1, 5);
|
||||
_voiceStates = new ConcurrentDictionary<ulong, SocketVoiceState>(1, 5);
|
||||
_users = new ConcurrentDictionary<ulong, SocketGroupUser>(1, 5);
|
||||
}
|
||||
public override void Update(Model model)
|
||||
internal new static SocketGroupChannel Create(DiscordSocketClient discord, Model model)
|
||||
{
|
||||
if (source == UpdateSource.Rest && IsAttached) return;
|
||||
|
||||
base.Update(model, source);
|
||||
var entity = new SocketGroupChannel(discord, model.Id);
|
||||
entity.Update(model);
|
||||
return entity;
|
||||
}
|
||||
|
||||
internal void UpdateUsers(UserModel[] models, DataStore dataStore)
|
||||
internal void Update(Model model)
|
||||
{
|
||||
var users = new ConcurrentDictionary<ulong, GroupUser>(1, models.Length);
|
||||
if (model.Name.IsSpecified)
|
||||
Name = model.Name.Value;
|
||||
if (model.Icon.IsSpecified)
|
||||
_iconId = model.Icon.Value;
|
||||
|
||||
if (model.Recipients.IsSpecified)
|
||||
UpdateUsers(model.Recipients.Value);
|
||||
}
|
||||
internal virtual void UpdateUsers(API.User[] models)
|
||||
{
|
||||
var users = new ConcurrentDictionary<ulong, SocketGroupUser>(1, (int)(models.Length * 1.05));
|
||||
for (int i = 0; i < models.Length; i++)
|
||||
{
|
||||
var globalUser = Discord.GetOrAddUser(models[i], dataStore);
|
||||
users[models[i].Id] = new SocketGroupUser(this, globalUser);
|
||||
}
|
||||
users[models[i].Id] = SocketGroupUser.Create(Discord, models[i]);
|
||||
_users = users;
|
||||
}
|
||||
internal override void UpdateUsers(UserModel[] models)
|
||||
=> UpdateUsers(models, source, Discord.DataStore);
|
||||
|
||||
public SocketGroupUser AddUser(UserModel model, DataStore dataStore)
|
||||
public async Task UpdateAsync()
|
||||
=> Update(await ChannelHelper.GetAsync(this, Discord));
|
||||
public Task LeaveAsync()
|
||||
=> ChannelHelper.DeleteAsync(this, Discord);
|
||||
|
||||
public SocketGroupUser GetUser(ulong id)
|
||||
{
|
||||
GroupUser user;
|
||||
if (_users.TryGetValue(model.Id, out user))
|
||||
return user as SocketGroupUser;
|
||||
else
|
||||
{
|
||||
var globalUser = Discord.GetOrAddUser(model, dataStore);
|
||||
var privateUser = new SocketGroupUser(this, globalUser);
|
||||
_users[privateUser.Id] = privateUser;
|
||||
return privateUser;
|
||||
}
|
||||
}
|
||||
public ISocketUser GetUser(ulong id)
|
||||
{
|
||||
GroupUser user;
|
||||
SocketGroupUser user;
|
||||
if (_users.TryGetValue(id, out user))
|
||||
return user as SocketGroupUser;
|
||||
if (id == Discord.CurrentUser.Id)
|
||||
return Discord.CurrentUser;
|
||||
return null;
|
||||
}
|
||||
public SocketGroupUser RemoveUser(ulong id)
|
||||
{
|
||||
GroupUser user;
|
||||
if (_users.TryRemove(id, out user))
|
||||
return user as SocketGroupUser;
|
||||
return user;
|
||||
return null;
|
||||
}
|
||||
|
||||
public VoiceState AddOrUpdateVoiceState(VoiceStateModel model, DataStore dataStore, ConcurrentDictionary<ulong, VoiceState> voiceStates = null)
|
||||
{
|
||||
var voiceChannel = dataStore.GetChannel(model.ChannelId.Value) as SocketVoiceChannel;
|
||||
var voiceState = new VoiceState(voiceChannel, model);
|
||||
(voiceStates ?? _voiceStates)[model.UserId] = voiceState;
|
||||
return voiceState;
|
||||
}
|
||||
public VoiceState? GetVoiceState(ulong id)
|
||||
{
|
||||
VoiceState voiceState;
|
||||
if (_voiceStates.TryGetValue(id, out voiceState))
|
||||
return voiceState;
|
||||
return null;
|
||||
}
|
||||
public VoiceState? RemoveVoiceState(ulong id)
|
||||
{
|
||||
VoiceState voiceState;
|
||||
if (_voiceStates.TryRemove(id, out voiceState))
|
||||
return voiceState;
|
||||
return null;
|
||||
}
|
||||
public Task<RestMessage> GetMessageAsync(ulong id)
|
||||
=> ChannelHelper.GetMessageAsync(this, Discord, id);
|
||||
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch)
|
||||
=> ChannelHelper.GetMessagesAsync(this, Discord, limit: limit);
|
||||
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
|
||||
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit);
|
||||
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
|
||||
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit);
|
||||
public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync()
|
||||
=> ChannelHelper.GetPinnedMessagesAsync(this, Discord);
|
||||
|
||||
public override async Task<IMessage> GetMessageAsync(ulong id)
|
||||
{
|
||||
return await _messages.DownloadAsync(id).ConfigureAwait(false);
|
||||
}
|
||||
public override async Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit)
|
||||
{
|
||||
return await _messages.DownloadAsync(null, Direction.Before, limit).ConfigureAwait(false);
|
||||
}
|
||||
public override async Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit)
|
||||
{
|
||||
return await _messages.DownloadAsync(fromMessageId, dir, limit).ConfigureAwait(false);
|
||||
}
|
||||
public ISocketMessage CreateMessage(ISocketUser author, MessageModel model)
|
||||
{
|
||||
return _messages.Create(author, model);
|
||||
}
|
||||
public ISocketMessage AddMessage(ISocketUser author, MessageModel model)
|
||||
{
|
||||
var msg = _messages.Create(author, model);
|
||||
_messages.Add(msg);
|
||||
return msg;
|
||||
}
|
||||
public ISocketMessage GetMessage(ulong id)
|
||||
{
|
||||
return _messages.Get(id);
|
||||
}
|
||||
public ISocketMessage RemoveMessage(ulong id)
|
||||
{
|
||||
return _messages.Remove(id);
|
||||
}
|
||||
public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS)
|
||||
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS);
|
||||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS)
|
||||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS);
|
||||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS)
|
||||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS);
|
||||
|
||||
public SocketDMChannel Clone() => MemberwiseClone() as SocketDMChannel;
|
||||
public Task DeleteMessagesAsync(IEnumerable<IMessage> messages)
|
||||
=> ChannelHelper.DeleteMessagesAsync(this, Discord, messages);
|
||||
|
||||
IMessage IMessageChannel.GetCachedMessage(ulong id) => GetMessage(id);
|
||||
ISocketUser ISocketMessageChannel.GetUser(ulong id, bool skipCheck) => GetUser(id);
|
||||
ISocketChannel ISocketChannel.Clone() => Clone();
|
||||
public IDisposable EnterTypingState()
|
||||
=> ChannelHelper.EnterTypingState(this, Discord);
|
||||
|
||||
//IPrivateChannel
|
||||
IReadOnlyCollection<IUser> IPrivateChannel.Recipients => Recipients;
|
||||
|
||||
//IMessageChannel
|
||||
IReadOnlyCollection<IMessage> IMessageChannel.CachedMessages => ImmutableArray.Create<IMessage>();
|
||||
|
||||
IMessage IMessageChannel.GetCachedMessage(ulong id)
|
||||
=> null;
|
||||
async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id)
|
||||
=> await GetMessageAsync(id);
|
||||
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit)
|
||||
=> GetMessagesAsync(limit);
|
||||
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit)
|
||||
=> GetMessagesAsync(fromMessageId, dir, limit);
|
||||
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync()
|
||||
=> await GetPinnedMessagesAsync();
|
||||
|
||||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS)
|
||||
=> await SendFileAsync(filePath, text, isTTS);
|
||||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS)
|
||||
=> await SendFileAsync(stream, filename, text, isTTS);
|
||||
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS)
|
||||
=> await SendMessageAsync(text, isTTS);
|
||||
IDisposable IMessageChannel.EnterTypingState()
|
||||
=> EnterTypingState();
|
||||
|
||||
//IChannel
|
||||
IReadOnlyCollection<IUser> IChannel.CachedUsers => Users;
|
||||
|
||||
IUser IChannel.GetCachedUser(ulong id)
|
||||
=> GetUser(id);
|
||||
Task<IUser> IChannel.GetUserAsync(ulong id)
|
||||
=> Task.FromResult<IUser>(GetUser(id));
|
||||
IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync()
|
||||
=> ImmutableArray.Create<IReadOnlyCollection<IUser>>(Users).ToAsyncEnumerable();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Discord.API.Rest;
|
||||
using Discord.Rest;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
@@ -7,89 +8,59 @@ using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Model = Discord.API.Channel;
|
||||
|
||||
namespace Discord.Rest
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
internal abstract class SocketGuildChannel : ISnowflakeEntity, IGuildChannel
|
||||
public abstract class SocketGuildChannel : SocketChannel, IGuildChannel
|
||||
{
|
||||
private List<Overwrite> _overwrites; //TODO: Is maintaining a list here too expensive? Is this threadsafe?
|
||||
private ImmutableArray<Overwrite> _overwrites;
|
||||
|
||||
public IReadOnlyCollection<Overwrite> PermissionOverwrites => _overwrites;
|
||||
|
||||
public ulong GuildId { get; }
|
||||
|
||||
public string Name { get; private set; }
|
||||
public int Position { get; private set; }
|
||||
|
||||
public Guild Guild { get; private set; }
|
||||
|
||||
public override DiscordRestClient Discord => Guild.Discord;
|
||||
|
||||
public GuildChannel(Guild guild, Model model)
|
||||
: base(model.Id)
|
||||
internal SocketGuildChannel(DiscordSocketClient discord, ulong id, ulong guildId)
|
||||
: base(discord, id)
|
||||
{
|
||||
Guild = guild;
|
||||
|
||||
Update(model);
|
||||
GuildId = guildId;
|
||||
}
|
||||
public virtual void Update(Model model)
|
||||
internal new static SocketGuildChannel Create(DiscordSocketClient discord, Model model)
|
||||
{
|
||||
switch (model.Type)
|
||||
{
|
||||
case ChannelType.Text:
|
||||
return SocketTextChannel.Create(discord, model);
|
||||
case ChannelType.Voice:
|
||||
return SocketVoiceChannel.Create(discord, model);
|
||||
default:
|
||||
throw new InvalidOperationException("Unknown guild channel type");
|
||||
}
|
||||
}
|
||||
internal virtual void Update(Model model)
|
||||
{
|
||||
if (source == UpdateSource.Rest && IsAttached) return;
|
||||
|
||||
Name = model.Name.Value;
|
||||
Position = model.Position.Value;
|
||||
|
||||
var overwrites = model.PermissionOverwrites.Value;
|
||||
var newOverwrites = new List<Overwrite>(overwrites.Length);
|
||||
var newOverwrites = ImmutableArray.CreateBuilder<Overwrite>(overwrites.Length);
|
||||
for (int i = 0; i < overwrites.Length; i++)
|
||||
newOverwrites.Add(new Overwrite(overwrites[i]));
|
||||
_overwrites = newOverwrites;
|
||||
_overwrites = newOverwrites.ToImmutable();
|
||||
}
|
||||
|
||||
public async Task UpdateAsync()
|
||||
{
|
||||
if (IsAttached) throw new NotSupportedException();
|
||||
|
||||
var model = await Discord.ApiClient.GetChannelAsync(Id).ConfigureAwait(false);
|
||||
Update(model, UpdateSource.Rest);
|
||||
}
|
||||
public async Task ModifyAsync(Action<ModifyGuildChannelParams> func)
|
||||
{
|
||||
if (func == null) throw new NullReferenceException(nameof(func));
|
||||
|
||||
var args = new ModifyGuildChannelParams();
|
||||
func(args);
|
||||
|
||||
if (!args._name.IsSpecified)
|
||||
args._name = Name;
|
||||
|
||||
var model = await Discord.ApiClient.ModifyGuildChannelAsync(Id, args).ConfigureAwait(false);
|
||||
Update(model, UpdateSource.Rest);
|
||||
}
|
||||
public async Task DeleteAsync()
|
||||
{
|
||||
await Discord.ApiClient.DeleteChannelAsync(Id).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public abstract Task<IGuildUser> GetUserAsync(ulong id);
|
||||
public abstract Task<IReadOnlyCollection<IGuildUser>> GetUsersAsync();
|
||||
|
||||
public async Task<IReadOnlyCollection<IInviteMetadata>> GetInvitesAsync()
|
||||
{
|
||||
var models = await Discord.ApiClient.GetChannelInvitesAsync(Id).ConfigureAwait(false);
|
||||
return models.Select(x => new InviteMetadata(Discord, x)).ToImmutableArray();
|
||||
}
|
||||
public async Task<IInviteMetadata> CreateInviteAsync(int? maxAge, int? maxUses, bool isTemporary)
|
||||
{
|
||||
var args = new CreateChannelInviteParams
|
||||
{
|
||||
MaxAge = maxAge ?? 0,
|
||||
MaxUses = maxUses ?? 0,
|
||||
Temporary = isTemporary
|
||||
};
|
||||
var model = await Discord.ApiClient.CreateChannelInviteAsync(Id, args).ConfigureAwait(false);
|
||||
return new InviteMetadata(Discord, model);
|
||||
}
|
||||
=> Update(await ChannelHelper.GetAsync(this, Discord));
|
||||
public Task ModifyAsync(Action<ModifyGuildChannelParams> func)
|
||||
=> ChannelHelper.ModifyAsync(this, Discord, func);
|
||||
public Task DeleteAsync()
|
||||
=> ChannelHelper.DeleteAsync(this, Discord);
|
||||
|
||||
public OverwritePermissions? GetPermissionOverwrite(IUser user)
|
||||
{
|
||||
for (int i = 0; i < _overwrites.Count; i++)
|
||||
for (int i = 0; i < _overwrites.Length; i++)
|
||||
{
|
||||
if (_overwrites[i].TargetId == user.Id)
|
||||
return _overwrites[i].Permissions;
|
||||
@@ -98,60 +69,91 @@ namespace Discord.Rest
|
||||
}
|
||||
public OverwritePermissions? GetPermissionOverwrite(IRole role)
|
||||
{
|
||||
for (int i = 0; i < _overwrites.Count; i++)
|
||||
for (int i = 0; i < _overwrites.Length; i++)
|
||||
{
|
||||
if (_overwrites[i].TargetId == role.Id)
|
||||
return _overwrites[i].Permissions;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task AddPermissionOverwriteAsync(IUser user, OverwritePermissions perms)
|
||||
{
|
||||
var args = new ModifyChannelPermissionsParams { Allow = perms.AllowValue, Deny = perms.DenyValue, Type = "member" };
|
||||
await Discord.ApiClient.ModifyChannelPermissionsAsync(Id, user.Id, args).ConfigureAwait(false);
|
||||
_overwrites.Add(new Overwrite(new API.Overwrite { Allow = perms.AllowValue, Deny = perms.DenyValue, TargetId = user.Id, TargetType = PermissionTarget.User }));
|
||||
await ChannelHelper.AddPermissionOverwriteAsync(this, Discord, user, perms).ConfigureAwait(false);
|
||||
_overwrites = _overwrites.Add(new Overwrite(new API.Overwrite { Allow = perms.AllowValue, Deny = perms.DenyValue, TargetId = user.Id, TargetType = PermissionTarget.User }));
|
||||
}
|
||||
public async Task AddPermissionOverwriteAsync(IRole role, OverwritePermissions perms)
|
||||
{
|
||||
var args = new ModifyChannelPermissionsParams { Allow = perms.AllowValue, Deny = perms.DenyValue, Type = "role" };
|
||||
await Discord.ApiClient.ModifyChannelPermissionsAsync(Id, role.Id, args).ConfigureAwait(false);
|
||||
await ChannelHelper.AddPermissionOverwriteAsync(this, Discord, role, perms).ConfigureAwait(false);
|
||||
_overwrites.Add(new Overwrite(new API.Overwrite { Allow = perms.AllowValue, Deny = perms.DenyValue, TargetId = role.Id, TargetType = PermissionTarget.Role }));
|
||||
}
|
||||
public async Task RemovePermissionOverwriteAsync(IUser user)
|
||||
{
|
||||
await Discord.ApiClient.DeleteChannelPermissionAsync(Id, user.Id).ConfigureAwait(false);
|
||||
await ChannelHelper.RemovePermissionOverwriteAsync(this, Discord, user).ConfigureAwait(false);
|
||||
|
||||
for (int i = 0; i < _overwrites.Count; i++)
|
||||
for (int i = 0; i < _overwrites.Length; i++)
|
||||
{
|
||||
if (_overwrites[i].TargetId == user.Id)
|
||||
{
|
||||
_overwrites.RemoveAt(i);
|
||||
_overwrites = _overwrites.RemoveAt(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
public async Task RemovePermissionOverwriteAsync(IRole role)
|
||||
{
|
||||
await Discord.ApiClient.DeleteChannelPermissionAsync(Id, role.Id).ConfigureAwait(false);
|
||||
await ChannelHelper.RemovePermissionOverwriteAsync(this, Discord, role).ConfigureAwait(false);
|
||||
|
||||
for (int i = 0; i < _overwrites.Count; i++)
|
||||
for (int i = 0; i < _overwrites.Length; i++)
|
||||
{
|
||||
if (_overwrites[i].TargetId == role.Id)
|
||||
{
|
||||
_overwrites.RemoveAt(i);
|
||||
_overwrites = _overwrites.RemoveAt(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString() => Name;
|
||||
private string DebuggerDisplay => $"{Name} ({Id})";
|
||||
|
||||
IGuild IGuildChannel.Guild => Guild;
|
||||
IReadOnlyCollection<Overwrite> IGuildChannel.PermissionOverwrites => _overwrites.AsReadOnly();
|
||||
|
||||
async Task<IUser> IChannel.GetUserAsync(ulong id) => await GetUserAsync(id).ConfigureAwait(false);
|
||||
async Task<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync() => await GetUsersAsync().ConfigureAwait(false);
|
||||
public async Task<IReadOnlyCollection<RestInviteMetadata>> GetInvitesAsync()
|
||||
=> await ChannelHelper.GetInvitesAsync(this, Discord);
|
||||
public async Task<RestInviteMetadata> CreateInviteAsync(int? maxAge = 3600, int? maxUses = null, bool isTemporary = true)
|
||||
=> await ChannelHelper.CreateInviteAsync(this, Discord, maxAge, maxUses, isTemporary);
|
||||
|
||||
//IGuildChannel
|
||||
async Task<IReadOnlyCollection<IInviteMetadata>> IGuildChannel.GetInvitesAsync()
|
||||
=> await GetInvitesAsync();
|
||||
async Task<IInviteMetadata> IGuildChannel.CreateInviteAsync(int? maxAge, int? maxUses, bool isTemporary)
|
||||
=> await CreateInviteAsync(maxAge, maxUses, isTemporary);
|
||||
|
||||
OverwritePermissions? IGuildChannel.GetPermissionOverwrite(IRole role)
|
||||
=> GetPermissionOverwrite(role);
|
||||
OverwritePermissions? IGuildChannel.GetPermissionOverwrite(IUser user)
|
||||
=> GetPermissionOverwrite(user);
|
||||
async Task IGuildChannel.AddPermissionOverwriteAsync(IRole role, OverwritePermissions permissions)
|
||||
=> await AddPermissionOverwriteAsync(role, permissions);
|
||||
async Task IGuildChannel.AddPermissionOverwriteAsync(IUser user, OverwritePermissions permissions)
|
||||
=> await AddPermissionOverwriteAsync(user, permissions);
|
||||
async Task IGuildChannel.RemovePermissionOverwriteAsync(IRole role)
|
||||
=> await RemovePermissionOverwriteAsync(role);
|
||||
async Task IGuildChannel.RemovePermissionOverwriteAsync(IUser user)
|
||||
=> await RemovePermissionOverwriteAsync(user);
|
||||
|
||||
IReadOnlyCollection<IGuildUser> IGuildChannel.CachedUsers
|
||||
=> ImmutableArray.Create<IGuildUser>();
|
||||
IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync()
|
||||
=> ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>().ToAsyncEnumerable(); //Overriden in Text/Voice //TODO: Does this actually override?
|
||||
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id)
|
||||
=> Task.FromResult<IGuildUser>(null); //Overriden in Text/Voice //TODO: Does this actually override?
|
||||
IGuildUser IGuildChannel.GetCachedUser(ulong id)
|
||||
=> null;
|
||||
|
||||
//IChannel
|
||||
IReadOnlyCollection<IUser> IChannel.CachedUsers
|
||||
=> ImmutableArray.Create<IUser>();
|
||||
IUser IChannel.GetCachedUser(ulong id)
|
||||
=> null;
|
||||
IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync()
|
||||
=> ImmutableArray.Create<IReadOnlyCollection<IUser>>().ToAsyncEnumerable(); //Overriden in Text/Voice //TODO: Does this actually override?
|
||||
Task<IUser> IChannel.GetUserAsync(ulong id)
|
||||
=> Task.FromResult<IUser>(null); //Overriden in Text/Voice //TODO: Does this actually override?
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
using Discord.Rest;
|
||||
using Discord.API.Rest;
|
||||
using Discord.Rest;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MessageModel = Discord.API.Message;
|
||||
@@ -8,81 +12,105 @@ using Model = Discord.API.Channel;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
internal class SocketTextChannel : TextChannel, ISocketGuildChannel, ISocketMessageChannel
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
public class SocketTextChannel : SocketGuildChannel, ITextChannel
|
||||
{
|
||||
internal override bool IsAttached => true;
|
||||
private readonly MessageCache _messages;
|
||||
|
||||
private readonly MessageManager _messages;
|
||||
public string Topic { get; private set; }
|
||||
|
||||
public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
|
||||
public new SocketGuild Guild => base.Guild as SocketGuild;
|
||||
public string Mention => MentionUtils.MentionChannel(Id);
|
||||
|
||||
public IReadOnlyCollection<SocketGuildUser> Members
|
||||
=> Guild.Members.Where(x => Permissions.GetValue(Permissions.ResolveChannel(x, this, x.GuildPermissions.RawValue), ChannelPermission.ReadMessages)).ToImmutableArray();
|
||||
|
||||
public SocketTextChannel(SocketGuild guild, Model model)
|
||||
: base(guild, model)
|
||||
internal SocketTextChannel(DiscordSocketClient discord, ulong id, ulong guildId)
|
||||
: base(discord, id, guildId)
|
||||
{
|
||||
if (Discord.MessageCacheSize > 0)
|
||||
_messages = new MessageCache(Discord, this);
|
||||
else
|
||||
_messages = new MessageManager(Discord, this);
|
||||
}
|
||||
internal new static SocketTextChannel Create(DiscordSocketClient discord, Model model)
|
||||
{
|
||||
var entity = new SocketTextChannel(discord, model.Id, model.GuildId.Value);
|
||||
entity.Update(model);
|
||||
return entity;
|
||||
}
|
||||
internal override void Update(Model model)
|
||||
{
|
||||
base.Update(model);
|
||||
|
||||
Topic = model.Topic.Value;
|
||||
}
|
||||
|
||||
public override Task<IGuildUser> GetUserAsync(ulong id) => Task.FromResult<IGuildUser>(GetUser(id));
|
||||
public override Task<IReadOnlyCollection<IGuildUser>> GetUsersAsync() => Task.FromResult<IReadOnlyCollection<IGuildUser>>(Members);
|
||||
public SocketGuildUser GetUser(ulong id, bool skipCheck = false)
|
||||
{
|
||||
var user = Guild.GetUser(id);
|
||||
if (skipCheck) return user;
|
||||
public Task ModifyAsync(Action<ModifyTextChannelParams> func)
|
||||
=> ChannelHelper.ModifyAsync(this, Discord, func);
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
ulong perms = Permissions.ResolveChannel(user, this, user.GuildPermissions.RawValue);
|
||||
if (Permissions.GetValue(perms, ChannelPermission.ReadMessages))
|
||||
return user;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public Task<RestGuildUser> GetUserAsync(ulong id)
|
||||
=> ChannelHelper.GetUserAsync(this, Discord, id);
|
||||
public IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync()
|
||||
=> ChannelHelper.GetUsersAsync(this, Discord);
|
||||
|
||||
public override async Task<IMessage> GetMessageAsync(ulong id)
|
||||
{
|
||||
return await _messages.DownloadAsync(id).ConfigureAwait(false);
|
||||
}
|
||||
public override async Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch)
|
||||
{
|
||||
return await _messages.DownloadAsync(null, Direction.Before, limit).ConfigureAwait(false);
|
||||
}
|
||||
public override async Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
|
||||
{
|
||||
return await _messages.DownloadAsync(fromMessageId, dir, limit).ConfigureAwait(false);
|
||||
}
|
||||
public Task<RestMessage> GetMessageAsync(ulong id)
|
||||
=> ChannelHelper.GetMessageAsync(this, Discord, id);
|
||||
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch)
|
||||
=> ChannelHelper.GetMessagesAsync(this, Discord, limit: limit);
|
||||
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
|
||||
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit);
|
||||
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
|
||||
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit);
|
||||
public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync()
|
||||
=> ChannelHelper.GetPinnedMessagesAsync(this, Discord);
|
||||
|
||||
public ISocketMessage CreateMessage(ISocketUser author, MessageModel model)
|
||||
public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS)
|
||||
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS);
|
||||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS)
|
||||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS);
|
||||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS)
|
||||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS);
|
||||
|
||||
public Task DeleteMessagesAsync(IEnumerable<IMessage> messages)
|
||||
=> ChannelHelper.DeleteMessagesAsync(this, Discord, messages);
|
||||
|
||||
public IDisposable EnterTypingState()
|
||||
=> ChannelHelper.EnterTypingState(this, Discord);
|
||||
|
||||
internal SocketMessage AddMessage(SocketUser author, MessageModel model)
|
||||
{
|
||||
return _messages.Create(author, model);
|
||||
}
|
||||
public ISocketMessage AddMessage(ISocketUser author, MessageModel model)
|
||||
{
|
||||
var msg = _messages.Create(author, model);
|
||||
var msg = SocketMessage.Create(Discord, author, model);
|
||||
_messages.Add(msg);
|
||||
return msg;
|
||||
}
|
||||
public ISocketMessage GetMessage(ulong id)
|
||||
{
|
||||
return _messages.Get(id);
|
||||
}
|
||||
public ISocketMessage RemoveMessage(ulong id)
|
||||
internal SocketMessage RemoveMessage(ulong id)
|
||||
{
|
||||
return _messages.Remove(id);
|
||||
}
|
||||
|
||||
public SocketTextChannel Clone() => MemberwiseClone() as SocketTextChannel;
|
||||
public override string ToString() => Name;
|
||||
private string DebuggerDisplay => $"@{Name} ({Id}, Text)";
|
||||
|
||||
IReadOnlyCollection<ISocketUser> ISocketMessageChannel.Users => Members;
|
||||
//IGuildChannel
|
||||
async Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id)
|
||||
=> await GetUserAsync(id);
|
||||
IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync()
|
||||
=> GetUsersAsync();
|
||||
|
||||
IMessage IMessageChannel.GetCachedMessage(ulong id) => GetMessage(id);
|
||||
ISocketUser ISocketMessageChannel.GetUser(ulong id, bool skipCheck) => GetUser(id, skipCheck);
|
||||
ISocketChannel ISocketChannel.Clone() => Clone();
|
||||
//IMessageChannel
|
||||
IReadOnlyCollection<IMessage> IMessageChannel.CachedMessages => ImmutableArray.Create<IMessage>();
|
||||
IMessage IMessageChannel.GetCachedMessage(ulong id) => null;
|
||||
|
||||
async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id)
|
||||
=> await GetMessageAsync(id);
|
||||
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit)
|
||||
=> GetMessagesAsync(limit);
|
||||
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit)
|
||||
=> GetMessagesAsync(fromMessageId, dir, limit);
|
||||
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync()
|
||||
=> await GetPinnedMessagesAsync().ConfigureAwait(false);
|
||||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS)
|
||||
=> await SendFileAsync(filePath, text, isTTS);
|
||||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS)
|
||||
=> await SendFileAsync(stream, filename, text, isTTS);
|
||||
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS)
|
||||
=> await SendMessageAsync(text, isTTS);
|
||||
IDisposable IMessageChannel.EnterTypingState()
|
||||
=> EnterTypingState();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,54 +1,50 @@
|
||||
using Discord.Audio;
|
||||
using Discord.API.Rest;
|
||||
using Discord.Audio;
|
||||
using Discord.Rest;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Model = Discord.API.Channel;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
internal class SocketVoiceChannel : VoiceChannel, ISocketGuildChannel
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
public class SocketVoiceChannel : SocketGuildChannel, IVoiceChannel
|
||||
{
|
||||
internal override bool IsAttached => true;
|
||||
public int Bitrate { get; private set; }
|
||||
public int UserLimit { get; private set; }
|
||||
|
||||
public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
|
||||
public new SocketGuild Guild => base.Guild as SocketGuild;
|
||||
|
||||
public IReadOnlyCollection<IGuildUser> Members
|
||||
=> Guild.VoiceStates.Where(x => x.Value.VoiceChannel.Id == Id).Select(x => Guild.GetUser(x.Key)).ToImmutableArray();
|
||||
|
||||
public SocketVoiceChannel(SocketGuild guild, Model model)
|
||||
: base(guild, model)
|
||||
internal SocketVoiceChannel(DiscordSocketClient discord, ulong id, ulong guildId)
|
||||
: base(discord, id, guildId)
|
||||
{
|
||||
}
|
||||
|
||||
public override Task<IGuildUser> GetUserAsync(ulong id)
|
||||
=> Task.FromResult(GetUser(id));
|
||||
public override Task<IReadOnlyCollection<IGuildUser>> GetUsersAsync()
|
||||
=> Task.FromResult(Members);
|
||||
public IGuildUser GetUser(ulong id)
|
||||
internal new static SocketVoiceChannel Create(DiscordSocketClient discord, Model model)
|
||||
{
|
||||
var user = Guild.GetUser(id);
|
||||
if (user != null && user.VoiceChannel.Id == Id)
|
||||
return user;
|
||||
return null;
|
||||
var entity = new SocketVoiceChannel(discord, model.Id, model.GuildId.Value);
|
||||
entity.Update(model);
|
||||
return entity;
|
||||
}
|
||||
internal override void Update(Model model)
|
||||
{
|
||||
base.Update(model);
|
||||
|
||||
Bitrate = model.Bitrate.Value;
|
||||
UserLimit = model.UserLimit.Value;
|
||||
}
|
||||
|
||||
public override async Task<IAudioClient> ConnectAsync()
|
||||
{
|
||||
var audioMode = Discord.AudioMode;
|
||||
if (audioMode == AudioMode.Disabled)
|
||||
throw new InvalidOperationException($"Audio is not enabled on this client, {nameof(DiscordSocketConfig.AudioMode)} in {nameof(DiscordSocketConfig)} must be set.");
|
||||
|
||||
return await Guild.ConnectAudioAsync(Id,
|
||||
(audioMode & AudioMode.Incoming) == 0,
|
||||
(audioMode & AudioMode.Outgoing) == 0).ConfigureAwait(false);
|
||||
}
|
||||
public Task ModifyAsync(Action<ModifyVoiceChannelParams> func)
|
||||
=> ChannelHelper.ModifyAsync(this, Discord, func);
|
||||
|
||||
public SocketVoiceChannel Clone() => MemberwiseClone() as SocketVoiceChannel;
|
||||
//IVoiceChannel
|
||||
Task<IAudioClient> IVoiceChannel.ConnectAsync() { throw new NotSupportedException(); }
|
||||
|
||||
ISocketChannel ISocketChannel.Clone() => Clone();
|
||||
//IGuildChannel
|
||||
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id)
|
||||
=> Task.FromResult<IGuildUser>(null);
|
||||
IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync()
|
||||
=> ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>().ToAsyncEnumerable();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Discord.Audio;
|
||||
using Discord.API.Rest;
|
||||
using Discord.Audio;
|
||||
using Discord.Rest;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
@@ -19,402 +20,204 @@ using VoiceStateModel = Discord.API.VoiceState;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
internal class SocketGuild : Guild, IGuild, IUserGuild
|
||||
public class SocketGuild : SocketEntity<ulong>, IGuild
|
||||
{
|
||||
internal override bool IsAttached => true;
|
||||
|
||||
private readonly SemaphoreSlim _audioLock;
|
||||
private TaskCompletionSource<bool> _syncPromise, _downloaderPromise;
|
||||
private TaskCompletionSource<AudioClient> _audioConnectPromise;
|
||||
private ConcurrentHashSet<ulong> _channels;
|
||||
private ConcurrentDictionary<ulong, SocketGuildUser> _members;
|
||||
private ConcurrentDictionary<ulong, VoiceState> _voiceStates;
|
||||
private ImmutableDictionary<ulong, RestRole> _roles;
|
||||
private ImmutableArray<Emoji> _emojis;
|
||||
private ImmutableArray<string> _features;
|
||||
internal bool _available;
|
||||
|
||||
public bool Available => _available && Discord.ConnectionState == ConnectionState.Connected;
|
||||
public int MemberCount { get; set; }
|
||||
public int DownloadedMemberCount { get; private set; }
|
||||
public AudioClient AudioClient { get; private set; }
|
||||
public string Name { get; private set; }
|
||||
public int AFKTimeout { get; private set; }
|
||||
public bool IsEmbeddable { get; private set; }
|
||||
public VerificationLevel VerificationLevel { get; private set; }
|
||||
public MfaLevel MfaLevel { get; private set; }
|
||||
public DefaultMessageNotifications DefaultMessageNotifications { get; private set; }
|
||||
|
||||
public bool HasAllMembers => _downloaderPromise.Task.IsCompleted;
|
||||
public bool IsSynced => _syncPromise.Task.IsCompleted;
|
||||
public Task SyncPromise => _syncPromise.Task;
|
||||
public Task DownloaderPromise => _downloaderPromise.Task;
|
||||
public ulong? AFKChannelId { get; private set; }
|
||||
public ulong? EmbedChannelId { get; private set; }
|
||||
public ulong OwnerId { get; private set; }
|
||||
public string VoiceRegionId { get; private set; }
|
||||
public string IconId { get; private set; }
|
||||
public string SplashId { get; private set; }
|
||||
|
||||
public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
|
||||
public SocketGuildUser CurrentUser => GetUser(Discord.CurrentUser.Id);
|
||||
public IReadOnlyCollection<ISocketGuildChannel> Channels
|
||||
public ulong DefaultChannelId => Id;
|
||||
public string IconUrl => API.CDN.GetGuildIconUrl(Id, IconId);
|
||||
public string SplashUrl => API.CDN.GetGuildSplashUrl(Id, SplashId);
|
||||
public bool IsSynced => false;
|
||||
|
||||
public RestRole EveryoneRole => GetRole(Id);
|
||||
public IReadOnlyCollection<RestRole> Roles => _roles.ToReadOnlyCollection();
|
||||
public IReadOnlyCollection<Emoji> Emojis => _emojis;
|
||||
public IReadOnlyCollection<string> Features => _features;
|
||||
|
||||
internal SocketGuild(DiscordSocketClient client, ulong id)
|
||||
: base(client, id)
|
||||
{
|
||||
get
|
||||
}
|
||||
internal static SocketGuild Create(DiscordSocketClient discord, Model model)
|
||||
{
|
||||
var entity = new SocketGuild(discord, model.Id);
|
||||
entity.Update(model);
|
||||
return entity;
|
||||
}
|
||||
internal void Update(Model model)
|
||||
{
|
||||
AFKChannelId = model.AFKChannelId;
|
||||
EmbedChannelId = model.EmbedChannelId;
|
||||
AFKTimeout = model.AFKTimeout;
|
||||
IsEmbeddable = model.EmbedEnabled;
|
||||
IconId = model.Icon;
|
||||
Name = model.Name;
|
||||
OwnerId = model.OwnerId;
|
||||
VoiceRegionId = model.Region;
|
||||
SplashId = model.Splash;
|
||||
VerificationLevel = model.VerificationLevel;
|
||||
MfaLevel = model.MfaLevel;
|
||||
DefaultMessageNotifications = model.DefaultMessageNotifications;
|
||||
|
||||
if (model.Emojis != null)
|
||||
{
|
||||
var channels = _channels;
|
||||
var store = Discord.DataStore;
|
||||
return channels.Select(x => store.GetChannel(x) as ISocketGuildChannel).Where(x => x != null).ToReadOnlyCollection(channels);
|
||||
var emojis = ImmutableArray.CreateBuilder<Emoji>(model.Emojis.Length);
|
||||
for (int i = 0; i < model.Emojis.Length; i++)
|
||||
emojis.Add(Emoji.Create(model.Emojis[i]));
|
||||
_emojis = emojis.ToImmutableArray();
|
||||
}
|
||||
}
|
||||
public IReadOnlyCollection<SocketGuildUser> Members => _members.ToReadOnlyCollection();
|
||||
public IEnumerable<KeyValuePair<ulong, VoiceState>> VoiceStates => _voiceStates;
|
||||
|
||||
public SocketGuild(DiscordSocketClient discord, ExtendedModel model, DataStore dataStore) : base(discord, model)
|
||||
{
|
||||
_audioLock = new SemaphoreSlim(1, 1);
|
||||
_syncPromise = new TaskCompletionSource<bool>();
|
||||
_downloaderPromise = new TaskCompletionSource<bool>();
|
||||
Update(model, dataStore);
|
||||
}
|
||||
else
|
||||
_emojis = ImmutableArray.Create<Emoji>();
|
||||
|
||||
public void Update(ExtendedModel model, DataStore dataStore)
|
||||
{
|
||||
if (source == UpdateSource.Rest && IsAttached) return;
|
||||
if (model.Features != null)
|
||||
_features = model.Features.ToImmutableArray();
|
||||
else
|
||||
_features = ImmutableArray.Create<string>();
|
||||
|
||||
_available = !(model.Unavailable ?? false);
|
||||
if (!_available)
|
||||
var roles = ImmutableDictionary.CreateBuilder<ulong, RestRole>();
|
||||
if (model.Roles != null)
|
||||
{
|
||||
if (_channels == null)
|
||||
_channels = new ConcurrentHashSet<ulong>();
|
||||
if (_members == null)
|
||||
_members = new ConcurrentDictionary<ulong, SocketGuildUser>();
|
||||
if (_roles == null)
|
||||
_roles = new ConcurrentDictionary<ulong, Role>();
|
||||
if (Emojis == null)
|
||||
Emojis = ImmutableArray.Create<Emoji>();
|
||||
if (Features == null)
|
||||
Features = ImmutableArray.Create<string>();
|
||||
return;
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
base.Update(model as Model, source);
|
||||
|
||||
var channels = new ConcurrentHashSet<ulong>(1, (int)(model.Channels.Length * 1.05));
|
||||
{
|
||||
for (int i = 0; i < model.Channels.Length; i++)
|
||||
AddChannel(model.Channels[i], dataStore, channels);
|
||||
}
|
||||
_channels = channels;
|
||||
|
||||
var members = new ConcurrentDictionary<ulong, SocketGuildUser>(1, (int)(model.Presences.Length * 1.05));
|
||||
{
|
||||
DownloadedMemberCount = 0;
|
||||
for (int i = 0; i < model.Members.Length; i++)
|
||||
AddOrUpdateUser(model.Members[i], dataStore, members);
|
||||
if (Discord.ApiClient.AuthTokenType != TokenType.User)
|
||||
{
|
||||
var _ = _syncPromise.TrySetResultAsync(true);
|
||||
if (!model.Large)
|
||||
_ = _downloaderPromise.TrySetResultAsync(true);
|
||||
}
|
||||
|
||||
for (int i = 0; i < model.Presences.Length; i++)
|
||||
AddOrUpdateUser(model.Presences[i], dataStore, members);
|
||||
}
|
||||
_members = members;
|
||||
MemberCount = model.MemberCount;
|
||||
|
||||
var voiceStates = new ConcurrentDictionary<ulong, VoiceState>(1, (int)(model.VoiceStates.Length * 1.05));
|
||||
{
|
||||
for (int i = 0; i < model.VoiceStates.Length; i++)
|
||||
AddOrUpdateVoiceState(model.VoiceStates[i], dataStore, voiceStates);
|
||||
}
|
||||
_voiceStates = voiceStates;
|
||||
}
|
||||
public void Update(GuildSyncModel model, DataStore dataStore)
|
||||
{
|
||||
if (source == UpdateSource.Rest && IsAttached) return;
|
||||
|
||||
var members = new ConcurrentDictionary<ulong, SocketGuildUser>(1, (int)(model.Presences.Length * 1.05));
|
||||
{
|
||||
DownloadedMemberCount = 0;
|
||||
for (int i = 0; i < model.Members.Length; i++)
|
||||
AddOrUpdateUser(model.Members[i], dataStore, members);
|
||||
var _ = _syncPromise.TrySetResultAsync(true);
|
||||
if (!model.Large)
|
||||
_ = _downloaderPromise.TrySetResultAsync(true);
|
||||
|
||||
for (int i = 0; i < model.Presences.Length; i++)
|
||||
AddOrUpdateUser(model.Presences[i], dataStore, members);
|
||||
}
|
||||
_members = members;
|
||||
_roles = roles.ToImmutable();
|
||||
}
|
||||
|
||||
public void Update(EmojiUpdateModel model)
|
||||
//General
|
||||
public async Task UpdateAsync()
|
||||
=> Update(await Discord.ApiClient.GetGuildAsync(Id));
|
||||
public Task DeleteAsync()
|
||||
=> GuildHelper.DeleteAsync(this, Discord);
|
||||
|
||||
public Task ModifyAsync(Action<ModifyGuildParams> func)
|
||||
=> GuildHelper.ModifyAsync(this, Discord, func);
|
||||
public Task ModifyEmbedAsync(Action<ModifyGuildEmbedParams> func)
|
||||
=> GuildHelper.ModifyEmbedAsync(this, Discord, func);
|
||||
public Task ModifyChannelsAsync(IEnumerable<ModifyGuildChannelsParams> args)
|
||||
=> GuildHelper.ModifyChannelsAsync(this, Discord, args);
|
||||
public Task ModifyRolesAsync(IEnumerable<ModifyGuildRolesParams> args)
|
||||
=> GuildHelper.ModifyRolesAsync(this, Discord, args);
|
||||
|
||||
public Task LeaveAsync()
|
||||
=> GuildHelper.LeaveAsync(this, Discord);
|
||||
|
||||
//Bans
|
||||
public Task<IReadOnlyCollection<RestBan>> GetBansAsync()
|
||||
=> GuildHelper.GetBansAsync(this, Discord);
|
||||
|
||||
public Task AddBanAsync(IUser user, int pruneDays = 0)
|
||||
=> GuildHelper.AddBanAsync(this, Discord, user.Id, pruneDays);
|
||||
public Task AddBanAsync(ulong userId, int pruneDays = 0)
|
||||
=> GuildHelper.AddBanAsync(this, Discord, userId, pruneDays);
|
||||
|
||||
public Task RemoveBanAsync(IUser user)
|
||||
=> GuildHelper.RemoveBanAsync(this, Discord, user.Id);
|
||||
public Task RemoveBanAsync(ulong userId)
|
||||
=> GuildHelper.RemoveBanAsync(this, Discord, userId);
|
||||
|
||||
//Channels
|
||||
public Task<IReadOnlyCollection<RestGuildChannel>> GetChannelsAsync()
|
||||
=> GuildHelper.GetChannelsAsync(this, Discord);
|
||||
public Task<RestGuildChannel> GetChannelAsync(ulong id)
|
||||
=> GuildHelper.GetChannelAsync(this, Discord, id);
|
||||
public Task<RestTextChannel> CreateTextChannelAsync(string name)
|
||||
=> GuildHelper.CreateTextChannelAsync(this, Discord, name);
|
||||
public Task<RestVoiceChannel> CreateVoiceChannelAsync(string name)
|
||||
=> GuildHelper.CreateVoiceChannelAsync(this, Discord, name);
|
||||
|
||||
//Integrations
|
||||
public Task<IReadOnlyCollection<RestGuildIntegration>> GetIntegrationsAsync()
|
||||
=> GuildHelper.GetIntegrationsAsync(this, Discord);
|
||||
public Task<RestGuildIntegration> CreateIntegrationAsync(ulong id, string type)
|
||||
=> GuildHelper.CreateIntegrationAsync(this, Discord, id, type);
|
||||
|
||||
//Invites
|
||||
public Task<IReadOnlyCollection<RestInviteMetadata>> GetInvitesAsync()
|
||||
=> GuildHelper.GetInvitesAsync(this, Discord);
|
||||
|
||||
//Roles
|
||||
public RestRole GetRole(ulong id)
|
||||
{
|
||||
if (source == UpdateSource.Rest && IsAttached) return;
|
||||
|
||||
var emojis = ImmutableArray.CreateBuilder<Emoji>(model.Emojis.Length);
|
||||
for (int i = 0; i < model.Emojis.Length; i++)
|
||||
emojis.Add(new Emoji(model.Emojis[i]));
|
||||
Emojis = emojis.ToImmutableArray();
|
||||
RestRole value;
|
||||
if (_roles.TryGetValue(id, out value))
|
||||
return value;
|
||||
return null;
|
||||
}
|
||||
|
||||
public override Task<IGuildChannel> GetChannelAsync(ulong id) => Task.FromResult<IGuildChannel>(GetChannel(id));
|
||||
public override Task<IReadOnlyCollection<IGuildChannel>> GetChannelsAsync() => Task.FromResult<IReadOnlyCollection<IGuildChannel>>(Channels);
|
||||
public ISocketGuildChannel AddChannel(ChannelModel model, DataStore dataStore, ConcurrentHashSet<ulong> channels = null)
|
||||
public async Task<IRole> CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?), bool isHoisted = false)
|
||||
{
|
||||
var channel = ToChannel(model);
|
||||
(channels ?? _channels).TryAdd(model.Id);
|
||||
dataStore.AddChannel(channel);
|
||||
return channel;
|
||||
}
|
||||
public ISocketGuildChannel GetChannel(ulong id)
|
||||
{
|
||||
return Discord.DataStore.GetChannel(id) as ISocketGuildChannel;
|
||||
}
|
||||
public ISocketGuildChannel RemoveChannel(ulong id)
|
||||
{
|
||||
_channels.TryRemove(id);
|
||||
return Discord.DataStore.RemoveChannel(id) as ISocketGuildChannel;
|
||||
}
|
||||
|
||||
public Role AddRole(RoleModel model, ConcurrentDictionary<ulong, Role> roles = null)
|
||||
{
|
||||
var role = new Role(this, model);
|
||||
(roles ?? _roles)[model.Id] = role;
|
||||
var role = await GuildHelper.CreateRoleAsync(this, Discord, name, permissions, color, isHoisted);
|
||||
_roles = _roles.Add(role.Id, role);
|
||||
return role;
|
||||
}
|
||||
public Role RemoveRole(ulong id)
|
||||
{
|
||||
Role role;
|
||||
if (_roles.TryRemove(id, out role))
|
||||
return role;
|
||||
return null;
|
||||
}
|
||||
|
||||
public override Task<IGuildUser> GetUserAsync(ulong id) => Task.FromResult<IGuildUser>(GetUser(id));
|
||||
public override Task<IGuildUser> GetCurrentUserAsync()
|
||||
=> Task.FromResult<IGuildUser>(CurrentUser);
|
||||
public override Task<IReadOnlyCollection<IGuildUser>> GetUsersAsync()
|
||||
=> Task.FromResult<IReadOnlyCollection<IGuildUser>>(Members);
|
||||
public SocketGuildUser AddOrUpdateUser(MemberModel model, DataStore dataStore, ConcurrentDictionary<ulong, SocketGuildUser> members = null)
|
||||
{
|
||||
members = members ?? _members;
|
||||
//Users
|
||||
public Task<IReadOnlyCollection<RestGuildUser>> GetUsersAsync()
|
||||
=> GuildHelper.GetUsersAsync(this, Discord);
|
||||
public Task<RestGuildUser> GetUserAsync(ulong id)
|
||||
=> GuildHelper.GetUserAsync(this, Discord, id);
|
||||
public Task<RestGuildUser> GetCurrentUserAsync()
|
||||
=> GuildHelper.GetUserAsync(this, Discord, Discord.CurrentUser.Id);
|
||||
|
||||
SocketGuildUser member;
|
||||
if (members.TryGetValue(model.User.Id, out member))
|
||||
member.Update(model, UpdateSource.WebSocket);
|
||||
else
|
||||
{
|
||||
var user = Discord.GetOrAddUser(model.User, dataStore);
|
||||
member = new SocketGuildUser(this, user, model);
|
||||
members[user.Id] = member;
|
||||
DownloadedMemberCount++;
|
||||
}
|
||||
return member;
|
||||
}
|
||||
public SocketGuildUser AddOrUpdateUser(PresenceModel model, DataStore dataStore, ConcurrentDictionary<ulong, SocketGuildUser> members = null)
|
||||
{
|
||||
members = members ?? _members;
|
||||
public Task<int> PruneUsersAsync(int days = 30, bool simulate = false)
|
||||
=> GuildHelper.PruneUsersAsync(this, Discord, days, simulate);
|
||||
|
||||
SocketGuildUser member;
|
||||
if (members.TryGetValue(model.User.Id, out member))
|
||||
member.Update(model, UpdateSource.WebSocket);
|
||||
else
|
||||
{
|
||||
var user = Discord.GetOrAddUser(model.User, dataStore);
|
||||
member = new SocketGuildUser(this, user, model);
|
||||
members[user.Id] = member;
|
||||
DownloadedMemberCount++;
|
||||
}
|
||||
return member;
|
||||
}
|
||||
public SocketGuildUser GetUser(ulong id)
|
||||
{
|
||||
SocketGuildUser member;
|
||||
if (_members.TryGetValue(id, out member))
|
||||
return member;
|
||||
return null;
|
||||
}
|
||||
public SocketGuildUser RemoveUser(ulong id)
|
||||
{
|
||||
SocketGuildUser member;
|
||||
if (_members.TryRemove(id, out member))
|
||||
{
|
||||
DownloadedMemberCount--;
|
||||
return member;
|
||||
}
|
||||
member.User.RemoveRef(Discord);
|
||||
return null;
|
||||
}
|
||||
public override async Task DownloadUsersAsync()
|
||||
{
|
||||
await Discord.DownloadUsersAsync(new [] { this });
|
||||
}
|
||||
public void CompleteDownloadMembers()
|
||||
{
|
||||
_downloaderPromise.TrySetResultAsync(true);
|
||||
}
|
||||
//IGuild
|
||||
bool IGuild.Available => true;
|
||||
IAudioClient IGuild.AudioClient => null;
|
||||
IReadOnlyCollection<IGuildUser> IGuild.CachedUsers => ImmutableArray.Create<IGuildUser>();
|
||||
IRole IGuild.EveryoneRole => EveryoneRole;
|
||||
IReadOnlyCollection<IRole> IGuild.Roles => Roles;
|
||||
|
||||
public VoiceState AddOrUpdateVoiceState(VoiceStateModel model, DataStore dataStore, ConcurrentDictionary<ulong, VoiceState> voiceStates = null)
|
||||
{
|
||||
var voiceChannel = dataStore.GetChannel(model.ChannelId.Value) as SocketVoiceChannel;
|
||||
var voiceState = new VoiceState(voiceChannel, model);
|
||||
(voiceStates ?? _voiceStates)[model.UserId] = voiceState;
|
||||
return voiceState;
|
||||
}
|
||||
public VoiceState? GetVoiceState(ulong id)
|
||||
{
|
||||
VoiceState voiceState;
|
||||
if (_voiceStates.TryGetValue(id, out voiceState))
|
||||
return voiceState;
|
||||
return null;
|
||||
}
|
||||
public VoiceState? RemoveVoiceState(ulong id)
|
||||
{
|
||||
VoiceState voiceState;
|
||||
if (_voiceStates.TryRemove(id, out voiceState))
|
||||
return voiceState;
|
||||
return null;
|
||||
}
|
||||
async Task<IReadOnlyCollection<IBan>> IGuild.GetBansAsync()
|
||||
=> await GetBansAsync();
|
||||
|
||||
public async Task<IAudioClient> ConnectAudioAsync(ulong channelId, bool selfDeaf, bool selfMute)
|
||||
{
|
||||
try
|
||||
{
|
||||
TaskCompletionSource<AudioClient> promise;
|
||||
async Task<IReadOnlyCollection<IGuildChannel>> IGuild.GetChannelsAsync()
|
||||
=> await GetChannelsAsync();
|
||||
async Task<IGuildChannel> IGuild.GetChannelAsync(ulong id)
|
||||
=> await GetChannelAsync(id);
|
||||
IGuildChannel IGuild.GetCachedChannel(ulong id)
|
||||
=> null;
|
||||
async Task<ITextChannel> IGuild.CreateTextChannelAsync(string name)
|
||||
=> await CreateTextChannelAsync(name);
|
||||
async Task<IVoiceChannel> IGuild.CreateVoiceChannelAsync(string name)
|
||||
=> await CreateVoiceChannelAsync(name);
|
||||
|
||||
await _audioLock.WaitAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await DisconnectAudioInternalAsync().ConfigureAwait(false);
|
||||
promise = new TaskCompletionSource<AudioClient>();
|
||||
_audioConnectPromise = promise;
|
||||
await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, channelId, selfDeaf, selfMute).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_audioLock.Release();
|
||||
}
|
||||
async Task<IReadOnlyCollection<IGuildIntegration>> IGuild.GetIntegrationsAsync()
|
||||
=> await GetIntegrationsAsync();
|
||||
async Task<IGuildIntegration> IGuild.CreateIntegrationAsync(ulong id, string type)
|
||||
=> await CreateIntegrationAsync(id, type);
|
||||
|
||||
var timeoutTask = Task.Delay(15000);
|
||||
if (await Task.WhenAny(promise.Task, timeoutTask) == timeoutTask)
|
||||
throw new TimeoutException();
|
||||
return await promise.Task.ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
await DisconnectAudioInternalAsync().ConfigureAwait(false);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public async Task DisconnectAudioAsync(AudioClient client = null)
|
||||
{
|
||||
await _audioLock.WaitAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await DisconnectAudioInternalAsync(client).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_audioLock.Release();
|
||||
}
|
||||
}
|
||||
private async Task DisconnectAudioInternalAsync(AudioClient client = null)
|
||||
{
|
||||
var oldClient = AudioClient;
|
||||
if (oldClient != null)
|
||||
{
|
||||
if (client == null || oldClient == client)
|
||||
{
|
||||
_audioConnectPromise?.TrySetCanceledAsync(); //Cancel any previous audio connection
|
||||
_audioConnectPromise = null;
|
||||
}
|
||||
if (oldClient == client)
|
||||
{
|
||||
AudioClient = null;
|
||||
await oldClient.DisconnectAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
public async Task FinishConnectAudio(int id, string url, string token)
|
||||
{
|
||||
var voiceState = GetVoiceState(CurrentUser.Id).Value;
|
||||
async Task<IReadOnlyCollection<IInviteMetadata>> IGuild.GetInvitesAsync()
|
||||
=> await GetInvitesAsync();
|
||||
|
||||
await _audioLock.WaitAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
if (AudioClient == null)
|
||||
{
|
||||
var audioClient = new AudioClient(this, id);
|
||||
audioClient.Disconnected += async ex =>
|
||||
{
|
||||
await _audioLock.WaitAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
if (AudioClient == audioClient) //Only reconnect if we're still assigned as this guild's audio client
|
||||
{
|
||||
if (ex != null)
|
||||
{
|
||||
//Reconnect if we still have channel info.
|
||||
//TODO: Is this threadsafe? Could channel data be deleted before we access it?
|
||||
var voiceState2 = GetVoiceState(CurrentUser.Id);
|
||||
if (voiceState2.HasValue)
|
||||
{
|
||||
var voiceChannelId = voiceState2.Value.VoiceChannel?.Id;
|
||||
if (voiceChannelId != null)
|
||||
await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, voiceChannelId, voiceState2.Value.IsSelfDeafened, voiceState2.Value.IsSelfMuted);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try { AudioClient.Dispose(); } catch { }
|
||||
AudioClient = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_audioLock.Release();
|
||||
}
|
||||
};
|
||||
AudioClient = audioClient;
|
||||
}
|
||||
await AudioClient.ConnectAsync(url, CurrentUser.Id, voiceState.VoiceSessionId, token).ConfigureAwait(false);
|
||||
await _audioConnectPromise.TrySetResultAsync(AudioClient).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
await DisconnectAudioAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await _audioConnectPromise.SetExceptionAsync(e).ConfigureAwait(false);
|
||||
await DisconnectAudioAsync();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_audioLock.Release();
|
||||
}
|
||||
}
|
||||
public async Task FinishJoinAudioChannel()
|
||||
{
|
||||
await _audioLock.WaitAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
if (AudioClient != null)
|
||||
await _audioConnectPromise.TrySetResultAsync(AudioClient).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_audioLock.Release();
|
||||
}
|
||||
}
|
||||
IRole IGuild.GetRole(ulong id)
|
||||
=> GetRole(id);
|
||||
|
||||
public SocketGuild Clone() => MemberwiseClone() as SocketGuild;
|
||||
|
||||
new internal ISocketGuildChannel ToChannel(ChannelModel model)
|
||||
{
|
||||
switch (model.Type)
|
||||
{
|
||||
case ChannelType.Text:
|
||||
return new SocketTextChannel(this, model);
|
||||
case ChannelType.Voice:
|
||||
return new SocketVoiceChannel(this, model);
|
||||
default:
|
||||
throw new InvalidOperationException($"Unexpected channel type: {model.Type}");
|
||||
}
|
||||
}
|
||||
|
||||
bool IUserGuild.IsOwner => OwnerId == Discord.CurrentUser.Id;
|
||||
GuildPermissions IUserGuild.Permissions => CurrentUser.GuildPermissions;
|
||||
IAudioClient IGuild.AudioClient => AudioClient;
|
||||
async Task<IReadOnlyCollection<IGuildUser>> IGuild.GetUsersAsync()
|
||||
=> await GetUsersAsync();
|
||||
async Task<IGuildUser> IGuild.GetUserAsync(ulong id)
|
||||
=> await GetUserAsync(id);
|
||||
IGuildUser IGuild.GetCachedUser(ulong id)
|
||||
=> null;
|
||||
async Task<IGuildUser> IGuild.GetCurrentUserAsync()
|
||||
=> await GetCurrentUserAsync();
|
||||
Task IGuild.DownloadUsersAsync() { throw new NotSupportedException(); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
using Discord.API.Rest;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using Model = Discord.API.Integration;
|
||||
|
||||
namespace Discord.Rest
|
||||
{
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
internal class GuildIntegration : IEntity<ulong>, IGuildIntegration
|
||||
{
|
||||
private long _syncedAtTicks;
|
||||
|
||||
public string Name { get; private set; }
|
||||
public string Type { get; private set; }
|
||||
public bool IsEnabled { get; private set; }
|
||||
public bool IsSyncing { get; private set; }
|
||||
public ulong ExpireBehavior { get; private set; }
|
||||
public ulong ExpireGracePeriod { get; private set; }
|
||||
|
||||
public Guild Guild { get; private set; }
|
||||
public Role Role { get; private set; }
|
||||
public User User { get; private set; }
|
||||
public IntegrationAccount Account { get; private set; }
|
||||
|
||||
public override DiscordRestClient Discord => Guild.Discord;
|
||||
public DateTimeOffset SyncedAt => DateTimeUtils.FromTicks(_syncedAtTicks);
|
||||
|
||||
public GuildIntegration(Guild guild, Model model)
|
||||
: base(model.Id)
|
||||
{
|
||||
Guild = guild;
|
||||
Update(model);
|
||||
}
|
||||
|
||||
public void Update(Model model)
|
||||
{
|
||||
Name = model.Name;
|
||||
Type = model.Type;
|
||||
IsEnabled = model.Enabled;
|
||||
IsSyncing = model.Syncing;
|
||||
ExpireBehavior = model.ExpireBehavior;
|
||||
ExpireGracePeriod = model.ExpireGracePeriod;
|
||||
_syncedAtTicks = model.SyncedAt.UtcTicks;
|
||||
|
||||
Role = Guild.GetRole(model.RoleId);
|
||||
User = new User(model.User);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync()
|
||||
{
|
||||
await Discord.ApiClient.DeleteGuildIntegrationAsync(Guild.Id, Id).ConfigureAwait(false);
|
||||
}
|
||||
public async Task ModifyAsync(Action<ModifyGuildIntegrationParams> func)
|
||||
{
|
||||
if (func == null) throw new NullReferenceException(nameof(func));
|
||||
|
||||
var args = new ModifyGuildIntegrationParams();
|
||||
func(args);
|
||||
var model = await Discord.ApiClient.ModifyGuildIntegrationAsync(Guild.Id, Id, args).ConfigureAwait(false);
|
||||
|
||||
Update(model, UpdateSource.Rest);
|
||||
}
|
||||
public async Task SyncAsync()
|
||||
{
|
||||
await Discord.ApiClient.SyncGuildIntegrationAsync(Guild.Id, Id).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override string ToString() => Name;
|
||||
private string DebuggerDisplay => $"{Name} ({Id}{(IsEnabled ? ", Enabled" : "")})";
|
||||
|
||||
IGuild IGuildIntegration.Guild => Guild;
|
||||
IUser IGuildIntegration.User => User;
|
||||
IRole IGuildIntegration.Role => Role;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using Model = Discord.API.Message;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
internal interface ISocketMessage : IMessage
|
||||
{
|
||||
DiscordSocketClient Discord { get; }
|
||||
new ISocketMessageChannel Channel { get; }
|
||||
|
||||
void Update(Model model);
|
||||
ISocketMessage Clone();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using Discord.Rest;
|
||||
using Discord.WebSocket;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
@@ -7,53 +9,52 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
internal class MessageCache : MessageManager
|
||||
internal class MessageCache
|
||||
{
|
||||
private readonly ConcurrentDictionary<ulong, ISocketMessage> _messages;
|
||||
private readonly ConcurrentDictionary<ulong, SocketMessage> _messages;
|
||||
private readonly ConcurrentQueue<ulong> _orderedMessages;
|
||||
private readonly int _size;
|
||||
|
||||
public override IReadOnlyCollection<ISocketMessage> Messages => _messages.ToReadOnlyCollection();
|
||||
public IReadOnlyCollection<SocketMessage> Messages => _messages.ToReadOnlyCollection();
|
||||
|
||||
public MessageCache(DiscordSocketClient discord, ISocketMessageChannel channel)
|
||||
: base(discord, channel)
|
||||
public MessageCache(DiscordSocketClient discord, IChannel channel)
|
||||
{
|
||||
_size = discord.MessageCacheSize;
|
||||
_messages = new ConcurrentDictionary<ulong, ISocketMessage>(1, (int)(_size * 1.05));
|
||||
_messages = new ConcurrentDictionary<ulong, SocketMessage>(1, (int)(_size * 1.05));
|
||||
_orderedMessages = new ConcurrentQueue<ulong>();
|
||||
}
|
||||
|
||||
public override void Add(ISocketMessage message)
|
||||
public void Add(SocketMessage message)
|
||||
{
|
||||
if (_messages.TryAdd(message.Id, message))
|
||||
{
|
||||
_orderedMessages.Enqueue(message.Id);
|
||||
|
||||
ulong msgId;
|
||||
ISocketMessage msg;
|
||||
SocketMessage msg;
|
||||
while (_orderedMessages.Count > _size && _orderedMessages.TryDequeue(out msgId))
|
||||
_messages.TryRemove(msgId, out msg);
|
||||
}
|
||||
}
|
||||
|
||||
public override ISocketMessage Remove(ulong id)
|
||||
public SocketMessage Remove(ulong id)
|
||||
{
|
||||
ISocketMessage msg;
|
||||
SocketMessage msg;
|
||||
_messages.TryRemove(id, out msg);
|
||||
return msg;
|
||||
}
|
||||
|
||||
public override ISocketMessage Get(ulong id)
|
||||
public SocketMessage Get(ulong id)
|
||||
{
|
||||
ISocketMessage result;
|
||||
SocketMessage result;
|
||||
if (_messages.TryGetValue(id, out result))
|
||||
return result;
|
||||
return null;
|
||||
}
|
||||
public override IImmutableList<ISocketMessage> GetMany(ulong? fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
|
||||
public IImmutableList<SocketMessage> GetMany(ulong? fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
|
||||
{
|
||||
if (limit < 0) throw new ArgumentOutOfRangeException(nameof(limit));
|
||||
if (limit == 0) return ImmutableArray<ISocketMessage>.Empty;
|
||||
if (limit == 0) return ImmutableArray<SocketMessage>.Empty;
|
||||
|
||||
IEnumerable<ulong> cachedMessageIds;
|
||||
if (fromMessageId == null)
|
||||
@@ -67,7 +68,7 @@ namespace Discord.WebSocket
|
||||
.Take(limit)
|
||||
.Select(x =>
|
||||
{
|
||||
ISocketMessage msg;
|
||||
SocketMessage msg;
|
||||
if (_messages.TryGetValue(x, out msg))
|
||||
return msg;
|
||||
return null;
|
||||
@@ -75,13 +76,5 @@ namespace Discord.WebSocket
|
||||
.Where(x => x != null)
|
||||
.ToImmutableArray();
|
||||
}
|
||||
|
||||
public override async Task<ISocketMessage> DownloadAsync(ulong id)
|
||||
{
|
||||
var msg = Get(id);
|
||||
if (msg != null)
|
||||
return msg;
|
||||
return await base.DownloadAsync(id).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
64
src/Discord.Net.WebSocket/Entities/Messages/SocketMessage.cs
Normal file
64
src/Discord.Net.WebSocket/Entities/Messages/SocketMessage.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using Model = Discord.API.Message;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
public abstract class SocketMessage : SocketEntity<ulong>, IMessage, IUpdateable
|
||||
{
|
||||
private long _timestampTicks;
|
||||
|
||||
public ulong ChannelId { get; }
|
||||
public SocketUser Author { get; }
|
||||
|
||||
public string Content { get; private set; }
|
||||
|
||||
public virtual bool IsTTS => false;
|
||||
public virtual bool IsPinned => false;
|
||||
public virtual DateTimeOffset? EditedTimestamp => null;
|
||||
|
||||
public virtual IReadOnlyCollection<IAttachment> Attachments => ImmutableArray.Create<IAttachment>();
|
||||
public virtual IReadOnlyCollection<IEmbed> Embeds => ImmutableArray.Create<IEmbed>();
|
||||
public virtual IReadOnlyCollection<ulong> MentionedChannelIds => ImmutableArray.Create<ulong>();
|
||||
public virtual IReadOnlyCollection<IRole> MentionedRoles => ImmutableArray.Create<IRole>();
|
||||
public virtual IReadOnlyCollection<IUser> MentionedUsers => ImmutableArray.Create<IUser>();
|
||||
|
||||
public DateTimeOffset Timestamp => DateTimeUtils.FromTicks(_timestampTicks);
|
||||
|
||||
internal SocketMessage(DiscordSocketClient discord, ulong id, ulong channelId, SocketUser author)
|
||||
: base(discord, id)
|
||||
{
|
||||
ChannelId = channelId;
|
||||
Author = author;
|
||||
}
|
||||
internal static SocketMessage Create(DiscordSocketClient discord, SocketUser author, Model model)
|
||||
{
|
||||
if (model.Type == MessageType.Default)
|
||||
return SocketUserMessage.Create(discord, author, model);
|
||||
else
|
||||
return SocketSystemMessage.Create(discord, author, model);
|
||||
}
|
||||
internal virtual void Update(Model model)
|
||||
{
|
||||
if (model.Timestamp.IsSpecified)
|
||||
_timestampTicks = model.Timestamp.Value.UtcTicks;
|
||||
|
||||
if (model.Content.IsSpecified)
|
||||
Content = model.Content.Value;
|
||||
}
|
||||
|
||||
public async Task UpdateAsync()
|
||||
{
|
||||
var model = await Discord.ApiClient.GetChannelMessageAsync(ChannelId, Id).ConfigureAwait(false);
|
||||
Update(model);
|
||||
}
|
||||
|
||||
//IMessage
|
||||
IUser IMessage.Author => Author;
|
||||
MessageType IMessage.Type => MessageType.Default;
|
||||
}
|
||||
}
|
||||
@@ -3,18 +3,25 @@ using Model = Discord.API.Message;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
internal class SocketSystemMessage : SystemMessage, ISocketMessage
|
||||
internal class SocketSystemMessage : SocketMessage, ISystemMessage
|
||||
{
|
||||
internal override bool IsAttached => true;
|
||||
public MessageType Type { get; private set; }
|
||||
|
||||
public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
|
||||
public new ISocketMessageChannel Channel => base.Channel as ISocketMessageChannel;
|
||||
|
||||
public SocketSystemMessage(ISocketMessageChannel channel, IUser author, Model model)
|
||||
: base(channel, author, model)
|
||||
internal SocketSystemMessage(DiscordSocketClient discord, ulong id, ulong channelId, SocketUser author)
|
||||
: base(discord, id, channelId, author)
|
||||
{
|
||||
}
|
||||
internal new static SocketSystemMessage Create(DiscordSocketClient discord, SocketUser author, Model model)
|
||||
{
|
||||
var entity = new SocketSystemMessage(discord, model.Id, model.ChannelId, author);
|
||||
entity.Update(model);
|
||||
return entity;
|
||||
}
|
||||
internal override void Update(Model model)
|
||||
{
|
||||
base.Update(model);
|
||||
|
||||
public ISocketMessage Clone() => MemberwiseClone() as ISocketMessage;
|
||||
Type = model.Type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,133 @@
|
||||
using Discord.Rest;
|
||||
using Discord.API.Rest;
|
||||
using Discord.Rest;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Threading.Tasks;
|
||||
using Model = Discord.API.Message;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
internal class SocketUserMessage : UserMessage, ISocketMessage
|
||||
internal class SocketUserMessage : SocketMessage, IUserMessage
|
||||
{
|
||||
internal override bool IsAttached => true;
|
||||
private bool _isMentioningEveryone, _isTTS, _isPinned;
|
||||
private long? _editedTimestampTicks;
|
||||
private ImmutableArray<RestAttachment> _attachments;
|
||||
private ImmutableArray<RestEmbed> _embeds;
|
||||
private ImmutableArray<ulong> _mentionedChannelIds;
|
||||
private ImmutableArray<RestRole> _mentionedRoles;
|
||||
private ImmutableArray<RestUser> _mentionedUsers;
|
||||
|
||||
public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
|
||||
public new ISocketMessageChannel Channel => base.Channel as ISocketMessageChannel;
|
||||
public override bool IsTTS => _isTTS;
|
||||
public override bool IsPinned => _isPinned;
|
||||
public override DateTimeOffset? EditedTimestamp => DateTimeUtils.FromTicks(_editedTimestampTicks);
|
||||
|
||||
public SocketUserMessage(ISocketMessageChannel channel, IUser author, Model model)
|
||||
: base(channel, author, model)
|
||||
public override IReadOnlyCollection<IAttachment> Attachments => _attachments;
|
||||
public override IReadOnlyCollection<IEmbed> Embeds => _embeds;
|
||||
public override IReadOnlyCollection<ulong> MentionedChannelIds => _mentionedChannelIds;
|
||||
public override IReadOnlyCollection<IRole> MentionedRoles => _mentionedRoles;
|
||||
public override IReadOnlyCollection<IUser> MentionedUsers => _mentionedUsers;
|
||||
|
||||
internal SocketUserMessage(DiscordSocketClient discord, ulong id, ulong channelId, SocketUser author)
|
||||
: base(discord, id, channelId, author)
|
||||
{
|
||||
}
|
||||
internal new static SocketUserMessage Create(DiscordSocketClient discord, SocketUser author, Model model)
|
||||
{
|
||||
var entity = new SocketUserMessage(discord, model.Id, model.ChannelId, author);
|
||||
entity.Update(model);
|
||||
return entity;
|
||||
}
|
||||
|
||||
public ISocketMessage Clone() => MemberwiseClone() as ISocketMessage;
|
||||
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.Attachments.IsSpecified)
|
||||
{
|
||||
var value = model.Attachments.Value;
|
||||
if (value.Length > 0)
|
||||
{
|
||||
var attachments = ImmutableArray.CreateBuilder<RestAttachment>(value.Length);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
attachments.Add(RestAttachment.Create(value[i]));
|
||||
_attachments = attachments.ToImmutable();
|
||||
}
|
||||
else
|
||||
_attachments = ImmutableArray.Create<RestAttachment>();
|
||||
}
|
||||
|
||||
if (model.Embeds.IsSpecified)
|
||||
{
|
||||
var value = model.Embeds.Value;
|
||||
if (value.Length > 0)
|
||||
{
|
||||
var embeds = ImmutableArray.CreateBuilder<RestEmbed>(value.Length);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
embeds.Add(RestEmbed.Create(value[i]));
|
||||
_embeds = embeds.ToImmutable();
|
||||
}
|
||||
else
|
||||
_embeds = ImmutableArray.Create<RestEmbed>();
|
||||
}
|
||||
|
||||
ImmutableArray<RestUser> mentions = ImmutableArray.Create<RestUser>();
|
||||
if (model.Mentions.IsSpecified)
|
||||
{
|
||||
var value = model.Mentions.Value;
|
||||
if (value.Length > 0)
|
||||
{
|
||||
var newMentions = ImmutableArray.CreateBuilder<RestUser>(value.Length);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
newMentions.Add(RestUser.Create(Discord, value[i]));
|
||||
mentions = newMentions.ToImmutable();
|
||||
}
|
||||
}
|
||||
|
||||
if (model.Content.IsSpecified)
|
||||
{
|
||||
var text = model.Content.Value;
|
||||
|
||||
_mentionedUsers = MentionsHelper.GetUserMentions(text, null, mentions);
|
||||
_mentionedChannelIds = MentionsHelper.GetChannelMentions(text, null);
|
||||
_mentionedRoles = MentionsHelper.GetRoleMentions<RestRole>(text, null);
|
||||
model.Content = text;
|
||||
}
|
||||
}
|
||||
|
||||
public Task ModifyAsync(Action<ModifyMessageParams> func)
|
||||
=> MessageHelper.ModifyAsync(this, Discord, func);
|
||||
public Task DeleteAsync()
|
||||
=> MessageHelper.DeleteAsync(this, Discord);
|
||||
|
||||
public Task PinAsync()
|
||||
=> MessageHelper.PinAsync(this, Discord);
|
||||
public Task UnpinAsync()
|
||||
=> MessageHelper.UnpinAsync(this, Discord);
|
||||
|
||||
public string Resolve(UserMentionHandling userHandling = UserMentionHandling.Name, ChannelMentionHandling channelHandling = ChannelMentionHandling.Name,
|
||||
RoleMentionHandling roleHandling = RoleMentionHandling.Name, EveryoneMentionHandling everyoneHandling = EveryoneMentionHandling.Ignore)
|
||||
=> Resolve(Content, userHandling, channelHandling, roleHandling, everyoneHandling);
|
||||
public string Resolve(int startIndex, int length, UserMentionHandling userHandling = UserMentionHandling.Name, ChannelMentionHandling channelHandling = ChannelMentionHandling.Name,
|
||||
RoleMentionHandling roleHandling = RoleMentionHandling.Name, EveryoneMentionHandling everyoneHandling = EveryoneMentionHandling.Ignore)
|
||||
=> Resolve(Content.Substring(startIndex, length), userHandling, channelHandling, roleHandling, everyoneHandling);
|
||||
public string Resolve(string text, UserMentionHandling userHandling, ChannelMentionHandling channelHandling,
|
||||
RoleMentionHandling roleHandling, EveryoneMentionHandling everyoneHandling)
|
||||
{
|
||||
text = MentionsHelper.ResolveUserMentions(text, null, MentionedUsers, userHandling);
|
||||
text = MentionsHelper.ResolveChannelMentions(text, null, channelHandling);
|
||||
text = MentionsHelper.ResolveRoleMentions(text, MentionedRoles, roleHandling);
|
||||
text = MentionsHelper.ResolveEveryoneMentions(text, everyoneHandling);
|
||||
return text;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
19
src/Discord.Net.WebSocket/Entities/SocketEntity.cs
Normal file
19
src/Discord.Net.WebSocket/Entities/SocketEntity.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
public abstract class SocketEntity<T> : IEntity<T>
|
||||
where T : IEquatable<T>
|
||||
{
|
||||
public DiscordSocketClient Discord { get; }
|
||||
public T Id { get; }
|
||||
|
||||
internal SocketEntity(DiscordSocketClient discord, T id)
|
||||
{
|
||||
Discord = discord;
|
||||
Id = id;
|
||||
}
|
||||
|
||||
IDiscordClient IEntity<T>.Discord => Discord;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
internal interface ISocketUser : IUser, IEntity<ulong>
|
||||
{
|
||||
SocketGlobalUser User { get; }
|
||||
|
||||
ISocketUser Clone();
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
//TODO: C#7 Candidate for record type
|
||||
internal struct Presence : IPresence
|
||||
{
|
||||
public Game Game { get; }
|
||||
public UserStatus Status { get; }
|
||||
|
||||
public Presence(Game game, UserStatus status)
|
||||
{
|
||||
Game = game;
|
||||
Status = status;
|
||||
}
|
||||
|
||||
public Presence Clone() => this;
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using PresenceModel = Discord.API.Presence;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
[DebuggerDisplay("{DebuggerDisplay,nq}")]
|
||||
internal class SocketDMUser : ISocketUser
|
||||
{
|
||||
internal bool IsAttached => true;
|
||||
bool IEntity<ulong>.IsAttached => IsAttached;
|
||||
|
||||
public SocketGlobalUser User { get; }
|
||||
|
||||
public DiscordSocketClient Discord => User.Discord;
|
||||
|
||||
public Game Game => Presence.Game;
|
||||
public UserStatus Status => Presence.Status;
|
||||
public Presence Presence => User.Presence; //{ get; private set; }
|
||||
|
||||
public ulong Id => User.Id;
|
||||
public string AvatarUrl => User.AvatarUrl;
|
||||
public DateTimeOffset CreatedAt => User.CreatedAt;
|
||||
public string Discriminator => User.Discriminator;
|
||||
public ushort DiscriminatorValue => User.DiscriminatorValue;
|
||||
public bool IsBot => User.IsBot;
|
||||
public string Mention => MentionUtils.Mention(this);
|
||||
public string Username => User.Username;
|
||||
|
||||
public SocketDMUser(SocketGlobalUser user)
|
||||
{
|
||||
User = user;
|
||||
}
|
||||
|
||||
public void Update(PresenceModel model)
|
||||
{
|
||||
User.Update(model, source);
|
||||
}
|
||||
|
||||
public SocketDMUser Clone() => MemberwiseClone() as SocketDMUser;
|
||||
ISocketUser ISocketUser.Clone() => Clone();
|
||||
|
||||
public override string ToString() => $"{Username}#{Discriminator}";
|
||||
private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id})";
|
||||
}
|
||||
}
|
||||
@@ -1,61 +1,10 @@
|
||||
using Discord.Rest;
|
||||
using System;
|
||||
using Model = Discord.API.User;
|
||||
using PresenceModel = Discord.API.Presence;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
internal class SocketGlobalUser : User, ISocketUser
|
||||
internal class SocketGlobalUser : SocketUser
|
||||
{
|
||||
internal override bool IsAttached => true;
|
||||
private readonly object _lockObj = new object();
|
||||
|
||||
private ushort _references;
|
||||
|
||||
public Presence Presence { get; private set; }
|
||||
|
||||
public new DiscordSocketClient Discord { get { throw new NotSupportedException(); } }
|
||||
SocketGlobalUser ISocketUser.User => this;
|
||||
|
||||
public SocketGlobalUser(Model model)
|
||||
: base(model)
|
||||
internal SocketGlobalUser(DiscordSocketClient discord, ulong id)
|
||||
: base(discord, id)
|
||||
{
|
||||
}
|
||||
|
||||
public void AddRef()
|
||||
{
|
||||
checked
|
||||
{
|
||||
lock (_lockObj)
|
||||
_references++;
|
||||
}
|
||||
}
|
||||
public void RemoveRef(DiscordSocketClient discord)
|
||||
{
|
||||
lock (_lockObj)
|
||||
{
|
||||
if (--_references == 0)
|
||||
discord.RemoveUser(Id);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Update(Model model)
|
||||
{
|
||||
lock (_lockObj)
|
||||
base.Update(model, source);
|
||||
}
|
||||
public void Update(PresenceModel model)
|
||||
{
|
||||
//Race conditions are okay here. Multiple shards racing already cant guarantee presence in order.
|
||||
|
||||
//lock (_lockObj)
|
||||
//{
|
||||
var game = model.Game != null ? new Game(model.Game) : null;
|
||||
Presence = new Presence(game, model.Status);
|
||||
//}
|
||||
}
|
||||
|
||||
public SocketGlobalUser Clone() => MemberwiseClone() as SocketGlobalUser;
|
||||
ISocketUser ISocketUser.Clone() => Clone();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +1,30 @@
|
||||
using Discord.Rest;
|
||||
using System.Diagnostics;
|
||||
using Model = Discord.API.User;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
[DebuggerDisplay("{DebuggerDisplay,nq}")]
|
||||
internal class SocketGroupUser : GroupUser, ISocketUser
|
||||
public class SocketGroupUser : SocketUser, IGroupUser
|
||||
{
|
||||
internal override bool IsAttached => true;
|
||||
|
||||
public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
|
||||
public new SocketGroupChannel Channel => base.Channel as SocketGroupChannel;
|
||||
public new SocketGlobalUser User => base.User as SocketGlobalUser;
|
||||
public Presence Presence => User.Presence; //{ get; private set; }
|
||||
|
||||
public override Game Game => Presence.Game;
|
||||
public override UserStatus Status => Presence.Status;
|
||||
|
||||
public VoiceState? VoiceState => Channel.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 SocketGroupUser(SocketGroupChannel channel, SocketGlobalUser user)
|
||||
: base(channel, user)
|
||||
internal SocketGroupUser(DiscordSocketClient discord, ulong id)
|
||||
: base(discord, id)
|
||||
{
|
||||
}
|
||||
internal new static SocketGroupUser Create(DiscordSocketClient discord, Model model)
|
||||
{
|
||||
var entity = new SocketGroupUser(discord, model.Id);
|
||||
entity.Update(model);
|
||||
return entity;
|
||||
}
|
||||
|
||||
public SocketGroupUser Clone() => MemberwiseClone() as SocketGroupUser;
|
||||
ISocketUser ISocketUser.Clone() => Clone();
|
||||
|
||||
public override string ToString() => $"{Username}#{Discriminator}";
|
||||
private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id})";
|
||||
//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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,53 +1,74 @@
|
||||
using Discord.Rest;
|
||||
using Discord.API.Rest;
|
||||
using Discord.Rest;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Threading.Tasks;
|
||||
using Model = Discord.API.GuildMember;
|
||||
using PresenceModel = Discord.API.Presence;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
internal class SocketGuildUser : GuildUser, ISocketUser, IVoiceState
|
||||
internal class SocketGuildUser : SocketUser, IGuildUser
|
||||
{
|
||||
internal override bool IsAttached => true;
|
||||
private long? _joinedAtTicks;
|
||||
private ImmutableArray<ulong> _roleIds;
|
||||
|
||||
public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
|
||||
public new SocketGuild Guild => base.Guild as SocketGuild;
|
||||
public new SocketGlobalUser User => base.User as SocketGlobalUser;
|
||||
public Presence Presence => User.Presence; //{ get; private set; }
|
||||
public string Nickname { get; private set; }
|
||||
public ulong GuildId { get; private set; }
|
||||
|
||||
public override Game Game => Presence.Game;
|
||||
public override UserStatus Status => Presence.Status;
|
||||
public IReadOnlyCollection<ulong> RoleIds => _roleIds;
|
||||
|
||||
public VoiceState? 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);
|
||||
|
||||
public SocketGuildUser(SocketGuild guild, SocketGlobalUser user, Model model)
|
||||
: base(guild, user, model)
|
||||
{
|
||||
//Presence = new Presence(null, UserStatus.Offline);
|
||||
}
|
||||
public SocketGuildUser(SocketGuild guild, SocketGlobalUser user, PresenceModel model)
|
||||
: base(guild, user, model)
|
||||
internal SocketGuildUser(DiscordSocketClient discord, ulong id)
|
||||
: base(discord, id)
|
||||
{
|
||||
}
|
||||
|
||||
public override void Update(PresenceModel model)
|
||||
internal static SocketGuildUser Create(DiscordSocketClient discord, Model model)
|
||||
{
|
||||
base.Update(model, source);
|
||||
|
||||
var game = model.Game != null ? new Game(model.Game) : null;
|
||||
//Presence = new Presence(game, model.Status);
|
||||
|
||||
User.Update(model, source);
|
||||
var entity = new SocketGuildUser(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();
|
||||
}
|
||||
|
||||
IVoiceChannel IVoiceState.VoiceChannel => VoiceState?.VoiceChannel;
|
||||
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 SocketGuildUser Clone() => MemberwiseClone() as SocketGuildUser;
|
||||
ISocketUser ISocketUser.Clone() => Clone();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
23
src/Discord.Net.WebSocket/Entities/Users/SocketPresence.cs
Normal file
23
src/Discord.Net.WebSocket/Entities/Users/SocketPresence.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using Model = Discord.API.Presence;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
//TODO: C#7 Candidate for record type
|
||||
internal struct SocketPresence : IPresence
|
||||
{
|
||||
public Game? Game { get; }
|
||||
public UserStatus Status { get; }
|
||||
|
||||
internal SocketPresence(Game? game, UserStatus status)
|
||||
{
|
||||
Game = game;
|
||||
Status = status;
|
||||
}
|
||||
internal SocketPresence Create(Model model)
|
||||
{
|
||||
return new SocketPresence(model.Game != null ? Discord.Game.Create(model.Game) : (Game?)null, model.Status);
|
||||
}
|
||||
|
||||
public SocketPresence Clone() => this;
|
||||
}
|
||||
}
|
||||
@@ -6,42 +6,39 @@ using Model = Discord.API.User;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
internal class SocketSelfUser : SelfUser, ISocketUser, ISelfUser
|
||||
public class SocketSelfUser : SocketUser, ISelfUser
|
||||
{
|
||||
internal override bool IsAttached => true;
|
||||
public string Email { get; private set; }
|
||||
public bool IsVerified { get; private set; }
|
||||
public bool IsMfaEnabled { get; private set; }
|
||||
|
||||
public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
|
||||
SocketGlobalUser ISocketUser.User { get { throw new NotSupportedException(); } }
|
||||
|
||||
public SocketSelfUser(DiscordSocketClient discord, Model model)
|
||||
: base(discord, model)
|
||||
internal SocketSelfUser(DiscordSocketClient discord, ulong id)
|
||||
: base(discord, id)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task ModifyStatusAsync(Action<ModifyPresenceParams> func)
|
||||
internal new static SocketSelfUser Create(DiscordSocketClient discord, Model model)
|
||||
{
|
||||
if (func == null) throw new NullReferenceException(nameof(func));
|
||||
var entity = new SocketSelfUser(discord, model.Id);
|
||||
entity.Update(model);
|
||||
return entity;
|
||||
}
|
||||
internal override void Update(Model model)
|
||||
{
|
||||
base.Update(model);
|
||||
|
||||
var args = new ModifyPresenceParams();
|
||||
func(args);
|
||||
|
||||
var game = args._game.GetValueOrDefault(_game);
|
||||
var status = args._status.GetValueOrDefault(_status);
|
||||
|
||||
long idleSince = _idleSince;
|
||||
if (status == UserStatus.Idle && _status != UserStatus.Idle)
|
||||
idleSince = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
var apiGame = game != null ? new API.Game { Name = game.Name, StreamType = game.StreamType, StreamUrl = game.StreamUrl } : null;
|
||||
|
||||
await Discord.ApiClient.SendStatusUpdateAsync(status == UserStatus.Idle ? _idleSince : (long?)null, apiGame).ConfigureAwait(false);
|
||||
|
||||
//Save values
|
||||
_idleSince = idleSince;
|
||||
_game = game;
|
||||
_status = status;
|
||||
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 SocketSelfUser Clone() => MemberwiseClone() as SocketSelfUser;
|
||||
ISocketUser ISocketUser.Clone() => Clone();
|
||||
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(); }
|
||||
}
|
||||
}
|
||||
|
||||
50
src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs
Normal file
50
src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using Discord.Rest;
|
||||
using System.Threading.Tasks;
|
||||
using Model = Discord.API.User;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
public 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 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 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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ using Model = Discord.API.VoiceState;
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
//TODO: C#7 Candidate for record type
|
||||
internal struct VoiceState : IVoiceState
|
||||
public struct SocketVoiceState : IVoiceState
|
||||
{
|
||||
[Flags]
|
||||
private enum Flags : byte
|
||||
@@ -28,9 +28,7 @@ namespace Discord.WebSocket
|
||||
public bool IsSelfMuted => (_voiceStates & Flags.SelfMuted) != 0;
|
||||
public bool IsSelfDeafened => (_voiceStates & Flags.SelfDeafened) != 0;
|
||||
|
||||
public VoiceState(SocketVoiceChannel voiceChannel, Model model)
|
||||
: this(voiceChannel, model.SessionId, model.SelfMute, model.SelfDeaf, model.Suppress) { }
|
||||
public VoiceState(SocketVoiceChannel voiceChannel, string sessionId, bool isSelfMuted, bool isSelfDeafened, bool isSuppressed)
|
||||
internal SocketVoiceState(SocketVoiceChannel voiceChannel, string sessionId, bool isSelfMuted, bool isSelfDeafened, bool isSuppressed)
|
||||
{
|
||||
VoiceChannel = voiceChannel;
|
||||
VoiceSessionId = sessionId;
|
||||
@@ -44,8 +42,12 @@ namespace Discord.WebSocket
|
||||
voiceStates |= Flags.Suppressed;
|
||||
_voiceStates = voiceStates;
|
||||
}
|
||||
internal static SocketVoiceState Create(SocketVoiceChannel voiceChannel, Model model)
|
||||
{
|
||||
return new SocketVoiceState(voiceChannel, model.SessionId, model.SelfMute, model.SelfDeaf, model.Suppress);
|
||||
}
|
||||
|
||||
public VoiceState Clone() => this;
|
||||
public SocketVoiceState Clone() => this;
|
||||
|
||||
IVoiceChannel IVoiceState.VoiceChannel => VoiceChannel;
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
using Discord.API.Rest;
|
||||
using Discord.Rest;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Model = Discord.API.Message;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
internal class MessageManager
|
||||
{
|
||||
private readonly DiscordSocketClient _discord;
|
||||
private readonly ISocketMessageChannel _channel;
|
||||
|
||||
public virtual IReadOnlyCollection<ISocketMessage> Messages
|
||||
=> ImmutableArray.Create<ISocketMessage>();
|
||||
|
||||
public MessageManager(DiscordSocketClient discord, ISocketMessageChannel channel)
|
||||
{
|
||||
_discord = discord;
|
||||
_channel = channel;
|
||||
}
|
||||
|
||||
public virtual void Add(ISocketMessage message) { }
|
||||
public virtual ISocketMessage Remove(ulong id) => null;
|
||||
public virtual ISocketMessage Get(ulong id) => null;
|
||||
|
||||
public virtual IImmutableList<ISocketMessage> GetMany(ulong? fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
|
||||
=> ImmutableArray.Create<ISocketMessage>();
|
||||
|
||||
public virtual async Task<ISocketMessage> DownloadAsync(ulong id)
|
||||
{
|
||||
var model = await _discord.ApiClient.GetChannelMessageAsync(_channel.Id, id).ConfigureAwait(false);
|
||||
if (model != null)
|
||||
return Create(new User(model.Author.Value), model);
|
||||
return null;
|
||||
}
|
||||
public async Task<IReadOnlyCollection<ISocketMessage>> DownloadAsync(ulong? fromId, Direction dir, int limit)
|
||||
{
|
||||
//TODO: Test heavily, especially the ordering of messages
|
||||
if (limit < 0) throw new ArgumentOutOfRangeException(nameof(limit));
|
||||
if (limit == 0) return ImmutableArray<ISocketMessage>.Empty;
|
||||
|
||||
var cachedMessages = GetMany(fromId, dir, limit);
|
||||
if (cachedMessages.Count == limit)
|
||||
return cachedMessages;
|
||||
else if (cachedMessages.Count > limit)
|
||||
return cachedMessages.Skip(cachedMessages.Count - limit).ToImmutableArray();
|
||||
else
|
||||
{
|
||||
var args = new GetChannelMessagesParams
|
||||
{
|
||||
Limit = limit - cachedMessages.Count,
|
||||
RelativeDirection = dir
|
||||
};
|
||||
if (cachedMessages.Count == 0)
|
||||
{
|
||||
if (fromId != null)
|
||||
args.RelativeMessageId = fromId.Value;
|
||||
}
|
||||
else
|
||||
args.RelativeMessageId = dir == Direction.Before ? cachedMessages[0].Id : cachedMessages[cachedMessages.Count - 1].Id;
|
||||
var downloadedMessages = await _discord.ApiClient.GetChannelMessagesAsync(_channel.Id, args).ConfigureAwait(false);
|
||||
|
||||
var guild = (_channel as ISocketGuildChannel)?.Guild;
|
||||
return cachedMessages.Concat(downloadedMessages.Select(x =>
|
||||
{
|
||||
IUser user = _channel.GetUser(x.Author.Value.Id, true);
|
||||
if (user == null)
|
||||
{
|
||||
var newUser = new User(x.Author.Value);
|
||||
if (guild != null)
|
||||
user = new GuildUser(guild, newUser);
|
||||
else
|
||||
user = newUser;
|
||||
}
|
||||
return Create(user, x);
|
||||
})).ToImmutableArray();
|
||||
}
|
||||
}
|
||||
|
||||
public ISocketMessage Create(IUser author, Model model)
|
||||
{
|
||||
if (model.Type == MessageType.Default)
|
||||
return new SocketUserMessage(_channel, author, model);
|
||||
else
|
||||
return new SocketSystemMessage(_channel, author, model);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user