Browse Source

Code Cleanup

pull/1127/head
Sleepy Boyy 7 years ago
parent
commit
7b87911351
47 changed files with 163 additions and 236 deletions
  1. +3
    -1
      src/Discord.Net.Commands/Attributes/Preconditions/RequireBotPermissionAttribute.cs
  2. +3
    -1
      src/Discord.Net.Commands/Attributes/Preconditions/RequireContextAttribute.cs
  3. +3
    -1
      src/Discord.Net.Commands/Attributes/Preconditions/RequireUserPermissionAttribute.cs
  4. +3
    -4
      src/Discord.Net.Commands/Extensions/IEnumerableExtensions.cs
  5. +0
    -3
      src/Discord.Net.Commands/Extensions/MessageExtensions.cs
  6. +9
    -7
      src/Discord.Net.Commands/Info/CommandInfo.cs
  7. +2
    -4
      src/Discord.Net.Commands/Info/ModuleInfo.cs
  8. +3
    -1
      src/Discord.Net.Commands/Map/CommandMapNode.cs
  9. +3
    -1
      src/Discord.Net.Commands/Readers/RoleTypeReader.cs
  10. +0
    -4
      src/Discord.Net.Commands/Results/ParseResult.cs
  11. +3
    -1
      src/Discord.Net.Commands/Utilities/ReflectionUtils.cs
  12. +0
    -1
      src/Discord.Net.Core/Entities/Emotes/Emote.cs
  13. +2
    -1
      src/Discord.Net.Core/Entities/Users/GuildUserProperties.cs
  14. +2
    -4
      src/Discord.Net.Core/Format.cs
  15. +6
    -6
      src/Discord.Net.Core/Net/HttpException.cs
  16. +3
    -4
      src/Discord.Net.Core/Utils/MentionUtils.cs
  17. +2
    -1
      src/Discord.Net.Core/Utils/Permissions.cs
  18. +1
    -1
      src/Discord.Net.Providers.WS4Net/WS4NetClient.cs
  19. +2
    -4
      src/Discord.Net.Rest/Entities/AuditLogs/AuditLogHelper.cs
  20. +9
    -12
      src/Discord.Net.Rest/Entities/Channels/RestDMChannel.cs
  21. +10
    -16
      src/Discord.Net.Rest/Entities/Channels/RestGroupChannel.cs
  22. +16
    -22
      src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs
  23. +9
    -12
      src/Discord.Net.Rest/Entities/Channels/RpcVirtualMessageChannel.cs
  24. +1
    -5
      src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
  25. +3
    -1
      src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs
  26. +3
    -1
      src/Discord.Net.Rest/Entities/Users/RestUser.cs
  27. +3
    -1
      src/Discord.Net.Rest/Net/Converters/DiscordContractResolver.cs
  28. +1
    -1
      src/Discord.Net.Rest/Net/Queue/RequestQueue.cs
  29. +2
    -4
      src/Discord.Net.WebSocket/Audio/AudioClient.cs
  30. +3
    -1
      src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs
  31. +1
    -1
      src/Discord.Net.WebSocket/Audio/Streams/InputStream.cs
  32. +1
    -0
      src/Discord.Net.WebSocket/Audio/Streams/JitterBuffer.cs
  33. +1
    -1
      src/Discord.Net.WebSocket/Audio/Streams/RTPWriteStream.cs
  34. +7
    -25
      src/Discord.Net.WebSocket/ClientState.cs
  35. +13
    -34
      src/Discord.Net.WebSocket/DiscordShardedClient.cs
  36. +1
    -1
      src/Discord.Net.WebSocket/DiscordSocketApiClient.cs
  37. +8
    -7
      src/Discord.Net.WebSocket/DiscordSocketClient.cs
  38. +1
    -1
      src/Discord.Net.WebSocket/DiscordVoiceApiClient.cs
  39. +6
    -5
      src/Discord.Net.WebSocket/Entities/Channels/SocketChannelHelper.cs
  40. +2
    -6
      src/Discord.Net.WebSocket/Entities/Channels/SocketGroupChannel.cs
  41. +3
    -13
      src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
  42. +2
    -8
      src/Discord.Net.WebSocket/Entities/Messages/MessageCache.cs
  43. +1
    -3
      src/Discord.Net.WebSocket/Entities/Messages/SocketUserMessage.cs
  44. +1
    -1
      src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs
  45. +2
    -2
      test/Discord.Net.Tests/Net/CachedRestClient.cs
  46. +2
    -1
      test/Discord.Net.Tests/Tests.GuildPermissions.cs
  47. +1
    -1
      test/Discord.Net.Tests/Tests.cs

+ 3
- 1
src/Discord.Net.Commands/Attributes/Preconditions/RequireBotPermissionAttribute.cs View File

@@ -70,7 +70,9 @@ namespace Discord.Commands
else else
perms = ChannelPermissions.All(context.Channel); perms = ChannelPermissions.All(context.Channel);


return !perms.Has(ChannelPermission.Value) ? PreconditionResult.FromError($"Bot requires channel permission {ChannelPermission.Value}") : PreconditionResult.FromSuccess();
return !perms.Has(ChannelPermission.Value)
? PreconditionResult.FromError($"Bot requires channel permission {ChannelPermission.Value}")
: PreconditionResult.FromSuccess();
} }
} }
} }

+ 3
- 1
src/Discord.Net.Commands/Attributes/Preconditions/RequireContextAttribute.cs View File

@@ -52,7 +52,9 @@ namespace Discord.Commands
if ((Contexts & ContextType.Group) != 0) if ((Contexts & ContextType.Group) != 0)
isValid = isValid || context.Channel is IGroupChannel; isValid = isValid || context.Channel is IGroupChannel;


return Task.FromResult(isValid ? PreconditionResult.FromSuccess() : PreconditionResult.FromError($"Invalid context for command; accepted contexts: {Contexts}"));
return Task.FromResult(isValid
? PreconditionResult.FromSuccess()
: PreconditionResult.FromError($"Invalid context for command; accepted contexts: {Contexts}"));
} }
} }
} }

+ 3
- 1
src/Discord.Net.Commands/Attributes/Preconditions/RequireUserPermissionAttribute.cs View File

@@ -70,7 +70,9 @@ namespace Discord.Commands
else else
perms = ChannelPermissions.All(context.Channel); perms = ChannelPermissions.All(context.Channel);


return Task.FromResult(!perms.Has(ChannelPermission.Value) ? PreconditionResult.FromError($"User requires channel permission {ChannelPermission.Value}") : PreconditionResult.FromSuccess());
return Task.FromResult(!perms.Has(ChannelPermission.Value)
? PreconditionResult.FromError($"User requires channel permission {ChannelPermission.Value}")
: PreconditionResult.FromSuccess());
} }
} }
} }

+ 3
- 4
src/Discord.Net.Commands/Extensions/IEnumerableExtensions.cs View File

@@ -9,9 +9,8 @@ namespace Discord.Commands
public static IEnumerable<TResult> Permutate<TFirst, TSecond, TResult>( public static IEnumerable<TResult> Permutate<TFirst, TSecond, TResult>(
this IEnumerable<TFirst> set, this IEnumerable<TFirst> set,
IEnumerable<TSecond> others, IEnumerable<TSecond> others,
Func<TFirst, TSecond, TResult> func)
{
return from elem in set from elem2 in others select func(elem, elem2);
}
Func<TFirst, TSecond, TResult> func) => from elem in set
from elem2 in others
select func(elem, elem2);
} }
} }

+ 0
- 3
src/Discord.Net.Commands/Extensions/MessageExtensions.cs View File

