diff --git a/src/Discord.Net.Commands/Map/CommandMapNode.cs b/src/Discord.Net.Commands/Map/CommandMapNode.cs index 16f469cde2..cb52376520 100644 --- a/src/Discord.Net.Commands/Map/CommandMapNode.cs +++ b/src/Discord.Net.Commands/Map/CommandMapNode.cs @@ -2,6 +2,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.Immutable; +using System.Threading; namespace Discord.Commands { @@ -11,7 +12,7 @@ internal class CommandMapNode private readonly ConcurrentDictionary _nodes; private readonly string _name; - private readonly object _lockObj = new object(); + private readonly Lock _lockObj = new(); private ImmutableArray _commands; public bool IsEmpty => _commands.Length == 0 && _nodes.Count == 0; diff --git a/src/Discord.Net.Core/Discord.Net.Core.csproj b/src/Discord.Net.Core/Discord.Net.Core.csproj index 632f00ad06..92df09afa4 100644 --- a/src/Discord.Net.Core/Discord.Net.Core.csproj +++ b/src/Discord.Net.Core/Discord.Net.Core.csproj @@ -19,5 +19,9 @@ all + + all + analyzers + \ No newline at end of file diff --git a/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs b/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs index b481a4eb21..af44aeb443 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Model = Discord.API.Interaction; @@ -32,7 +33,7 @@ public ulong CommandId /// internal new RestCommandBaseData Data { get; private set; } - private object _lock = new object(); + private readonly Lock _lock = new(); internal RestCommandBase(DiscordRestClient client, Model model) : base(client, model.Id) diff --git a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs index ab4cc4f6a3..cee094440e 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; using DataModel = Discord.API.MessageComponentInteractionData; using Model = Discord.API.Interaction; @@ -24,7 +25,7 @@ public class RestMessageComponent : RestInteraction, IComponentInteraction, IDis /// public RestUserMessage Message { get; private set; } - private object _lock = new object(); + private readonly Lock _lock = new(); internal RestMessageComponent(BaseDiscordClient client, Model model) : base(client, model.Id) diff --git a/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs b/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs index 09cc59ab1f..727e8fc1d7 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs @@ -7,6 +7,7 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Threading; using System.Threading.Tasks; using DataModel = Discord.API.ModalInteractionData; @@ -41,7 +42,7 @@ internal RestModal(DiscordRestClient client, ModelBase model) return entity; } - private object _lock = new object(); + private readonly Lock _lock = new(); /// /// Acknowledges this interaction with the if the modal was created diff --git a/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs b/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs index 6c60990b70..e1b768f026 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; using DataModel = Discord.API.AutocompleteInteractionData; using Model = Discord.API.Interaction; @@ -19,7 +20,7 @@ public class RestAutocompleteInteraction : RestInteraction, IAutocompleteInterac /// public new RestAutocompleteInteractionData Data { get; } - private object _lock = new object(); + private readonly Lock _lock = new(); internal RestAutocompleteInteraction(DiscordRestClient client, Model model) : base(client, model.Id) diff --git a/src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs b/src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs index 1fa7494d6e..7b790dbc7c 100644 --- a/src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs +++ b/src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs @@ -17,7 +17,7 @@ internal class RequestBucket { private const int MinimumSleepTimeMs = 750; - private readonly object _lock; + private readonly Lock _lock; private readonly RequestQueue _queue; private int _semaphore; private DateTimeOffset? _resetTick; @@ -32,7 +32,7 @@ public RequestBucket(RequestQueue queue, IRequest request, BucketId id) _queue = queue; Id = id; - _lock = new object(); + _lock = new(); if (request.Options.IsClientBucket) WindowCount = ClientBucket.Get(request.Options.BucketId).WindowCount; diff --git a/src/Discord.Net.WebSocket/DiscordShardedClient.cs b/src/Discord.Net.WebSocket/DiscordShardedClient.cs index 68fcbc63ef..1f2c6e214e 100644 --- a/src/Discord.Net.WebSocket/DiscordShardedClient.cs +++ b/src/Discord.Net.WebSocket/DiscordShardedClient.cs @@ -21,7 +21,7 @@ public partial class DiscordShardedClient : BaseSocketClient, IDiscordClient private ImmutableArray> _defaultStickers; private int _totalShards; private SemaphoreSlim[] _identifySemaphores; - private object _semaphoreResetLock; + private readonly Lock _semaphoreResetLock; private Task _semaphoreResetTask; private bool _isDisposed; @@ -80,7 +80,7 @@ private DiscordShardedClient(int[] ids, DiscordSocketConfig config, API.DiscordS if (ids != null && config.TotalShards == null) throw new ArgumentException($"Custom ids are not supported when {nameof(config.TotalShards)} is not specified."); - _semaphoreResetLock = new object(); + _semaphoreResetLock = new(); _shardIdsToIndex = new Dictionary(); config.DisplayInitialLog = false; _baseConfig = config; diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs index 39a51b01fe..5ba5610329 100644 --- a/src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs +++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.IO; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Model = Discord.API.Channel; using ThreadMember = Discord.API.ThreadMember; @@ -110,8 +111,8 @@ public bool IsPrivateThread private bool _usersDownloaded; - private readonly object _downloadLock = new object(); - private readonly object _ownerLock = new object(); + private readonly Lock _downloadLock = new(); + private readonly Lock _ownerLock = new(); private ulong _ownerId; diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs b/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs index 957accca24..9b69ec249d 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading; using System.Threading.Tasks; using DataModel = Discord.API.MessageComponentInteractionData; using Model = Discord.API.Interaction; @@ -24,7 +25,7 @@ public class SocketMessageComponent : SocketInteraction, IComponentInteraction, /// public SocketUserMessage Message { get; private set; } - private object _lock = new object(); + private readonly Lock _lock = new(); public override bool HasResponded { get; internal set; } = false; internal SocketMessageComponent(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user) diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs b/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs index a6de28d332..36a8e507de 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs @@ -7,6 +7,7 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Threading; using System.Threading.Tasks; using DataModel = Discord.API.ModalInteractionData; @@ -66,7 +67,7 @@ internal SocketModal(DiscordSocketClient client, ModelBase model, ISocketMessage /// public override bool HasResponded { get; internal set; } - private object _lock = new object(); + private readonly Lock _lock = new(); /// public override async Task RespondWithFilesAsync( diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketAutocompleteInteraction.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketAutocompleteInteraction.cs index c4521513ff..5528e07e6c 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketAutocompleteInteraction.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketAutocompleteInteraction.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Threading; using System.Threading.Tasks; using DataModel = Discord.API.AutocompleteInteractionData; using Model = Discord.API.Interaction; @@ -21,7 +22,7 @@ public class SocketAutocompleteInteraction : SocketInteraction, IAutocompleteInt /// public override bool HasResponded { get; internal set; } - private object _lock = new object(); + private readonly Lock _lock = new(); internal SocketAutocompleteInteraction(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user) : base(client, model.Id, channel, user) diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketCommandBase.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketCommandBase.cs index 914df8ac33..c0c2eebdef 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketCommandBase.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketCommandBase.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading; using System.Threading.Tasks; using DataModel = Discord.API.ApplicationCommandInteractionData; using Model = Discord.API.Interaction; @@ -35,7 +36,7 @@ public ulong CommandId /// public override bool HasResponded { get; internal set; } - private object _lock = new object(); + private readonly Lock _lock = new(); internal SocketCommandBase(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user) : base(client, model.Id, channel, user) diff --git a/src/Discord.Net.WebSocket/Entities/Users/SocketGlobalUser.cs b/src/Discord.Net.WebSocket/Entities/Users/SocketGlobalUser.cs index 7f89e49a08..5b453c181c 100644 --- a/src/Discord.Net.WebSocket/Entities/Users/SocketGlobalUser.cs +++ b/src/Discord.Net.WebSocket/Entities/Users/SocketGlobalUser.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics; using System.Linq; +using System.Threading; using Model = Discord.API.User; namespace Discord.WebSocket @@ -31,7 +32,7 @@ internal class SocketGlobalUser : SocketUser /// internal override SocketGlobalUser GlobalUser { get => this; set => throw new NotImplementedException(); } - private readonly object _lockObj = new object(); + private readonly Lock _lockObj = new(); private ushort _references; private SocketGlobalUser(DiscordSocketClient discord, ulong id)