Started converting websocket and rpc classes

This commit is contained in:
RogueException
2016-09-28 04:12:17 -03:00
parent 0c4641ac68
commit dd86f03306
107 changed files with 1836 additions and 1882 deletions

View 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();
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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?
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}