@@ -10,7 +10,6 @@ namespace Discord.Commands
if (text.Length <= 0 || text[0] != c) return false; if (text.Length <= 0 || text[0] != c) return false;
argPos = 1; argPos = 1;
return true; return true;

} }


public static bool HasStringPrefix(this IUserMessage msg, string str, ref int argPos, public static bool HasStringPrefix(this IUserMessage msg, string str, ref int argPos,
@@ -20,7 +19,6 @@ namespace Discord.Commands
if (!text.StartsWith(str, comparisonType)) return false; if (!text.StartsWith(str, comparisonType)) return false;
argPos = str.Length; argPos = str.Length;
return true; return true;

} }


public static bool HasMentionPrefix(this IUserMessage msg, IUser user, ref int argPos) public static bool HasMentionPrefix(this IUserMessage msg, IUser user, ref int argPos)
@@ -37,7 +35,6 @@ namespace Discord.Commands
if (userId != user.Id) return false; if (userId != user.Id) return false;
argPos = endPos + 2; argPos = endPos + 2;
return true; return true;

} }
} }
} }

+ 9
- 7
src/Discord.Net.Commands/Info/CommandInfo.cs View File

@@ -202,7 +202,8 @@ namespace Discord.Commands
case Task<IResult> resultTask: case Task<IResult> resultTask:
{ {
var result = await resultTask.ConfigureAwait(false); var result = await resultTask.ConfigureAwait(false);
await Module.Service._commandExecutedEvent.InvokeAsync(this, context, result).ConfigureAwait(false);
await Module.Service._commandExecutedEvent.InvokeAsync(this, context, result)
.ConfigureAwait(false);
if (result is RuntimeResult execResult) if (result is RuntimeResult execResult)
return execResult; return execResult;
break; break;
@@ -210,14 +211,16 @@ namespace Discord.Commands
case Task<ExecuteResult> execTask: case Task<ExecuteResult> execTask:
{ {
var result = await execTask.ConfigureAwait(false); var result = await execTask.ConfigureAwait(false);
await Module.Service._commandExecutedEvent.InvokeAsync(this, context, result).ConfigureAwait(false);
await Module.Service._commandExecutedEvent.InvokeAsync(this, context, result)
.ConfigureAwait(false);
return result; return result;
} }
default: default:
{ {
await task.ConfigureAwait(false); await task.ConfigureAwait(false);
var result = ExecuteResult.FromSuccess(); var result = ExecuteResult.FromSuccess();
await Module.Service._commandExecutedEvent.InvokeAsync(this, context, result).ConfigureAwait(false);
await Module.Service._commandExecutedEvent.InvokeAsync(this, context, result)
.ConfigureAwait(false);
break; break;
} }
} }
@@ -280,9 +283,8 @@ namespace Discord.Commands
private static T[] ConvertParamsList<T>(IEnumerable<object> paramsList) private static T[] ConvertParamsList<T>(IEnumerable<object> paramsList)
=> paramsList.Cast<T>().ToArray(); => paramsList.Cast<T>().ToArray();


internal string GetLogText(ICommandContext context)
{
return context.Guild != null ? $"\"{Name}\" for {context.User} in {context.Guild}/{context.Channel}" : $"\"{Name}\" for {context.User} in {context.Channel}";
}
internal string GetLogText(ICommandContext context) => context.Guild != null
? $"\"{Name}\" for {context.User} in {context.Guild}/{context.Channel}"
: $"\"{Name}\" for {context.User} in {context.Channel}";
} }
} }

+ 2
- 4
src/Discord.Net.Commands/Info/ModuleInfo.cs View File

@@ -68,10 +68,8 @@ namespace Discord.Commands
} }


private IEnumerable<ModuleInfo> BuildSubmodules(ModuleBuilder parent, CommandService service, private IEnumerable<ModuleInfo> BuildSubmodules(ModuleBuilder parent, CommandService service,
IServiceProvider services)
{
return parent.Modules.Select(submodule => submodule.Build(service, services, this)).ToList();
}
IServiceProvider services) =>
parent.Modules.Select(submodule => submodule.Build(service, services, this)).ToList();


private static IEnumerable<PreconditionAttribute> BuildPreconditions(ModuleBuilder builder) private static IEnumerable<PreconditionAttribute> BuildPreconditions(ModuleBuilder builder)
{ {


+ 3
- 1
src/Discord.Net.Commands/Map/CommandMapNode.cs View File

@@ -36,7 +36,9 @@ namespace Discord.Commands
_commands = _commands.Add(command); _commands = _commands.Add(command);
break; break;
default: default:
var name = nextSegment == -1 ? text.Substring(index) : text.Substring(index, nextSegment - index);
var name = nextSegment == -1
? text.Substring(index)
: text.Substring(index, nextSegment - index);


var fullName = _name == "" ? name : _name + service._separatorChar + name; var fullName = _name == "" ? name : _name + service._separatorChar + name;
var nextNode = _nodes.GetOrAdd(name, x => new CommandMapNode(fullName)); var nextNode = _nodes.GetOrAdd(name, x => new CommandMapNode(fullName));


+ 3
- 1
src/Discord.Net.Commands/Readers/RoleTypeReader.cs View File

@@ -29,7 +29,9 @@ namespace Discord.Commands
foreach (var role in roles.Where(x => string.Equals(input, x.Name, StringComparison.OrdinalIgnoreCase))) foreach (var role in roles.Where(x => string.Equals(input, x.Name, StringComparison.OrdinalIgnoreCase)))
AddResult(results, role as T, role.Name == input ? 0.80f : 0.70f); AddResult(results, role as T, role.Name == input ? 0.80f : 0.70f);


return Task.FromResult(results.Count > 0 ? TypeReaderResult.FromSuccess(results.Values.ToReadOnlyCollection()) : TypeReaderResult.FromError(CommandError.ObjectNotFound, "Role not found."));
return Task.FromResult(results.Count > 0
? TypeReaderResult.FromSuccess(results.Values.ToReadOnlyCollection())
: TypeReaderResult.FromError(CommandError.ObjectNotFound, "Role not found."));
} }


private void AddResult(Dictionary<ulong, TypeReaderValue> results, T role, float score) private void AddResult(Dictionary<ulong, TypeReaderValue> results, T role, float score)


+ 0
- 4
src/Discord.Net.Commands/Results/ParseResult.cs View File

@@ -29,15 +29,11 @@ namespace Discord.Commands
IReadOnlyList<TypeReaderResult> paramValues) IReadOnlyList<TypeReaderResult> paramValues)
{ {
if (argValues.Any(t => t.Values.Count > 1)) if (argValues.Any(t => t.Values.Count > 1))
{
return new ParseResult(argValues, paramValues, CommandError.MultipleMatches, return new ParseResult(argValues, paramValues, CommandError.MultipleMatches,
"Multiple matches found."); "Multiple matches found.");
}
if (paramValues.Any(t => t.Values.Count > 1)) if (paramValues.Any(t => t.Values.Count > 1))
{
return new ParseResult(argValues, paramValues, CommandError.MultipleMatches, return new ParseResult(argValues, paramValues, CommandError.MultipleMatches,
"Multiple matches found."); "Multiple matches found.");
}


return new ParseResult(argValues, paramValues, null, null); return new ParseResult(argValues, paramValues, null, null);
} }


+ 3
- 1
src/Discord.Net.Commands/Utilities/ReflectionUtils.cs View File

