@@ -20,8 +20,7 @@ namespace Discord | |||||
public static Emoji Parse(string text) | public static Emoji Parse(string text) | ||||
{ | { | ||||
Emoji result; | |||||
if (TryParse(text, out result)) | |||||
if (TryParse(text, out Emoji result)) | |||||
return result; | return result; | ||||
throw new ArgumentException("Invalid emoji format", nameof(text)); | throw new ArgumentException("Invalid emoji format", nameof(text)); | ||||
} | } | ||||
@@ -35,8 +34,7 @@ namespace Discord | |||||
if (splitIndex == -1) | if (splitIndex == -1) | ||||
return false; | return false; | ||||
ulong id; | |||||
if (!ulong.TryParse(text.Substring(splitIndex + 1, text.Length - splitIndex - 2), NumberStyles.None, CultureInfo.InvariantCulture, out id)) | |||||
if (!ulong.TryParse(text.Substring(splitIndex + 1, text.Length - splitIndex - 2), NumberStyles.None, CultureInfo.InvariantCulture, out ulong id)) | |||||
return false; | return false; | ||||
string name = text.Substring(2, splitIndex - 2); | string name = text.Substring(2, splitIndex - 2); | ||||
@@ -239,10 +239,8 @@ namespace Discord | |||||
{ | { | ||||
while (true) | while (true) | ||||
{ | { | ||||
int bucketNo, lockNo; | |||||
Tables tables = _tables; | Tables tables = _tables; | ||||
GetBucketAndLockNo(hashcode, out bucketNo, out lockNo, tables._buckets.Length, tables._locks.Length); | |||||
GetBucketAndLockNo(hashcode, out int bucketNo, out int lockNo, tables._buckets.Length, tables._locks.Length); | |||||
bool resizeDesired = false; | bool resizeDesired = false; | ||||
bool lockTaken = false; | bool lockTaken = false; | ||||
@@ -292,9 +290,7 @@ namespace Discord | |||||
while (true) | while (true) | ||||
{ | { | ||||
Tables tables = _tables; | Tables tables = _tables; | ||||
int bucketNo, lockNo; | |||||
GetBucketAndLockNo(hashcode, out bucketNo, out lockNo, tables._buckets.Length, tables._locks.Length); | |||||
GetBucketAndLockNo(hashcode, out int bucketNo, out int lockNo, tables._buckets.Length, tables._locks.Length); | |||||
lock (tables._locks[lockNo]) | lock (tables._locks[lockNo]) | ||||
{ | { | ||||
@@ -426,8 +422,7 @@ namespace Discord | |||||
while (current != null) | while (current != null) | ||||
{ | { | ||||
Node next = current._next; | Node next = current._next; | ||||
int newBucketNo, newLockNo; | |||||
GetBucketAndLockNo(current._hashcode, out newBucketNo, out newLockNo, newBuckets.Length, newLocks.Length); | |||||
GetBucketAndLockNo(current._hashcode, out int newBucketNo, out int newLockNo, newBuckets.Length, newLocks.Length); | |||||
newBuckets[newBucketNo] = new Node(current._value, current._hashcode, newBuckets[newBucketNo]); | newBuckets[newBucketNo] = new Node(current._value, current._hashcode, newBuckets[newBucketNo]); | ||||
@@ -19,8 +19,7 @@ namespace Discord | |||||
/// <summary> Parses a provided user mention string. </summary> | /// <summary> Parses a provided user mention string. </summary> | ||||
public static ulong ParseUser(string text) | public static ulong ParseUser(string text) | ||||
{ | { | ||||
ulong id; | |||||
if (TryParseUser(text, out id)) | |||||
if (TryParseUser(text, out ulong id)) | |||||
return id; | return id; | ||||
throw new ArgumentException("Invalid mention format", nameof(text)); | throw new ArgumentException("Invalid mention format", nameof(text)); | ||||
} | } | ||||
@@ -44,8 +43,7 @@ namespace Discord | |||||
/// <summary> Parses a provided channel mention string. </summary> | /// <summary> Parses a provided channel mention string. </summary> | ||||
public static ulong ParseChannel(string text) | public static ulong ParseChannel(string text) | ||||
{ | { | ||||
ulong id; | |||||
if (TryParseChannel(text, out id)) | |||||
if (TryParseChannel(text, out ulong id)) | |||||
return id; | return id; | ||||
throw new ArgumentException("Invalid mention format", nameof(text)); | throw new ArgumentException("Invalid mention format", nameof(text)); | ||||
} | } | ||||
@@ -66,8 +64,7 @@ namespace Discord | |||||
/// <summary> Parses a provided role mention string. </summary> | /// <summary> Parses a provided role mention string. </summary> | ||||
public static ulong ParseRole(string text) | public static ulong ParseRole(string text) | ||||
{ | { | ||||
ulong id; | |||||
if (TryParseRole(text, out id)) | |||||
if (TryParseRole(text, out ulong id)) | |||||
return id; | return id; | ||||
throw new ArgumentException("Invalid mention format", nameof(text)); | throw new ArgumentException("Invalid mention format", nameof(text)); | ||||
} | } | ||||
@@ -60,8 +60,7 @@ namespace Discord.Rest | |||||
public RestUser GetUser(ulong id) | public RestUser GetUser(ulong id) | ||||
{ | { | ||||
RestGroupUser user; | |||||
if (_users.TryGetValue(id, out user)) | |||||
if (_users.TryGetValue(id, out RestGroupUser user)) | |||||
return user; | return user; | ||||
return null; | return null; | ||||
} | } | ||||
@@ -213,8 +213,7 @@ namespace Discord.Rest | |||||
//Roles | //Roles | ||||
public RestRole GetRole(ulong id) | public RestRole GetRole(ulong id) | ||||
{ | { | ||||
RestRole value; | |||||
if (_roles.TryGetValue(id, out value)) | |||||
if (_roles.TryGetValue(id, out RestRole value)) | |||||
return value; | return value; | ||||
return null; | return null; | ||||
} | } | ||||
@@ -58,8 +58,7 @@ namespace Discord.Rest | |||||
{ | { | ||||
if (Guild != null) | if (Guild != null) | ||||
return Guild; | return Guild; | ||||
var guildChannel = Channel as IGuildChannel; | |||||
if (guildChannel != null) | |||||
if (Channel is IGuildChannel guildChannel) | |||||
return guildChannel.Guild; //If it fails, it'll still return this exception | return guildChannel.Guild; //If it fails, it'll still return this exception | ||||
throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object."); | throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object."); | ||||
} | } | ||||
@@ -80,8 +80,7 @@ namespace Discord.Rest | |||||
if (endIndex == -1) break; | if (endIndex == -1) break; | ||||
string content = text.Substring(index, endIndex - index + 1); | string content = text.Substring(index, endIndex - index + 1); | ||||
ulong id; | |||||
if (MentionUtils.TryParseUser(content, out id)) | |||||
if (MentionUtils.TryParseUser(content, out ulong id)) | |||||
{ | { | ||||
IUser mentionedUser = null; | IUser mentionedUser = null; | ||||
foreach (var mention in userMentions) | foreach (var mention in userMentions) | ||||
@@ -19,8 +19,7 @@ namespace Discord.Net.Converters | |||||
if (property.Ignored) | if (property.Ignored) | ||||
return property; | return property; | ||||
var propInfo = member as PropertyInfo; | |||||
if (propInfo != null) | |||||
if (member is PropertyInfo propInfo) | |||||
{ | { | ||||
var converter = GetConverter(property, propInfo, propInfo.PropertyType, 0); | var converter = GetConverter(property, propInfo, propInfo.PropertyType, 0); | ||||
if (converter != null) | if (converter != null) | ||||
@@ -114,9 +114,8 @@ namespace Discord.Net.Queue | |||||
var now = DateTimeOffset.UtcNow; | var now = DateTimeOffset.UtcNow; | ||||
foreach (var bucket in _buckets.Select(x => x.Value)) | foreach (var bucket in _buckets.Select(x => x.Value)) | ||||
{ | { | ||||
RequestBucket ignored; | |||||
if ((now - bucket.LastAttemptAt).TotalMinutes > 1.0) | if ((now - bucket.LastAttemptAt).TotalMinutes > 1.0) | ||||
_buckets.TryRemove(bucket.Id, out ignored); | |||||
_buckets.TryRemove(bucket.Id, out RequestBucket ignored); | |||||
} | } | ||||
await Task.Delay(60000, _cancelToken.Token); //Runs each minute | await Task.Delay(60000, _cancelToken.Token); //Runs each minute | ||||
} | } | ||||
@@ -14,9 +14,8 @@ namespace Discord.Net | |||||
internal RateLimitInfo(Dictionary<string, string> headers) | internal RateLimitInfo(Dictionary<string, string> headers) | ||||
{ | { | ||||
string temp; | |||||
IsGlobal = headers.TryGetValue("X-RateLimit-Global", out temp) && | |||||
bool.TryParse(temp, out var isGlobal) ? isGlobal : false; | |||||
IsGlobal = headers.TryGetValue("X-RateLimit-Global", out string temp) && | |||||
bool.TryParse(temp, out var isGlobal) ? isGlobal : false; | |||||
Limit = headers.TryGetValue("X-RateLimit-Limit", out temp) && | Limit = headers.TryGetValue("X-RateLimit-Limit", out temp) && | ||||
int.TryParse(temp, out var limit) ? limit : (int?)null; | int.TryParse(temp, out var limit) ? limit : (int?)null; | ||||
Remaining = headers.TryGetValue("X-RateLimit-Remaining", out temp) && | Remaining = headers.TryGetValue("X-RateLimit-Remaining", out temp) && | ||||
@@ -378,8 +378,7 @@ namespace Discord.API | |||||
private bool ProcessMessage(API.Rpc.RpcFrame msg) | private bool ProcessMessage(API.Rpc.RpcFrame msg) | ||||
{ | { | ||||
RpcRequest requestTracker; | |||||
if (_requests.TryGetValue(msg.Nonce.Value.Value, out requestTracker)) | |||||
if (_requests.TryGetValue(msg.Nonce.Value.Value, out RpcRequest requestTracker)) | |||||
{ | { | ||||
if (msg.Event.GetValueOrDefault("") == "ERROR") | if (msg.Event.GetValueOrDefault("") == "ERROR") | ||||
{ | { | ||||
@@ -131,8 +131,7 @@ namespace Discord.Audio | |||||
await keepaliveTask.ConfigureAwait(false); | await keepaliveTask.ConfigureAwait(false); | ||||
_keepaliveTask = null; | _keepaliveTask = null; | ||||
long time; | |||||
while (_heartbeatTimes.TryDequeue(out time)) { } | |||||
while (_heartbeatTimes.TryDequeue(out long time)) { } | |||||
_lastMessageTime = 0; | _lastMessageTime = 0; | ||||
await ClearInputStreamsAsync().ConfigureAwait(false); | await ClearInputStreamsAsync().ConfigureAwait(false); | ||||
@@ -186,8 +185,7 @@ namespace Discord.Audio | |||||
} | } | ||||
internal AudioInStream GetInputStream(ulong id) | internal AudioInStream GetInputStream(ulong id) | ||||
{ | { | ||||
StreamPair streamPair; | |||||
if (_streams.TryGetValue(id, out streamPair)) | |||||
if (_streams.TryGetValue(id, out StreamPair streamPair)) | |||||
return streamPair.Reader; | return streamPair.Reader; | ||||
return null; | return null; | ||||
} | } | ||||
@@ -254,8 +252,7 @@ namespace Discord.Audio | |||||
{ | { | ||||
await _audioLogger.DebugAsync("Received HeartbeatAck").ConfigureAwait(false); | await _audioLogger.DebugAsync("Received HeartbeatAck").ConfigureAwait(false); | ||||
long time; | |||||
if (_heartbeatTimes.TryDequeue(out time)) | |||||
if (_heartbeatTimes.TryDequeue(out long time)) | |||||
{ | { | ||||
int latency = (int)(Environment.TickCount - time); | int latency = (int)(Environment.TickCount - time); | ||||
int before = Latency; | int before = Latency; | ||||
@@ -85,8 +85,7 @@ namespace Discord.Audio.Streams | |||||
long dist = nextTick - tick; | long dist = nextTick - tick; | ||||
if (dist <= 0) | if (dist <= 0) | ||||
{ | { | ||||
Frame frame; | |||||
if (_queuedFrames.TryDequeue(out frame)) | |||||
if (_queuedFrames.TryDequeue(out Frame frame)) | |||||
{ | { | ||||
await _client.SetSpeakingAsync(true).ConfigureAwait(false); | await _client.SetSpeakingAsync(true).ConfigureAwait(false); | ||||
_next.WriteHeader(seq++, timestamp, false); | _next.WriteHeader(seq++, timestamp, false); | ||||
@@ -100,7 +99,7 @@ namespace Discord.Audio.Streams | |||||
var _ = _logger?.DebugAsync($"Sent {frame.Bytes} bytes ({_queuedFrames.Count} frames buffered)"); | var _ = _logger?.DebugAsync($"Sent {frame.Bytes} bytes ({_queuedFrames.Count} frames buffered)"); | ||||
#endif | #endif | ||||
} | } | ||||
else | |||||
else | |||||
{ | { | ||||
while ((nextTick - tick) <= 0) | while ((nextTick - tick) <= 0) | ||||
{ | { | ||||
@@ -135,8 +134,7 @@ namespace Discord.Audio.Streams | |||||
cancelToken = _cancelToken; | cancelToken = _cancelToken; | ||||
await _queueLock.WaitAsync(-1, cancelToken).ConfigureAwait(false); | await _queueLock.WaitAsync(-1, cancelToken).ConfigureAwait(false); | ||||
byte[] buffer; | |||||
if (!_bufferPool.TryDequeue(out buffer)) | |||||
if (!_bufferPool.TryDequeue(out byte[] buffer)) | |||||
{ | { | ||||
#if DEBUG | #if DEBUG | ||||
var _ = _logger?.DebugAsync($"Buffer overflow"); //Should never happen because of the queueLock | var _ = _logger?.DebugAsync($"Buffer overflow"); //Should never happen because of the queueLock | ||||
@@ -166,10 +164,9 @@ namespace Discord.Audio.Streams | |||||
} | } | ||||
public override Task ClearAsync(CancellationToken cancelToken) | public override Task ClearAsync(CancellationToken cancelToken) | ||||
{ | { | ||||
Frame ignored; | |||||
do | do | ||||
cancelToken.ThrowIfCancellationRequested(); | cancelToken.ThrowIfCancellationRequested(); | ||||
while (_queuedFrames.TryDequeue(out ignored)); | |||||
while (_queuedFrames.TryDequeue(out Frame ignored)); | |||||
return Task.Delay(0); | return Task.Delay(0); | ||||
} | } | ||||
} | } |
@@ -55,9 +55,8 @@ namespace Discord.Audio.Streams | |||||
{ | { | ||||
cancelToken.ThrowIfCancellationRequested(); | cancelToken.ThrowIfCancellationRequested(); | ||||
RTPFrame frame; | |||||
await _signal.WaitAsync(cancelToken).ConfigureAwait(false); | await _signal.WaitAsync(cancelToken).ConfigureAwait(false); | ||||
_frames.TryDequeue(out frame); | |||||
_frames.TryDequeue(out RTPFrame frame); | |||||
return frame; | return frame; | ||||
} | } | ||||
@@ -94,8 +94,7 @@ namespace Discord.Audio.Streams | |||||
continue; | continue; | ||||
} | } | ||||
Frame frame; | |||||
if (_queuedFrames.TryPeek(out frame)) | |||||
if (_queuedFrames.TryPeek(out Frame frame)) | |||||
{ | { | ||||
silenceFrames = 0; | silenceFrames = 0; | ||||
uint distance = (uint)(frame.Timestamp - _timestamp); | uint distance = (uint)(frame.Timestamp - _timestamp); | ||||
@@ -201,7 +200,6 @@ namespace Discord.Audio.Streams | |||||
return; //This is an old frame, ignore | return; //This is an old frame, ignore | ||||
} | } | ||||
byte[] buffer; | |||||
if (!await _queueLock.WaitAsync(0).ConfigureAwait(false)) | if (!await _queueLock.WaitAsync(0).ConfigureAwait(false)) | ||||
{ | { | ||||
#if DEBUG | #if DEBUG | ||||
@@ -209,7 +207,7 @@ namespace Discord.Audio.Streams | |||||
#endif | #endif | ||||
return; | return; | ||||
} | } | ||||
_bufferPool.TryDequeue(out buffer); | |||||
_bufferPool.TryDequeue(out byte[] buffer); | |||||
Buffer.BlockCopy(data, offset, buffer, 0, count); | Buffer.BlockCopy(data, offset, buffer, 0, count); | ||||
#if DEBUG | #if DEBUG | ||||
@@ -239,10 +237,9 @@ namespace Discord.Audio.Streams | |||||
} | } | ||||
public override Task ClearAsync(CancellationToken cancelToken) | public override Task ClearAsync(CancellationToken cancelToken) | ||||
{ | { | ||||
Frame ignored; | |||||
do | do | ||||
cancelToken.ThrowIfCancellationRequested(); | cancelToken.ThrowIfCancellationRequested(); | ||||
while (_queuedFrames.TryDequeue(out ignored)); | |||||
while (_queuedFrames.TryDequeue(out Frame ignored)); | |||||
return Task.Delay(0); | return Task.Delay(0); | ||||
} | } | ||||
} | } |
@@ -41,15 +41,13 @@ namespace Discord.WebSocket | |||||
internal SocketChannel GetChannel(ulong id) | internal SocketChannel GetChannel(ulong id) | ||||
{ | { | ||||
SocketChannel channel; | |||||
if (_channels.TryGetValue(id, out channel)) | |||||
if (_channels.TryGetValue(id, out SocketChannel channel)) | |||||
return channel; | return channel; | ||||
return null; | return null; | ||||
} | } | ||||
internal SocketDMChannel GetDMChannel(ulong userId) | internal SocketDMChannel GetDMChannel(ulong userId) | ||||
{ | { | ||||
SocketDMChannel channel; | |||||
if (_dmChannels.TryGetValue(userId, out channel)) | |||||
if (_dmChannels.TryGetValue(userId, out SocketDMChannel channel)) | |||||
return channel; | return channel; | ||||
return null; | return null; | ||||
} | } | ||||
@@ -57,31 +55,25 @@ namespace Discord.WebSocket | |||||
{ | { | ||||
_channels[channel.Id] = channel; | _channels[channel.Id] = channel; | ||||
var dmChannel = channel as SocketDMChannel; | |||||
if (dmChannel != null) | |||||
if (channel is SocketDMChannel dmChannel) | |||||
_dmChannels[dmChannel.Recipient.Id] = dmChannel; | _dmChannels[dmChannel.Recipient.Id] = dmChannel; | ||||
else | else | ||||
{ | { | ||||
var groupChannel = channel as SocketGroupChannel; | |||||
if (groupChannel != null) | |||||
if (channel is SocketGroupChannel groupChannel) | |||||
_groupChannels.TryAdd(groupChannel.Id); | _groupChannels.TryAdd(groupChannel.Id); | ||||
} | } | ||||
} | } | ||||
internal SocketChannel RemoveChannel(ulong id) | internal SocketChannel RemoveChannel(ulong id) | ||||
{ | { | ||||
SocketChannel channel; | |||||
if (_channels.TryRemove(id, out channel)) | |||||
if (_channels.TryRemove(id, out SocketChannel channel)) | |||||
{ | { | ||||
var dmChannel = channel as SocketDMChannel; | |||||
if (dmChannel != null) | |||||
if (channel is SocketDMChannel dmChannel) | |||||
{ | { | ||||
SocketDMChannel ignored; | |||||
_dmChannels.TryRemove(dmChannel.Recipient.Id, out ignored); | |||||
_dmChannels.TryRemove(dmChannel.Recipient.Id, out SocketDMChannel ignored); | |||||
} | } | ||||
else | else | ||||
{ | { | ||||
var groupChannel = channel as SocketGroupChannel; | |||||
if (groupChannel != null) | |||||
if (channel is SocketGroupChannel groupChannel) | |||||
_groupChannels.TryRemove(id); | _groupChannels.TryRemove(id); | ||||
} | } | ||||
return channel; | return channel; | ||||
@@ -91,8 +83,7 @@ namespace Discord.WebSocket | |||||
internal SocketGuild GetGuild(ulong id) | internal SocketGuild GetGuild(ulong id) | ||||
{ | { | ||||
SocketGuild guild; | |||||
if (_guilds.TryGetValue(id, out guild)) | |||||
if (_guilds.TryGetValue(id, out SocketGuild guild)) | |||||
return guild; | return guild; | ||||
return null; | return null; | ||||
} | } | ||||
@@ -102,16 +93,14 @@ namespace Discord.WebSocket | |||||
} | } | ||||
internal SocketGuild RemoveGuild(ulong id) | internal SocketGuild RemoveGuild(ulong id) | ||||
{ | { | ||||
SocketGuild guild; | |||||
if (_guilds.TryRemove(id, out guild)) | |||||
if (_guilds.TryRemove(id, out SocketGuild guild)) | |||||
return guild; | return guild; | ||||
return null; | return null; | ||||
} | } | ||||
internal SocketGlobalUser GetUser(ulong id) | internal SocketGlobalUser GetUser(ulong id) | ||||
{ | { | ||||
SocketGlobalUser user; | |||||
if (_users.TryGetValue(id, out user)) | |||||
if (_users.TryGetValue(id, out SocketGlobalUser user)) | |||||
return user; | return user; | ||||
return null; | return null; | ||||
} | } | ||||
@@ -121,8 +110,7 @@ namespace Discord.WebSocket | |||||
} | } | ||||
internal SocketGlobalUser RemoveUser(ulong id) | internal SocketGlobalUser RemoveUser(ulong id) | ||||
{ | { | ||||
SocketGlobalUser user; | |||||
if (_users.TryRemove(id, out user)) | |||||
if (_users.TryRemove(id, out SocketGlobalUser user)) | |||||
return user; | return user; | ||||
return null; | return null; | ||||
} | } | ||||
@@ -197,7 +197,6 @@ namespace Discord.WebSocket | |||||
} | } | ||||
private async Task OnDisconnectingAsync(Exception ex) | private async Task OnDisconnectingAsync(Exception ex) | ||||
{ | { | ||||
ulong guildId; | |||||
await _gatewayLogger.DebugAsync("Disconnecting ApiClient").ConfigureAwait(false); | await _gatewayLogger.DebugAsync("Disconnecting ApiClient").ConfigureAwait(false); | ||||
await ApiClient.DisconnectAsync().ConfigureAwait(false); | await ApiClient.DisconnectAsync().ConfigureAwait(false); | ||||
@@ -209,8 +208,7 @@ namespace Discord.WebSocket | |||||
await heartbeatTask.ConfigureAwait(false); | await heartbeatTask.ConfigureAwait(false); | ||||
_heartbeatTask = null; | _heartbeatTask = null; | ||||
long time; | |||||
while (_heartbeatTimes.TryDequeue(out time)) { } | |||||
while (_heartbeatTimes.TryDequeue(out long time)) { } | |||||
_lastMessageTime = 0; | _lastMessageTime = 0; | ||||
await _gatewayLogger.DebugAsync("Waiting for guild downloader").ConfigureAwait(false); | await _gatewayLogger.DebugAsync("Waiting for guild downloader").ConfigureAwait(false); | ||||
@@ -221,7 +219,7 @@ namespace Discord.WebSocket | |||||
//Clear large guild queue | //Clear large guild queue | ||||
await _gatewayLogger.DebugAsync("Clearing large guild queue").ConfigureAwait(false); | await _gatewayLogger.DebugAsync("Clearing large guild queue").ConfigureAwait(false); | ||||
while (_largeGuilds.TryDequeue(out guildId)) { } | |||||
while (_largeGuilds.TryDequeue(out ulong guildId)) { } | |||||
//Raise virtual GUILD_UNAVAILABLEs | //Raise virtual GUILD_UNAVAILABLEs | ||||
await _gatewayLogger.DebugAsync("Raising virtual GuildUnavailables").ConfigureAwait(false); | await _gatewayLogger.DebugAsync("Raising virtual GuildUnavailables").ConfigureAwait(false); | ||||
@@ -298,8 +296,7 @@ namespace Discord.WebSocket | |||||
/// <inheritdoc /> | /// <inheritdoc /> | ||||
public RestVoiceRegion GetVoiceRegion(string id) | public RestVoiceRegion GetVoiceRegion(string id) | ||||
{ | { | ||||
RestVoiceRegion region; | |||||
if (_voiceRegions.TryGetValue(id, out region)) | |||||
if (_voiceRegions.TryGetValue(id, out RestVoiceRegion region)) | |||||
return region; | return region; | ||||
return null; | return null; | ||||
} | } | ||||
@@ -417,8 +414,7 @@ namespace Discord.WebSocket | |||||
{ | { | ||||
await _gatewayLogger.DebugAsync("Received HeartbeatAck").ConfigureAwait(false); | await _gatewayLogger.DebugAsync("Received HeartbeatAck").ConfigureAwait(false); | ||||
long time; | |||||
if (_heartbeatTimes.TryDequeue(out time)) | |||||
if (_heartbeatTimes.TryDequeue(out long time)) | |||||
{ | { | ||||
int latency = (int)(Environment.TickCount - time); | int latency = (int)(Environment.TickCount - time); | ||||
int before = Latency; | int before = Latency; | ||||
@@ -903,8 +899,7 @@ namespace Discord.WebSocket | |||||
await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_RECIPIENT_ADD)").ConfigureAwait(false); | await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_RECIPIENT_ADD)").ConfigureAwait(false); | ||||
var data = (payload as JToken).ToObject<RecipientEvent>(_serializer); | var data = (payload as JToken).ToObject<RecipientEvent>(_serializer); | ||||
var channel = State.GetChannel(data.ChannelId) as SocketGroupChannel; | |||||
if (channel != null) | |||||
if (State.GetChannel(data.ChannelId) is SocketGroupChannel channel) | |||||
{ | { | ||||
var user = channel.GetOrAddUser(data.User); | var user = channel.GetOrAddUser(data.User); | ||||
await TimedInvokeAsync(_recipientAddedEvent, nameof(RecipientAdded), user).ConfigureAwait(false); | await TimedInvokeAsync(_recipientAddedEvent, nameof(RecipientAdded), user).ConfigureAwait(false); | ||||
@@ -921,8 +916,7 @@ namespace Discord.WebSocket | |||||
await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_RECIPIENT_REMOVE)").ConfigureAwait(false); | await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_RECIPIENT_REMOVE)").ConfigureAwait(false); | ||||
var data = (payload as JToken).ToObject<RecipientEvent>(_serializer); | var data = (payload as JToken).ToObject<RecipientEvent>(_serializer); | ||||
var channel = State.GetChannel(data.ChannelId) as SocketGroupChannel; | |||||
if (channel != null) | |||||
if (State.GetChannel(data.ChannelId) is SocketGroupChannel channel) | |||||
{ | { | ||||
var user = channel.RemoveUser(data.User.Id); | var user = channel.RemoveUser(data.User.Id); | ||||
if (user != null) | if (user != null) | ||||
@@ -1094,12 +1088,11 @@ namespace Discord.WebSocket | |||||
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_CREATE)").ConfigureAwait(false); | await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_CREATE)").ConfigureAwait(false); | ||||
var data = (payload as JToken).ToObject<API.Message>(_serializer); | var data = (payload as JToken).ToObject<API.Message>(_serializer); | ||||
var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel; | |||||
if (channel != null) | |||||
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) | |||||
{ | { | ||||
var guild = (channel as SocketGuildChannel)?.Guild; | var guild = (channel as SocketGuildChannel)?.Guild; | ||||
if (guild != null && !guild.IsSynced) | if (guild != null && !guild.IsSynced) | ||||
{ | |||||
{ | |||||
await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false); | await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false); | ||||
return; | return; | ||||
} | } | ||||
@@ -1142,8 +1135,7 @@ namespace Discord.WebSocket | |||||
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_UPDATE)").ConfigureAwait(false); | await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_UPDATE)").ConfigureAwait(false); | ||||
var data = (payload as JToken).ToObject<API.Message>(_serializer); | var data = (payload as JToken).ToObject<API.Message>(_serializer); | ||||
var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel; | |||||
if (channel != null) | |||||
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) | |||||
{ | { | ||||
var guild = (channel as SocketGuildChannel)?.Guild; | var guild = (channel as SocketGuildChannel)?.Guild; | ||||
if (guild != null && !guild.IsSynced) | if (guild != null && !guild.IsSynced) | ||||
@@ -1174,9 +1166,9 @@ namespace Discord.WebSocket | |||||
after = SocketMessage.Create(this, State, author, channel, data); | after = SocketMessage.Create(this, State, author, channel, data); | ||||
} | } | ||||
var cacheableBefore = new Cacheable<IMessage, ulong>(before, data.Id, isCached , async () => await channel.GetMessageAsync(data.Id)); | |||||
var cacheableBefore = new Cacheable<IMessage, ulong>(before, data.Id, isCached, async () => await channel.GetMessageAsync(data.Id)); | |||||
await TimedInvokeAsync(_messageUpdatedEvent, nameof(MessageUpdated), cacheableBefore, after, channel).ConfigureAwait(false); | |||||
await TimedInvokeAsync(_messageUpdatedEvent, nameof(MessageUpdated), cacheableBefore, after, channel).ConfigureAwait(false); | |||||
} | } | ||||
else | else | ||||
{ | { | ||||
@@ -1190,8 +1182,7 @@ namespace Discord.WebSocket | |||||
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_DELETE)").ConfigureAwait(false); | await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_DELETE)").ConfigureAwait(false); | ||||
var data = (payload as JToken).ToObject<API.Message>(_serializer); | var data = (payload as JToken).ToObject<API.Message>(_serializer); | ||||
var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel; | |||||
if (channel != null) | |||||
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) | |||||
{ | { | ||||
var guild = (channel as SocketGuildChannel)?.Guild; | var guild = (channel as SocketGuildChannel)?.Guild; | ||||
if (!(guild?.IsSynced ?? true)) | if (!(guild?.IsSynced ?? true)) | ||||
@@ -1218,8 +1209,7 @@ namespace Discord.WebSocket | |||||
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_ADD)").ConfigureAwait(false); | await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_ADD)").ConfigureAwait(false); | ||||
var data = (payload as JToken).ToObject<API.Gateway.Reaction>(_serializer); | var data = (payload as JToken).ToObject<API.Gateway.Reaction>(_serializer); | ||||
var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel; | |||||
if (channel != null) | |||||
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) | |||||
{ | { | ||||
SocketUserMessage cachedMsg = channel.GetCachedMessage(data.MessageId) as SocketUserMessage; | SocketUserMessage cachedMsg = channel.GetCachedMessage(data.MessageId) as SocketUserMessage; | ||||
bool isCached = cachedMsg != null; | bool isCached = cachedMsg != null; | ||||
@@ -1243,8 +1233,7 @@ namespace Discord.WebSocket | |||||
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_REMOVE)").ConfigureAwait(false); | await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_REMOVE)").ConfigureAwait(false); | ||||
var data = (payload as JToken).ToObject<API.Gateway.Reaction>(_serializer); | var data = (payload as JToken).ToObject<API.Gateway.Reaction>(_serializer); | ||||
var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel; | |||||
if (channel != null) | |||||
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) | |||||
{ | { | ||||
SocketUserMessage cachedMsg = channel.GetCachedMessage(data.MessageId) as SocketUserMessage; | SocketUserMessage cachedMsg = channel.GetCachedMessage(data.MessageId) as SocketUserMessage; | ||||
bool isCached = cachedMsg != null; | bool isCached = cachedMsg != null; | ||||
@@ -1268,8 +1257,7 @@ namespace Discord.WebSocket | |||||
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_REMOVE_ALL)").ConfigureAwait(false); | await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_REMOVE_ALL)").ConfigureAwait(false); | ||||
var data = (payload as JToken).ToObject<RemoveAllReactionsEvent>(_serializer); | var data = (payload as JToken).ToObject<RemoveAllReactionsEvent>(_serializer); | ||||
var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel; | |||||
if (channel != null) | |||||
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) | |||||
{ | { | ||||
SocketUserMessage cachedMsg = channel.GetCachedMessage(data.MessageId) as SocketUserMessage; | SocketUserMessage cachedMsg = channel.GetCachedMessage(data.MessageId) as SocketUserMessage; | ||||
bool isCached = cachedMsg != null; | bool isCached = cachedMsg != null; | ||||
@@ -1291,8 +1279,7 @@ namespace Discord.WebSocket | |||||
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_DELETE_BULK)").ConfigureAwait(false); | await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_DELETE_BULK)").ConfigureAwait(false); | ||||
var data = (payload as JToken).ToObject<MessageDeleteBulkEvent>(_serializer); | var data = (payload as JToken).ToObject<MessageDeleteBulkEvent>(_serializer); | ||||
var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel; | |||||
if (channel != null) | |||||
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) | |||||
{ | { | ||||
var guild = (channel as SocketGuildChannel)?.Guild; | var guild = (channel as SocketGuildChannel)?.Guild; | ||||
if (!(guild?.IsSynced ?? true)) | if (!(guild?.IsSynced ?? true)) | ||||
@@ -1376,8 +1363,7 @@ namespace Discord.WebSocket | |||||
await _gatewayLogger.DebugAsync("Received Dispatch (TYPING_START)").ConfigureAwait(false); | await _gatewayLogger.DebugAsync("Received Dispatch (TYPING_START)").ConfigureAwait(false); | ||||
var data = (payload as JToken).ToObject<TypingStartEvent>(_serializer); | var data = (payload as JToken).ToObject<TypingStartEvent>(_serializer); | ||||
var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel; | |||||
if (channel != null) | |||||
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) | |||||
{ | { | ||||
var guild = (channel as SocketGuildChannel)?.Guild; | var guild = (channel as SocketGuildChannel)?.Guild; | ||||
if (!(guild?.IsSynced ?? true)) | if (!(guild?.IsSynced ?? true)) | ||||
@@ -120,15 +120,13 @@ namespace Discord.WebSocket | |||||
//Users | //Users | ||||
public new SocketGroupUser GetUser(ulong id) | public new SocketGroupUser GetUser(ulong id) | ||||
{ | { | ||||
SocketGroupUser user; | |||||
if (_users.TryGetValue(id, out user)) | |||||
if (_users.TryGetValue(id, out SocketGroupUser user)) | |||||
return user; | return user; | ||||
return null; | return null; | ||||
} | } | ||||
internal SocketGroupUser GetOrAddUser(UserModel model) | internal SocketGroupUser GetOrAddUser(UserModel model) | ||||
{ | { | ||||
SocketGroupUser user; | |||||
if (_users.TryGetValue(model.Id, out user)) | |||||
if (_users.TryGetValue(model.Id, out SocketGroupUser user)) | |||||
return user as SocketGroupUser; | return user as SocketGroupUser; | ||||
else | else | ||||
{ | { | ||||
@@ -139,8 +137,7 @@ namespace Discord.WebSocket | |||||
} | } | ||||
internal SocketGroupUser RemoveUser(ulong id) | internal SocketGroupUser RemoveUser(ulong id) | ||||
{ | { | ||||
SocketGroupUser user; | |||||
if (_users.TryRemove(id, out user)) | |||||
if (_users.TryRemove(id, out SocketGroupUser user)) | |||||
{ | { | ||||
user.GlobalUser.RemoveRef(Discord); | user.GlobalUser.RemoveRef(Discord); | ||||
return user as SocketGroupUser; | return user as SocketGroupUser; | ||||
@@ -158,15 +155,13 @@ namespace Discord.WebSocket | |||||
} | } | ||||
internal SocketVoiceState? GetVoiceState(ulong id) | internal SocketVoiceState? GetVoiceState(ulong id) | ||||
{ | { | ||||
SocketVoiceState voiceState; | |||||
if (_voiceStates.TryGetValue(id, out voiceState)) | |||||
if (_voiceStates.TryGetValue(id, out SocketVoiceState voiceState)) | |||||
return voiceState; | return voiceState; | ||||
return null; | return null; | ||||
} | } | ||||
internal SocketVoiceState? RemoveVoiceState(ulong id) | internal SocketVoiceState? RemoveVoiceState(ulong id) | ||||
{ | { | ||||
SocketVoiceState voiceState; | |||||
if (_voiceStates.TryRemove(id, out voiceState)) | |||||
if (_voiceStates.TryRemove(id, out SocketVoiceState voiceState)) | |||||
return voiceState; | return voiceState; | ||||
return null; | return null; | ||||
} | } | ||||
@@ -82,16 +82,7 @@ namespace Discord.WebSocket | |||||
=> Channels.Select(x => x as SocketTextChannel).Where(x => x != null).ToImmutableArray(); | => Channels.Select(x => x as SocketTextChannel).Where(x => x != null).ToImmutableArray(); | ||||
public IReadOnlyCollection<SocketVoiceChannel> VoiceChannels | public IReadOnlyCollection<SocketVoiceChannel> VoiceChannels | ||||
=> Channels.Select(x => x as SocketVoiceChannel).Where(x => x != null).ToImmutableArray(); | => Channels.Select(x => x as SocketVoiceChannel).Where(x => x != null).ToImmutableArray(); | ||||
public SocketGuildUser CurrentUser | |||||
{ | |||||
get | |||||
{ | |||||
SocketGuildUser member; | |||||
if (_members.TryGetValue(Discord.CurrentUser.Id, out member)) | |||||
return member; | |||||
return null; | |||||
} | |||||
} | |||||
public SocketGuildUser CurrentUser => _members.TryGetValue(Discord.CurrentUser.Id, out SocketGuildUser member) ? member : null; | |||||
public SocketRole EveryoneRole => GetRole(Id); | public SocketRole EveryoneRole => GetRole(Id); | ||||
public IReadOnlyCollection<SocketGuildChannel> Channels | public IReadOnlyCollection<SocketGuildChannel> Channels | ||||
{ | { | ||||
@@ -162,8 +153,7 @@ namespace Discord.WebSocket | |||||
for (int i = 0; i < model.Presences.Length; i++) | for (int i = 0; i < model.Presences.Length; i++) | ||||
{ | { | ||||
SocketGuildUser member; | |||||
if (members.TryGetValue(model.Presences[i].User.Id, out member)) | |||||
if (members.TryGetValue(model.Presences[i].User.Id, out SocketGuildUser member)) | |||||
member.Update(state, model.Presences[i], true); | member.Update(state, model.Presences[i], true); | ||||
else | else | ||||
Debug.Assert(false); | Debug.Assert(false); | ||||
@@ -248,8 +238,7 @@ namespace Discord.WebSocket | |||||
for (int i = 0; i < model.Presences.Length; i++) | for (int i = 0; i < model.Presences.Length; i++) | ||||
{ | { | ||||
SocketGuildUser member; | |||||
if (members.TryGetValue(model.Presences[i].User.Id, out member)) | |||||
if (members.TryGetValue(model.Presences[i].User.Id, out SocketGuildUser member)) | |||||
member.Update(state, model.Presences[i], true); | member.Update(state, model.Presences[i], true); | ||||
else | else | ||||
Debug.Assert(false); | Debug.Assert(false); | ||||
@@ -343,8 +332,7 @@ namespace Discord.WebSocket | |||||
//Roles | //Roles | ||||
public SocketRole GetRole(ulong id) | public SocketRole GetRole(ulong id) | ||||
{ | { | ||||
SocketRole value; | |||||
if (_roles.TryGetValue(id, out value)) | |||||
if (_roles.TryGetValue(id, out SocketRole value)) | |||||
return value; | return value; | ||||
return null; | return null; | ||||
} | } | ||||
@@ -359,8 +347,7 @@ namespace Discord.WebSocket | |||||
} | } | ||||
internal SocketRole RemoveRole(ulong id) | internal SocketRole RemoveRole(ulong id) | ||||
{ | { | ||||
SocketRole role; | |||||
if (_roles.TryRemove(id, out role)) | |||||
if (_roles.TryRemove(id, out SocketRole role)) | |||||
return role; | return role; | ||||
return null; | return null; | ||||
} | } | ||||
@@ -368,8 +355,7 @@ namespace Discord.WebSocket | |||||
//Users | //Users | ||||
public SocketGuildUser GetUser(ulong id) | public SocketGuildUser GetUser(ulong id) | ||||
{ | { | ||||
SocketGuildUser member; | |||||
if (_members.TryGetValue(id, out member)) | |||||
if (_members.TryGetValue(id, out SocketGuildUser member)) | |||||
return member; | return member; | ||||
return null; | return null; | ||||
} | } | ||||
@@ -378,8 +364,7 @@ namespace Discord.WebSocket | |||||
internal SocketGuildUser AddOrUpdateUser(UserModel model) | internal SocketGuildUser AddOrUpdateUser(UserModel model) | ||||
{ | { | ||||
SocketGuildUser member; | |||||
if (_members.TryGetValue(model.Id, out member)) | |||||
if (_members.TryGetValue(model.Id, out SocketGuildUser member)) | |||||
member.GlobalUser?.Update(Discord.State, model); | member.GlobalUser?.Update(Discord.State, model); | ||||
else | else | ||||
{ | { | ||||
@@ -391,8 +376,7 @@ namespace Discord.WebSocket | |||||
} | } | ||||
internal SocketGuildUser AddOrUpdateUser(MemberModel model) | internal SocketGuildUser AddOrUpdateUser(MemberModel model) | ||||
{ | { | ||||
SocketGuildUser member; | |||||
if (_members.TryGetValue(model.User.Id, out member)) | |||||
if (_members.TryGetValue(model.User.Id, out SocketGuildUser member)) | |||||
member.Update(Discord.State, model); | member.Update(Discord.State, model); | ||||
else | else | ||||
{ | { | ||||
@@ -404,8 +388,7 @@ namespace Discord.WebSocket | |||||
} | } | ||||
internal SocketGuildUser AddOrUpdateUser(PresenceModel model) | internal SocketGuildUser AddOrUpdateUser(PresenceModel model) | ||||
{ | { | ||||
SocketGuildUser member; | |||||
if (_members.TryGetValue(model.User.Id, out member)) | |||||
if (_members.TryGetValue(model.User.Id, out SocketGuildUser member)) | |||||
member.Update(Discord.State, model, false); | member.Update(Discord.State, model, false); | ||||
else | else | ||||
{ | { | ||||
@@ -417,8 +400,7 @@ namespace Discord.WebSocket | |||||
} | } | ||||
internal SocketGuildUser RemoveUser(ulong id) | internal SocketGuildUser RemoveUser(ulong id) | ||||
{ | { | ||||
SocketGuildUser member; | |||||
if (_members.TryRemove(id, out member)) | |||||
if (_members.TryRemove(id, out SocketGuildUser member)) | |||||
{ | { | ||||
DownloadedMemberCount--; | DownloadedMemberCount--; | ||||
member.GlobalUser.RemoveRef(Discord); | member.GlobalUser.RemoveRef(Discord); | ||||
@@ -466,15 +448,13 @@ namespace Discord.WebSocket | |||||
} | } | ||||
internal SocketVoiceState? GetVoiceState(ulong id) | internal SocketVoiceState? GetVoiceState(ulong id) | ||||
{ | { | ||||
SocketVoiceState voiceState; | |||||
if (_voiceStates.TryGetValue(id, out voiceState)) | |||||
if (_voiceStates.TryGetValue(id, out SocketVoiceState voiceState)) | |||||
return voiceState; | return voiceState; | ||||
return null; | return null; | ||||
} | } | ||||
internal async Task<SocketVoiceState?> RemoveVoiceStateAsync(ulong id) | internal async Task<SocketVoiceState?> RemoveVoiceStateAsync(ulong id) | ||||
{ | { | ||||
SocketVoiceState voiceState; | |||||
if (_voiceStates.TryRemove(id, out voiceState)) | |||||
if (_voiceStates.TryRemove(id, out SocketVoiceState voiceState)) | |||||
{ | { | ||||
if (_audioClient != null) | if (_audioClient != null) | ||||
await _audioClient.RemoveInputStreamAsync(id).ConfigureAwait(false); //User changed channels, end their stream | await _audioClient.RemoveInputStreamAsync(id).ConfigureAwait(false); //User changed channels, end their stream | ||||
@@ -27,24 +27,20 @@ namespace Discord.WebSocket | |||||
{ | { | ||||
_orderedMessages.Enqueue(message.Id); | _orderedMessages.Enqueue(message.Id); | ||||
ulong msgId; | |||||
SocketMessage msg; | |||||
while (_orderedMessages.Count > _size && _orderedMessages.TryDequeue(out msgId)) | |||||
_messages.TryRemove(msgId, out msg); | |||||
while (_orderedMessages.Count > _size && _orderedMessages.TryDequeue(out ulong msgId)) | |||||
_messages.TryRemove(msgId, out SocketMessage msg); | |||||
} | } | ||||
} | } | ||||
public SocketMessage Remove(ulong id) | public SocketMessage Remove(ulong id) | ||||
{ | { | ||||
SocketMessage msg; | |||||
_messages.TryRemove(id, out msg); | |||||
_messages.TryRemove(id, out SocketMessage msg); | |||||
return msg; | return msg; | ||||
} | } | ||||
public SocketMessage Get(ulong id) | public SocketMessage Get(ulong id) | ||||
{ | { | ||||
SocketMessage result; | |||||
if (_messages.TryGetValue(id, out result)) | |||||
if (_messages.TryGetValue(id, out SocketMessage result)) | |||||
return result; | return result; | ||||
return null; | return null; | ||||
} | } | ||||
@@ -67,8 +63,7 @@ namespace Discord.WebSocket | |||||
return cachedMessageIds | return cachedMessageIds | ||||
.Select(x => | .Select(x => | ||||
{ | { | ||||
SocketMessage msg; | |||||
if (_messages.TryGetValue(x, out msg)) | |||||
if (_messages.TryGetValue(x, out SocketMessage msg)) | |||||
return msg; | return msg; | ||||
return null; | return null; | ||||
}) | }) | ||||
@@ -206,8 +206,7 @@ namespace Discord.Net.WebSockets | |||||
//Use the internal buffer if we can get it | //Use the internal buffer if we can get it | ||||
resultCount = (int)stream.Length; | resultCount = (int)stream.Length; | ||||
ArraySegment<byte> streamBuffer; | |||||
if (stream.TryGetBuffer(out streamBuffer)) | |||||
if (stream.TryGetBuffer(out ArraySegment<byte> streamBuffer)) | |||||
result = streamBuffer.Array; | result = streamBuffer.Array; | ||||
else | else | ||||
result = stream.ToArray(); | result = stream.ToArray(); | ||||