@@ -58,7 +58,9 @@ namespace Discord.Commands
var result = new List<PropertyInfo>(); var result = new List<PropertyInfo>();
while (ownerType != _objectTypeInfo) while (ownerType != _objectTypeInfo)
{ {
result.AddRange(ownerType.DeclaredProperties.Where(prop => prop.SetMethod?.IsStatic == false && prop.SetMethod?.IsPublic == true && prop.GetCustomAttribute<DontInjectAttribute>() == null));
result.AddRange(ownerType.DeclaredProperties.Where(prop =>
prop.SetMethod?.IsStatic == false && prop.SetMethod?.IsPublic == true &&
prop.GetCustomAttribute<DontInjectAttribute>() == null));
ownerType = ownerType.BaseType.GetTypeInfo(); ownerType = ownerType.BaseType.GetTypeInfo();
} }




+ 0
- 1
src/Discord.Net.Core/Entities/Emotes/Emote.cs View File

@@ -86,7 +86,6 @@ namespace Discord
var name = text.Substring(startIndex, splitIndex - startIndex); var name = text.Substring(startIndex, splitIndex - startIndex);
result = new Emote(id, name, animated); result = new Emote(id, name, animated);
return true; return true;

} }


public override string ToString() => $"<{(Animated ? "a" : "")}:{Name}:{Id}>"; public override string ToString() => $"<{(Animated ? "a" : "")}:{Name}:{Id}>";


+ 2
- 1
src/Discord.Net.Core/Entities/Users/GuildUserProperties.cs View File

@@ -46,7 +46,8 @@ namespace Discord
/// <remarks> /// <remarks>
/// To add a role to a user: /// To add a role to a user:
/// <see cref="IGuildUser.AddRolesAsync(System.Collections.Generic.IEnumerable{Discord.IRole}, RequestOptions)" /> /// <see cref="IGuildUser.AddRolesAsync(System.Collections.Generic.IEnumerable{Discord.IRole}, RequestOptions)" />
/// To remove a role from a user: <see cref="IGuildUser.RemoveRolesAsync(System.Collections.Generic.IEnumerable{Discord.IRole}, RequestOptions)" />
/// To remove a role from a user:
/// <see cref="IGuildUser.RemoveRolesAsync(System.Collections.Generic.IEnumerable{Discord.IRole}, RequestOptions)" />
/// </remarks> /// </remarks>
public Optional<IEnumerable<IRole>> Roles { get; set; } public Optional<IEnumerable<IRole>> Roles { get; set; }




+ 2
- 4
src/Discord.Net.Core/Format.cs View File

@@ -28,9 +28,7 @@ namespace Discord
} }


/// <summary> Sanitizes the string, safely escaping any Markdown sequences. </summary> /// <summary> Sanitizes the string, safely escaping any Markdown sequences. </summary>
public static string Sanitize(string text)
{
return SensitiveCharacters.Aggregate(text, (current, unsafeChar) => current.Replace(unsafeChar, $"\\{unsafeChar}"));
}
public static string Sanitize(string text) => SensitiveCharacters.Aggregate(text,
(current, unsafeChar) => current.Replace(unsafeChar, $"\\{unsafeChar}"));
} }
} }

+ 6
- 6
src/Discord.Net.Core/Net/HttpException.cs View File

@@ -23,13 +23,13 @@ namespace Discord.Net
{ {
string msg; string msg;
if (discordCode != null && discordCode != 0) if (discordCode != null && discordCode != 0)
{
msg = reason != null ? $"The server responded with error {(int)discordCode}: {reason}" : $"The server responded with error {(int)discordCode}: {httpCode}";
}
msg = reason != null
? $"The server responded with error {(int)discordCode}: {reason}"
: $"The server responded with error {(int)discordCode}: {httpCode}";
else else
{
msg = reason != null ? $"The server responded with error {(int)httpCode}: {reason}" : $"The server responded with error {(int)httpCode}: {httpCode}";
}
msg = reason != null
? $"The server responded with error {(int)httpCode}: {reason}"
: $"The server responded with error {(int)httpCode}: {httpCode}";


return msg; return msg;
} }


+ 3
- 4
src/Discord.Net.Core/Utils/MentionUtils.cs View File

@@ -214,6 +214,7 @@ namespace Discord
case TagHandling.Sanitize: case TagHandling.Sanitize:
return $"@{SanitizeChar}everyone"; return $"@{SanitizeChar}everyone";
} }

return ""; return "";
} }


@@ -230,6 +231,7 @@ namespace Discord
case TagHandling.Sanitize: case TagHandling.Sanitize:
return $"@{SanitizeChar}here"; return $"@{SanitizeChar}here";
} }

return ""; return "";
} }


@@ -239,10 +241,7 @@ namespace Discord
var emoji = (Emote)tag.Value; var emoji = (Emote)tag.Value;


//Remove if its name contains any bad chars (prevents a few tag exploits) //Remove if its name contains any bad chars (prevents a few tag exploits)
if (emoji.Name.Any(c => !char.IsLetterOrDigit(c) && c != '_' && c != '-'))
{
return "";
}
if (emoji.Name.Any(c => !char.IsLetterOrDigit(c) && c != '_' && c != '-')) return "";


switch (mode) switch (mode)
{ {


+ 2
- 1
src/Discord.Net.Core/Utils/Permissions.cs View File

@@ -111,7 +111,8 @@ namespace Discord
resolvedPermissions = GuildPermissions.Webhook.RawValue; resolvedPermissions = GuildPermissions.Webhook.RawValue;
else else
{ {
resolvedPermissions = user.RoleIds.Aggregate(resolvedPermissions, (current, roleId) => current | (guild.GetRole(roleId)?.Permissions.RawValue ?? 0));
resolvedPermissions = user.RoleIds.Aggregate(resolvedPermissions,
(current, roleId) => current | (guild.GetRole(roleId)?.Permissions.RawValue ?? 0));
if (GetValue(resolvedPermissions, GuildPermission.Administrator)) if (GetValue(resolvedPermissions, GuildPermission.Administrator))
resolvedPermissions = GuildPermissions.All.RawValue; //Administrators always have all permissions resolvedPermissions = GuildPermissions.All.RawValue; //Administrators always have all permissions
} }


+ 1
- 1
src/Discord.Net.Providers.WS4Net/WS4NetClient.cs View File

@@ -16,11 +16,11 @@ namespace Discord.Net.Providers.WS4Net
private readonly Dictionary<string, string> _headers; private readonly Dictionary<string, string> _headers;


private readonly SemaphoreSlim _lock; private readonly SemaphoreSlim _lock;
private readonly ManualResetEventSlim _waitUntilConnect;
private CancellationToken _cancelToken, _parentToken; private CancellationToken _cancelToken, _parentToken;
private CancellationTokenSource _cancelTokenSource; private CancellationTokenSource _cancelTokenSource;
private WS4NetSocket _client; private WS4NetSocket _client;
private bool _isDisposed; private bool _isDisposed;
private readonly ManualResetEventSlim _waitUntilConnect;


public WS4NetClient() public WS4NetClient()
{ {


+ 2
- 4
src/Discord.Net.Rest/Entities/AuditLogs/AuditLogHelper.cs View File

@@ -47,9 +47,7 @@ namespace Discord.Rest
[ActionType.MessageDeleted] = MessageDeleteAuditLogData.Create [ActionType.MessageDeleted] = MessageDeleteAuditLogData.Create
}; };


public static IAuditLogData CreateData(BaseDiscordClient discord, Model log, EntryModel entry)
{
return CreateMapping.TryGetValue(entry.Action, out var func) ? func(discord, log, entry) : null;
}
public static IAuditLogData CreateData(BaseDiscordClient discord, Model log, EntryModel entry) =>
CreateMapping.TryGetValue(entry.Action, out var func) ? func(discord, log, entry) : null;
} }
} }

+ 9
- 12
src/Discord.Net.Rest/Entities/Channels/RestDMChannel.cs View File

@@ -52,22 +52,19 @@ namespace Discord.Rest
} }


IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode,
RequestOptions options)
{
return mode == CacheMode.AllowDownload ? GetMessagesAsync(limit, options) : AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
}
RequestOptions options) => mode == CacheMode.AllowDownload
? GetMessagesAsync(limit, options)
: AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();


IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId,
Direction dir, int limit, CacheMode mode, RequestOptions options)
{
return mode == CacheMode.AllowDownload ? GetMessagesAsync(fromMessageId, dir, limit, options) : AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
}
Direction dir, int limit, CacheMode mode, RequestOptions options) => mode == CacheMode.AllowDownload
? GetMessagesAsync(fromMessageId, dir, limit, options)
: AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();


IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage,
Direction dir, int limit, CacheMode mode, RequestOptions options)
{
return mode == CacheMode.AllowDownload ? GetMessagesAsync(fromMessage, dir, limit, options) : AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
}
Direction dir, int limit, CacheMode mode, RequestOptions options) => mode == CacheMode.AllowDownload
? GetMessagesAsync(fromMessage, dir, limit, options)
: AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();


async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options) async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
=> await GetPinnedMessagesAsync(options).ConfigureAwait(false); => await GetPinnedMessagesAsync(options).ConfigureAwait(false);


+ 10
- 16
src/Discord.Net.Rest/Entities/Channels/RestGroupChannel.cs View File

@@ -57,22 +57,19 @@ namespace Discord.Rest
} }


IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode,
RequestOptions options)
{
return mode == CacheMode.AllowDownload ? GetMessagesAsync(limit, options) : AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
}
RequestOptions options) => mode == CacheMode.AllowDownload
? GetMessagesAsync(limit, options)
: AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();


IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId,
Direction dir, int limit, CacheMode mode, RequestOptions options)
{
return mode == CacheMode.AllowDownload ? GetMessagesAsync(fromMessageId, dir, limit, options) : AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
}
Direction dir, int limit, CacheMode mode, RequestOptions options) => mode == CacheMode.AllowDownload
? GetMessagesAsync(fromMessageId, dir, limit, options)
: AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();


IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage,
Direction dir, int limit, CacheMode mode, RequestOptions options)
{
return mode == CacheMode.AllowDownload ? GetMessagesAsync(fromMessage, dir, limit, options) : AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
}
Direction dir, int limit, CacheMode mode, RequestOptions options) => mode == CacheMode.AllowDownload
? GetMessagesAsync(fromMessage, dir, limit, options)
: AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();


async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options) async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
=> await GetPinnedMessagesAsync(options).ConfigureAwait(false); => await GetPinnedMessagesAsync(options).ConfigureAwait(false);
@@ -170,10 +167,7 @@ namespace Discord.Rest
_users = users.ToImmutable(); _users = users.ToImmutable();
} }


public RestUser GetUser(ulong id)
{
return _users.TryGetValue(id, out var user) ? user : null;
}
public RestUser GetUser(ulong id) => _users.TryGetValue(id, out var user) ? user : null;


public IDisposable EnterTypingState(RequestOptions options = null) public IDisposable EnterTypingState(RequestOptions options = null)
=> ChannelHelper.EnterTypingState(this, Discord, options); => ChannelHelper.EnterTypingState(this, Discord, options);


+ 16
- 22
src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs View File

@@ -66,24 +66,19 @@ namespace Discord.Rest
} }


IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode,
RequestOptions options)
{
return mode == CacheMode.AllowDownload
? GetMessagesAsync(limit, options)
: AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
}
RequestOptions options) => mode == CacheMode.AllowDownload
? GetMessagesAsync(limit, options)
: AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();


IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId,
Direction dir, int limit, CacheMode mode, RequestOptions options)
{
return mode == CacheMode.AllowDownload ? GetMessagesAsync(fromMessageId, dir, limit, options) : AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
}
Direction dir, int limit, CacheMode mode, RequestOptions options) => mode == CacheMode.AllowDownload
? GetMessagesAsync(fromMessageId, dir, limit, options)
: AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();


IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage,
Direction dir, int limit, CacheMode mode, RequestOptions options)
{
return mode == CacheMode.AllowDownload ? GetMessagesAsync(fromMessage, dir, limit, options) : AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
}
Direction dir, int limit, CacheMode mode, RequestOptions options) => mode == CacheMode.AllowDownload
? GetMessagesAsync(fromMessage, dir, limit, options)
: AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();


async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options) async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
=> await GetPinnedMessagesAsync(options).ConfigureAwait(false); => await GetPinnedMessagesAsync(options).ConfigureAwait(false);
@@ -111,10 +106,10 @@ namespace Discord.Rest
return null; return null;
} }


IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
{
return mode == CacheMode.AllowDownload ? GetUsersAsync(options) : AsyncEnumerable.Empty<IReadOnlyCollection<IGuildUser>>();
}
IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options) =>
mode == CacheMode.AllowDownload
? GetUsersAsync(options)
: AsyncEnumerable.Empty<IReadOnlyCollection<IGuildUser>>();


public string Topic { get; private set; } public string Topic { get; private set; }
public ulong? CategoryId { get; private set; } public ulong? CategoryId { get; private set; }
@@ -153,10 +148,9 @@ namespace Discord.Rest
} }


IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode,
RequestOptions options)
{
return mode == CacheMode.AllowDownload ? GetUsersAsync(options) : AsyncEnumerable.Empty<IReadOnlyCollection<IGuildUser>>();
}
RequestOptions options) => mode == CacheMode.AllowDownload
? GetUsersAsync(options)
: AsyncEnumerable.Empty<IReadOnlyCollection<IGuildUser>>();


// INestedChannel // INestedChannel
async Task<ICategoryChannel> INestedChannel.GetCategoryAsync(CacheMode mode, RequestOptions options) async Task<ICategoryChannel> INestedChannel.GetCategoryAsync(CacheMode mode, RequestOptions options)


+ 9
- 12
src/Discord.Net.Rest/Entities/Channels/RpcVirtualMessageChannel.cs View File

@@ -38,22 +38,19 @@ namespace Discord.Rest
} }


IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode,
RequestOptions options)
{
return mode == CacheMode.AllowDownload ? GetMessagesAsync(limit, options) : AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
}
RequestOptions options) => mode == CacheMode.AllowDownload
? GetMessagesAsync(limit, options)
: AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();


IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId,
Direction dir, int limit, CacheMode mode, RequestOptions options)
{
return mode == CacheMode.AllowDownload ? GetMessagesAsync(fromMessageId, dir, limit, options) : AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
}
Direction dir, int limit, CacheMode mode, RequestOptions options) => mode == CacheMode.AllowDownload
? GetMessagesAsync(fromMessageId, dir, limit, options)
: AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();


IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage,
Direction dir, int limit, CacheMode mode, RequestOptions options)
{
return mode == CacheMode.AllowDownload ? GetMessagesAsync(fromMessage, dir, limit, options) : AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
}
Direction dir, int limit, CacheMode mode, RequestOptions options) => mode == CacheMode.AllowDownload
? GetMessagesAsync(fromMessage, dir, limit, options)
: AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();


async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options) async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
=> await GetPinnedMessagesAsync(options); => await GetPinnedMessagesAsync(options);


+ 1
- 5
src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs View File

@@ -4,7 +4,6 @@ using System.Collections.Immutable;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.API;
using Discord.Audio; using Discord.Audio;
using EmbedModel = Discord.API.GuildEmbed; using EmbedModel = Discord.API.GuildEmbed;
using Model = Discord.API.Guild; using Model = Discord.API.Guild;
@@ -473,10 +472,7 @@ namespace Discord.Rest
=> GuildHelper.GetVanityInviteAsync(this, Discord, options); => GuildHelper.GetVanityInviteAsync(this, Discord, options);


//Roles //Roles
public RestRole GetRole(ulong id)
{
return _roles.TryGetValue(id, out var value) ? value : null;
}
public RestRole GetRole(ulong id) => _roles.TryGetValue(id, out var value) ? value : null;


public async Task<RestRole> CreateRoleAsync(string name, public async Task<RestRole> CreateRoleAsync(string name,
GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?), GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?),


+ 3
- 1
src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs View File

@@ -204,7 +204,9 @@ namespace Discord.Rest
return MessageSource.System; return MessageSource.System;
if (msg.WebhookId.IsSpecified) if (msg.WebhookId.IsSpecified)
return MessageSource.Webhook; return MessageSource.Webhook;
return msg.Author.GetValueOrDefault()?.Bot.GetValueOrDefault(false) == true ? MessageSource.Bot : MessageSource.User;
return msg.Author.GetValueOrDefault()?.Bot.GetValueOrDefault(false) == true
? MessageSource.Bot
: MessageSource.User;
} }
} }
} }

+ 3
- 1
src/Discord.Net.Rest/Entities/Users/RestUser.cs View File

@@ -49,7 +49,9 @@ namespace Discord.Rest
internal static RestUser Create(BaseDiscordClient discord, IGuild guild, Model model, ulong? webhookId) internal static RestUser Create(BaseDiscordClient discord, IGuild guild, Model model, ulong? webhookId)
{ {
RestUser entity; RestUser entity;
entity = webhookId.HasValue ? new RestWebhookUser(discord, guild, model.Id, webhookId.Value) : new RestUser(discord, model.Id);
entity = webhookId.HasValue
? new RestWebhookUser(discord, guild, model.Id, webhookId.Value)
: new RestUser(discord, model.Id);
entity.Update(model); entity.Update(model);
return entity; return entity;
} }


+ 3
- 1
src/Discord.Net.Rest/Net/Converters/DiscordContractResolver.cs View File

@@ -89,7 +89,9 @@ namespace Discord.Net.Converters
var typeInfo = type.GetTypeInfo(); var typeInfo = type.GetTypeInfo();
if (typeInfo.ImplementedInterfaces.Any(x => x == typeof(IEntity<ulong>))) if (typeInfo.ImplementedInterfaces.Any(x => x == typeof(IEntity<ulong>)))
return UInt64EntityConverter.Instance; return UInt64EntityConverter.Instance;
return typeInfo.ImplementedInterfaces.Any(x => x == typeof(IEntity<string>)) ? StringEntityConverter.Instance : null;
return typeInfo.ImplementedInterfaces.Any(x => x == typeof(IEntity<string>))
? StringEntityConverter.Instance
: null;
} }


private static bool ShouldSerialize<TOwner, TValue>(object owner, Delegate getter) => private static bool ShouldSerialize<TOwner, TValue>(object owner, Delegate getter) =>


+ 1
- 1
src/Discord.Net.Rest/Net/Queue/RequestQueue.cs View File

@@ -14,8 +14,8 @@ namespace Discord.Net.Queue
internal class RequestQueue : IDisposable internal class RequestQueue : IDisposable
{ {
private readonly ConcurrentDictionary<string, RequestBucket> _buckets; private readonly ConcurrentDictionary<string, RequestBucket> _buckets;
private readonly SemaphoreSlim _tokenLock;
private readonly CancellationTokenSource _cancelToken; //Dispose token private readonly CancellationTokenSource _cancelToken; //Dispose token
private readonly SemaphoreSlim _tokenLock;


private Task _cleanupTask; private Task _cleanupTask;
private CancellationTokenSource _clearToken; private CancellationTokenSource _clearToken;


+ 2
- 4
src/Discord.Net.WebSocket/Audio/AudioClient.cs View File

@@ -198,10 +198,8 @@ namespace Discord.Audio
} }
} }


internal AudioInStream GetInputStream(ulong id)
{
return _streams.TryGetValue(id, out var streamPair) ? streamPair.Reader : null;
}
internal AudioInStream GetInputStream(ulong id) =>
_streams.TryGetValue(id, out var streamPair) ? streamPair.Reader : null;


internal async Task RemoveInputStreamAsync(ulong userId) internal async Task RemoveInputStreamAsync(ulong userId)
{ {


+ 3
- 1
src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs View File

@@ -133,7 +133,9 @@ namespace Discord.Audio.Streams


public override async Task WriteAsync(byte[] data, int offset, int count, CancellationToken cancelToken) public override async Task WriteAsync(byte[] data, int offset, int count, CancellationToken cancelToken)
{ {
cancelToken = cancelToken.CanBeCanceled ? CancellationTokenSource.CreateLinkedTokenSource(cancelToken, _cancelToken).Token : _cancelToken;
cancelToken = cancelToken.CanBeCanceled
? CancellationTokenSource.CreateLinkedTokenSource(cancelToken, _cancelToken).Token
: _cancelToken;


await _queueLock.WaitAsync(-1, cancelToken).ConfigureAwait(false); await _queueLock.WaitAsync(-1, cancelToken).ConfigureAwait(false);
if (!_bufferPool.TryDequeue(out var buffer)) if (!_bufferPool.TryDequeue(out var buffer))


+ 1
- 1
src/Discord.Net.WebSocket/Audio/Streams/InputStream.cs View File

@@ -11,12 +11,12 @@ namespace Discord.Audio.Streams
private const int MaxFrames = 100; //1-2 Seconds private const int MaxFrames = 100; //1-2 Seconds


private readonly ConcurrentQueue<RTPFrame> _frames; private readonly ConcurrentQueue<RTPFrame> _frames;
private readonly SemaphoreSlim _signal;
private bool _hasHeader; private bool _hasHeader;
private bool _isDisposed; private bool _isDisposed;
private bool _nextMissed; private bool _nextMissed;
private ushort _nextSeq; private ushort _nextSeq;
private uint _nextTimestamp; private uint _nextTimestamp;
private readonly SemaphoreSlim _signal;


public InputStream() public InputStream()
{ {


+ 1
- 0
src/Discord.Net.WebSocket/Audio/Streams/JitterBuffer.cs View File

@@ -246,3 +246,4 @@ namespace Discord.Audio.Streams
}*/ }*/






+ 1
- 1
src/Discord.Net.WebSocket/Audio/Streams/RTPWriteStream.cs View File

@@ -10,10 +10,10 @@ namespace Discord.Audio.Streams
protected readonly byte[] _buffer; protected readonly byte[] _buffer;
private readonly byte[] _header; private readonly byte[] _header;
private readonly AudioStream _next; private readonly AudioStream _next;
private readonly uint _ssrc;
private bool _hasHeader; private bool _hasHeader;
private ushort _nextSeq; private ushort _nextSeq;
private uint _nextTimestamp; private uint _nextTimestamp;
private readonly uint _ssrc;


public RTPWriteStream(AudioStream next, uint ssrc, int bufferSize = 4000) public RTPWriteStream(AudioStream next, uint ssrc, int bufferSize = 4000)
{ {


+ 7
- 25
src/Discord.Net.WebSocket/ClientState.cs View File

@@ -47,15 +47,10 @@ namespace Discord.WebSocket
_groupChannels.Select(x => GetChannel(x) as ISocketPrivateChannel)) _groupChannels.Select(x => GetChannel(x) as ISocketPrivateChannel))
.ToReadOnlyCollection(() => _dmChannels.Count + _groupChannels.Count); .ToReadOnlyCollection(() => _dmChannels.Count + _groupChannels.Count);


internal SocketChannel GetChannel(ulong id)
{
return _channels.TryGetValue(id, out var channel) ? channel : null;
}
internal SocketChannel GetChannel(ulong id) => _channels.TryGetValue(id, out var channel) ? channel : null;


internal SocketDMChannel GetDMChannel(ulong userId)
{
return _dmChannels.TryGetValue(userId, out var channel) ? channel : null;
}
internal SocketDMChannel GetDMChannel(ulong userId) =>
_dmChannels.TryGetValue(userId, out var channel) ? channel : null;


internal void AddChannel(SocketChannel channel) internal void AddChannel(SocketChannel channel)
{ {
@@ -86,32 +81,19 @@ namespace Discord.WebSocket
} }


return channel; return channel;

} }


internal SocketGuild GetGuild(ulong id)
{
return _guilds.TryGetValue(id, out var guild) ? guild : null;
}
internal SocketGuild GetGuild(ulong id) => _guilds.TryGetValue(id, out var guild) ? guild : null;


internal void AddGuild(SocketGuild guild) => _guilds[guild.Id] = guild; internal void AddGuild(SocketGuild guild) => _guilds[guild.Id] = guild;


internal SocketGuild RemoveGuild(ulong id)
{
return _guilds.TryRemove(id, out var guild) ? guild : null;
}
internal SocketGuild RemoveGuild(ulong id) => _guilds.TryRemove(id, out var guild) ? guild : null;


internal SocketGlobalUser GetUser(ulong id)
{
return _users.TryGetValue(id, out var user) ? user : null;
}
internal SocketGlobalUser GetUser(ulong id) => _users.TryGetValue(id, out var user) ? user : null;


internal SocketGlobalUser GetOrAddUser(ulong id, Func<ulong, SocketGlobalUser> userFactory) => internal SocketGlobalUser GetOrAddUser(ulong id, Func<ulong, SocketGlobalUser> userFactory) =>
_users.GetOrAdd(id, userFactory); _users.GetOrAdd(id, userFactory);


internal SocketGlobalUser RemoveUser(ulong id)
{
return _users.TryRemove(id, out var user) ? user : null;
}
internal SocketGlobalUser RemoveUser(ulong id) => _users.TryRemove(id, out var user) ? user : null;
} }
} }

+ 13
- 34
src/Discord.Net.WebSocket/DiscordShardedClient.cs View File

@@ -11,11 +11,11 @@ namespace Discord.WebSocket
{ {
public partial class DiscordShardedClient : BaseSocketClient, IDiscordClient public partial class DiscordShardedClient : BaseSocketClient, IDiscordClient
{ {
private readonly bool _automaticShards;
private readonly DiscordSocketConfig _baseConfig; private readonly DiscordSocketConfig _baseConfig;
private readonly SemaphoreSlim _connectionGroupLock; private readonly SemaphoreSlim _connectionGroupLock;
private readonly bool _automaticShards;
private int[] _shardIds;
private readonly Dictionary<int, int> _shardIdsToIndex; private readonly Dictionary<int, int> _shardIdsToIndex;
private int[] _shardIds;
private DiscordSocketClient[] _shards; private DiscordSocketClient[] _shards;
private int _totalShards; private int _totalShards;


@@ -192,10 +192,7 @@ namespace Discord.WebSocket
} }
} }


public DiscordSocketClient GetShard(int id)
{
return _shardIdsToIndex.TryGetValue(id, out id) ? _shards[id] : null;
}
public DiscordSocketClient GetShard(int id) => _shardIdsToIndex.TryGetValue(id, out id) ? _shards[id] : null;


private int GetShardIdFor(ulong guildId) private int GetShardIdFor(ulong guildId)
=> (int)((guildId >> 22) % (uint)_totalShards); => (int)((guildId >> 22) % (uint)_totalShards);
@@ -218,42 +215,24 @@ namespace Discord.WebSocket
=> GetShardFor(id).GetGuild(id); => GetShardFor(id).GetGuild(id);


/// <inheritdoc /> /// <inheritdoc />
public override SocketChannel GetChannel(ulong id)
{
return _shards.Select(t => t.GetChannel(id)).FirstOrDefault(channel => channel != null);
}
public override SocketChannel GetChannel(ulong id) =>
_shards.Select(t => t.GetChannel(id)).FirstOrDefault(channel => channel != null);


private IEnumerable<ISocketPrivateChannel> GetPrivateChannels()
{
return _shards.SelectMany(t => t.PrivateChannels);
}
private IEnumerable<ISocketPrivateChannel> GetPrivateChannels() => _shards.SelectMany(t => t.PrivateChannels);


private int GetPrivateChannelCount()
{
return _shards.Sum(t => t.PrivateChannels.Count);
}
private int GetPrivateChannelCount() => _shards.Sum(t => t.PrivateChannels.Count);


private IEnumerable<SocketGuild> GetGuilds()
{
return _shards.SelectMany(t => t.Guilds);
}
private IEnumerable<SocketGuild> GetGuilds() => _shards.SelectMany(t => t.Guilds);


private int GetGuildCount()
{
return _shards.Sum(t => t.Guilds.Count);
}
private int GetGuildCount() => _shards.Sum(t => t.Guilds.Count);


/// <inheritdoc /> /// <inheritdoc />
public override SocketUser GetUser(ulong id)
{
return _shards.Select(t => t.GetUser(id)).FirstOrDefault(user => user != null);
}
public override SocketUser GetUser(ulong id) =>
_shards.Select(t => t.GetUser(id)).FirstOrDefault(user => user != null);


/// <inheritdoc /> /// <inheritdoc />
public override SocketUser GetUser(string username, string discriminator)
{
return _shards.Select(t => t.GetUser(username, discriminator)).FirstOrDefault(user => user != null);
}
public override SocketUser GetUser(string username, string discriminator) => _shards
.Select(t => t.GetUser(username, discriminator)).FirstOrDefault(user => user != null);


/// <inheritdoc /> /// <inheritdoc />
public override RestVoiceRegion GetVoiceRegion(string id) public override RestVoiceRegion GetVoiceRegion(string id)


+ 1
- 1
src/Discord.Net.WebSocket/DiscordSocketApiClient.cs View File

@@ -18,6 +18,7 @@ namespace Discord.API
internal class DiscordSocketApiClient : DiscordRestApiClient internal class DiscordSocketApiClient : DiscordRestApiClient
{ {
private readonly AsyncEvent<Func<Exception, Task>> _disconnectedEvent = new AsyncEvent<Func<Exception, Task>>(); private readonly AsyncEvent<Func<Exception, Task>> _disconnectedEvent = new AsyncEvent<Func<Exception, Task>>();
private readonly bool _isExplicitUrl;


private readonly AsyncEvent<Func<GatewayOpCode, int?, string, object, Task>> _receivedGatewayEvent = private readonly AsyncEvent<Func<GatewayOpCode, int?, string, object, Task>> _receivedGatewayEvent =
new AsyncEvent<Func<GatewayOpCode, int?, string, object, Task>>(); new AsyncEvent<Func<GatewayOpCode, int?, string, object, Task>>();
@@ -31,7 +32,6 @@ namespace Discord.API
private CancellationTokenSource _connectCancelToken; private CancellationTokenSource _connectCancelToken;
private DeflateStream _decompressor; private DeflateStream _decompressor;
private string _gatewayUrl; private string _gatewayUrl;
private readonly bool _isExplicitUrl;


public DiscordSocketApiClient(RestClientProvider restClientProvider, WebSocketProvider webSocketProvider, public DiscordSocketApiClient(RestClientProvider restClientProvider, WebSocketProvider webSocketProvider,
string userAgent, string userAgent,


+ 8
- 7
src/Discord.Net.WebSocket/DiscordSocketClient.cs View File

@@ -357,10 +357,8 @@ namespace Discord.WebSocket
=> State.RemoveUser(id); => State.RemoveUser(id);


/// <inheritdoc /> /// <inheritdoc />
public override RestVoiceRegion GetVoiceRegion(string id)
{
return _voiceRegions.TryGetValue(id, out var region) ? region : null;
}
public override RestVoiceRegion GetVoiceRegion(string id) =>
_voiceRegions.TryGetValue(id, out var region) ? region : null;


/// <summary> Downloads the users list for the provided guilds, if they don't have a complete list. </summary> /// <summary> Downloads the users list for the provided guilds, if they don't have a complete list. </summary>
public override async Task DownloadUsersAsync(IEnumerable<IGuild> guilds) public override async Task DownloadUsersAsync(IEnumerable<IGuild> guilds)
@@ -1152,7 +1150,7 @@ namespace Discord.WebSocket
author = guild.GetUser(data.Author.Value.Id); author = guild.GetUser(data.Author.Value.Id);
} }
else else
author = ((SocketChannel) channel).GetUser(data.Author.Value.Id);
author = ((SocketChannel)channel).GetUser(data.Author.Value.Id);


if (author == null) if (author == null)
{ {
@@ -1205,8 +1203,11 @@ namespace Discord.WebSocket
else if (data.Author.IsSpecified) else if (data.Author.IsSpecified)
{ {
//Edited message isnt in cache, create a detached one //Edited message isnt in cache, create a detached one
var author = (guild != null ? guild.GetUser(data.Author.Value.Id) : ((SocketChannel) channel).GetUser(data.Author.Value.Id)) ??
SocketUnknownUser.Create(this, State, data.Author.Value);
var author =
(guild != null
? guild.GetUser(data.Author.Value.Id)
: ((SocketChannel)channel).GetUser(data.Author.Value.Id)) ??
SocketUnknownUser.Create(this, State, data.Author.Value);


after = SocketMessage.Create(this, State, author, channel, data); after = SocketMessage.Create(this, State, author, channel, data);
} }


+ 1
- 1
src/Discord.Net.WebSocket/DiscordVoiceApiClient.cs View File

@@ -37,10 +37,10 @@ namespace Discord.Audio
new AsyncEvent<Func<string, string, double, Task>>(); new AsyncEvent<Func<string, string, double, Task>>();


private readonly JsonSerializer _serializer; private readonly JsonSerializer _serializer;
private readonly IUdpSocket _udp;
private CancellationTokenSource _connectCancelToken; private CancellationTokenSource _connectCancelToken;
private bool _isDisposed; private bool _isDisposed;
private ulong _nextKeepalive; private ulong _nextKeepalive;
private readonly IUdpSocket _udp;


internal DiscordVoiceAPIClient(ulong guildId, WebSocketProvider webSocketProvider, internal DiscordVoiceAPIClient(ulong guildId, WebSocketProvider webSocketProvider,
UdpSocketProvider udpSocketProvider, JsonSerializer serializer = null) UdpSocketProvider udpSocketProvider, JsonSerializer serializer = null)


+ 6
- 5
src/Discord.Net.WebSocket/Entities/Channels/SocketChannelHelper.cs View File

@@ -23,7 +23,9 @@ namespace Discord.WebSocket


if (dir == Direction.Before || mode == CacheMode.CacheOnly) if (dir == Direction.Before || mode == CacheMode.CacheOnly)
{ {
cachedMessages = messages != null ? messages.GetMany(fromMessageId, dir, limit) : ImmutableArray.Create<SocketMessage>();
cachedMessages = messages != null
? messages.GetMany(fromMessageId, dir, limit)
: ImmutableArray.Create<SocketMessage>();
result = ImmutableArray.Create(cachedMessages).ToAsyncEnumerable<IReadOnlyCollection<IMessage>>(); result = ImmutableArray.Create(cachedMessages).ToAsyncEnumerable<IReadOnlyCollection<IMessage>>();
} }


@@ -45,10 +47,9 @@ namespace Discord.WebSocket


public static IReadOnlyCollection<SocketMessage> GetCachedMessages(SocketChannel channel, public static IReadOnlyCollection<SocketMessage> GetCachedMessages(SocketChannel channel,
DiscordSocketClient discord, MessageCache messages, DiscordSocketClient discord, MessageCache messages,
ulong? fromMessageId, Direction dir, int limit)
{
return messages != null ? messages.GetMany(fromMessageId, dir, limit) : ImmutableArray.Create<SocketMessage>();
}
ulong? fromMessageId, Direction dir, int limit) => messages != null
? messages.GetMany(fromMessageId, dir, limit)
: ImmutableArray.Create<SocketMessage>();


public static void AddMessage(ISocketMessageChannel channel, DiscordSocketClient discord, public static void AddMessage(ISocketMessageChannel channel, DiscordSocketClient discord,
SocketMessage msg) SocketMessage msg)


+ 2
- 6
src/Discord.Net.WebSocket/Entities/Channels/SocketGroupChannel.cs View File

@@ -19,10 +19,10 @@ namespace Discord.WebSocket
ISocketAudioChannel ISocketAudioChannel
{ {
private readonly MessageCache _messages; private readonly MessageCache _messages;
private readonly ConcurrentDictionary<ulong, SocketVoiceState> _voiceStates;


private string _iconId; private string _iconId;
private ConcurrentDictionary<ulong, SocketGroupUser> _users; private ConcurrentDictionary<ulong, SocketGroupUser> _users;
private readonly ConcurrentDictionary<ulong, SocketVoiceState> _voiceStates;


internal SocketGroupChannel(DiscordSocketClient discord, ulong id) internal SocketGroupChannel(DiscordSocketClient discord, ulong id)
: base(discord, id) : base(discord, id)
@@ -211,10 +211,7 @@ namespace Discord.WebSocket
=> _messages?.Remove(id); => _messages?.Remove(id);


//Users //Users
public new SocketGroupUser GetUser(ulong id)
{
return _users.TryGetValue(id, out var user) ? user : null;
}
public new SocketGroupUser GetUser(ulong id) => _users.TryGetValue(id, out var user) ? user : null;


internal SocketGroupUser GetOrAddUser(UserModel model) internal SocketGroupUser GetOrAddUser(UserModel model)
{ {
@@ -231,7 +228,6 @@ namespace Discord.WebSocket
if (!_users.TryRemove(id, out var user)) return null; if (!_users.TryRemove(id, out var user)) return null;
user.GlobalUser.RemoveRef(Discord); user.GlobalUser.RemoveRef(Discord);
return user; return user;

} }


//Voice States //Voice States


+ 3
- 13
src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs View File

@@ -523,10 +523,7 @@ namespace Discord.WebSocket
=> GuildHelper.GetVanityInviteAsync(this, Discord, options); => GuildHelper.GetVanityInviteAsync(this, Discord, options);


//Roles //Roles
public SocketRole GetRole(ulong id)
{
return _roles.TryGetValue(id, out var value) ? value : null;
}
public SocketRole GetRole(ulong id) => _roles.TryGetValue(id, out var value) ? value : null;


public Task<RestRole> CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), public Task<RestRole> CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?),
Color? color = default(Color?), Color? color = default(Color?),
@@ -540,16 +537,10 @@ namespace Discord.WebSocket
return role; return role;
} }


internal SocketRole RemoveRole(ulong id)
{
return _roles.TryRemove(id, out var role) ? role : null;
}
internal SocketRole RemoveRole(ulong id) => _roles.TryRemove(id, out var role) ? role : null;


//Users //Users
public SocketGuildUser GetUser(ulong id)
{
return _members.TryGetValue(id, out var member) ? member : null;
}
public SocketGuildUser GetUser(ulong id) => _members.TryGetValue(id, out var member) ? member : null;


internal SocketGuildUser AddOrUpdateUser(UserModel model) internal SocketGuildUser AddOrUpdateUser(UserModel model)
{ {
@@ -602,7 +593,6 @@ namespace Discord.WebSocket
DownloadedMemberCount--; DownloadedMemberCount--;
member.GlobalUser.RemoveRef(Discord); member.GlobalUser.RemoveRef(Discord);
return member; return member;

} }


internal void CompleteDownloadUsers() => _downloaderPromise.TrySetResultAsync(true); internal void CompleteDownloadUsers() => _downloaderPromise.TrySetResultAsync(true);


+ 2
- 8
src/Discord.Net.WebSocket/Entities/Messages/MessageCache.cs View File

@@ -37,10 +37,7 @@ namespace Discord.WebSocket
return msg; return msg;
} }


public SocketMessage Get(ulong id)
{
return _messages.TryGetValue(id, out var result) ? result : null;
}
public SocketMessage Get(ulong id) => _messages.TryGetValue(id, out var result) ? result : null;


public IReadOnlyCollection<SocketMessage> GetMany(ulong? fromMessageId, Direction dir, public IReadOnlyCollection<SocketMessage> GetMany(ulong? fromMessageId, Direction dir,
int limit = DiscordConfig.MaxMessagesPerBatch) int limit = DiscordConfig.MaxMessagesPerBatch)
@@ -60,10 +57,7 @@ namespace Discord.WebSocket
cachedMessageIds = cachedMessageIds.Reverse(); cachedMessageIds = cachedMessageIds.Reverse();


return cachedMessageIds return cachedMessageIds
.Select(x =>
{
return _messages.TryGetValue(x, out var msg) ? msg : null;
})
.Select(x => { return _messages.TryGetValue(x, out var msg) ? msg : null; })
.Where(x => x != null) .Where(x => x != null)
.Take(limit) .Take(limit)
.ToImmutableArray(); .ToImmutableArray();


+ 1
- 3
src/Discord.Net.WebSocket/Entities/Messages/SocketUserMessage.cs View File

@@ -12,11 +12,11 @@ namespace Discord.WebSocket
[DebuggerDisplay(@"{" + nameof(DebuggerDisplay) + @",nq}")] [DebuggerDisplay(@"{" + nameof(DebuggerDisplay) + @",nq}")]
public class SocketUserMessage : SocketMessage, IUserMessage public class SocketUserMessage : SocketMessage, IUserMessage
{ {
private readonly List<SocketReaction> _reactions = new List<SocketReaction>();
private ImmutableArray<Attachment> _attachments; private ImmutableArray<Attachment> _attachments;
private long? _editedTimestampTicks; private long? _editedTimestampTicks;
private ImmutableArray<Embed> _embeds; private ImmutableArray<Embed> _embeds;
private bool _isMentioningEveryone, _isTTS, _isPinned; private bool _isMentioningEveryone, _isTTS, _isPinned;
private readonly List<SocketReaction> _reactions = new List<SocketReaction>();
private ImmutableArray<ITag> _tags; private ImmutableArray<ITag> _tags;


internal SocketUserMessage(DiscordSocketClient discord, ulong id, ISocketMessageChannel channel, internal SocketUserMessage(DiscordSocketClient discord, ulong id, ISocketMessageChannel channel,
@@ -142,10 +142,8 @@ namespace Discord.WebSocket
{ {
var newMentions = ImmutableArray.CreateBuilder<SocketUnknownUser>(value.Length); var newMentions = ImmutableArray.CreateBuilder<SocketUnknownUser>(value.Length);
foreach (var val in value) foreach (var val in value)
{
if (val.Object != null) if (val.Object != null)
newMentions.Add(SocketUnknownUser.Create(Discord, state, val.Object)); newMentions.Add(SocketUnknownUser.Create(Discord, state, val.Object));
}


mentions = newMentions.ToImmutable(); mentions = newMentions.ToImmutable();
} }


+ 1
- 1
src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs View File

@@ -18,11 +18,11 @@ namespace Discord.Net.WebSockets
private readonly Dictionary<string, string> _headers; private readonly Dictionary<string, string> _headers;


private readonly SemaphoreSlim _lock; private readonly SemaphoreSlim _lock;
private readonly IWebProxy _proxy;
private CancellationToken _cancelToken, _parentToken; private CancellationToken _cancelToken, _parentToken;
private CancellationTokenSource _cancelTokenSource; private CancellationTokenSource _cancelTokenSource;
private ClientWebSocket _client; private ClientWebSocket _client;
private bool _isDisposed, _isDisconnecting; private bool _isDisposed, _isDisconnecting;
private readonly IWebProxy _proxy;
private Task _task; private Task _task;


public DefaultWebSocketClient(IWebProxy proxy = null) public DefaultWebSocketClient(IWebProxy proxy = null)


+ 2
- 2
test/Discord.Net.Tests/Net/CachedRestClient.cs View File

@@ -15,11 +15,11 @@ namespace Discord.Net
{ {
internal class CachedRestClient : IRestClient internal class CachedRestClient : IRestClient
{ {
private readonly IBlobCache _blobCache;
private readonly CancellationTokenSource _cancelTokenSource;
private readonly Dictionary<string, string> _headers; private readonly Dictionary<string, string> _headers;
private string _baseUrl; private string _baseUrl;
private readonly IBlobCache _blobCache;
private CancellationToken _cancelToken, _parentToken; private CancellationToken _cancelToken, _parentToken;
private readonly CancellationTokenSource _cancelTokenSource;
private bool _isDisposed; private bool _isDisposed;


public CachedRestClient() public CachedRestClient()


+ 2
- 1
test/Discord.Net.Tests/Tests.GuildPermissions.cs View File

@@ -44,7 +44,8 @@ namespace Discord
.Distinct() .Distinct()
.ToArray(); .ToArray();
// test GuildPermissions.All // test GuildPermissions.All
var sumOfAllGuildPermissions = enumValues.Aggregate<GuildPermission, ulong>(0, (current, v) => current | (ulong)v);
var sumOfAllGuildPermissions =
enumValues.Aggregate<GuildPermission, ulong>(0, (current, v) => current | (ulong)v);


// assert that the raw values match // assert that the raw values match
Assert.Equal(sumOfAllGuildPermissions, GuildPermissions.All.RawValue); Assert.Equal(sumOfAllGuildPermissions, GuildPermissions.All.RawValue);


+ 1
- 1
test/Discord.Net.Tests/Tests.cs View File

@@ -41,8 +41,8 @@ namespace Discord


public partial class Tests : IClassFixture<TestsFixture> public partial class Tests : IClassFixture<TestsFixture>
{ {
private DiscordRestClient _client;
private readonly RestGuild _guild; private readonly RestGuild _guild;
private DiscordRestClient _client;


public Tests(TestsFixture fixture) public Tests(TestsFixture fixture)
{ {


Loading…
Cancel
Save