diff --git a/src/Discord.Net.Commands/Map/CommandMapNode.cs b/src/Discord.Net.Commands/Map/CommandMapNode.cs index 16f469cd..cb523765 100644 --- a/src/Discord.Net.Commands/Map/CommandMapNode.cs +++ b/src/Discord.Net.Commands/Map/CommandMapNode.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.Immutable; +using System.Threading; namespace Discord.Commands { @@ -11,7 +12,7 @@ namespace Discord.Commands 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 632f00ad..92df09af 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 b481a4eb..af44aeb4 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; 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 @@ namespace Discord.Rest /// 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 ab4cc4f6..cee09444 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.Collections.Generic; 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 @@ namespace Discord.Rest /// 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 09cc59ab..727e8fc1 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.ComponentModel; 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 @@ namespace Discord.Rest 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 6c60990b..e1b768f0 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.Collections.Generic; 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 @@ namespace Discord.Rest /// 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 1fa7494d..7b790dbc 100644 --- a/src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs +++ b/src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs @@ -17,7 +17,7 @@ namespace Discord.Net.Queue { 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 @@ namespace Discord.Net.Queue _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 68fcbc63..1f2c6e21 100644 --- a/src/Discord.Net.WebSocket/DiscordShardedClient.cs +++ b/src/Discord.Net.WebSocket/DiscordShardedClient.cs @@ -21,7 +21,7 @@ namespace Discord.WebSocket 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 @@ namespace Discord.WebSocket 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 39a51b01..5ba56103 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.Collections.Immutable; 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 @@ namespace Discord.WebSocket 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 957accca..9b69ec24 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; 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 @@ namespace Discord.WebSocket /// 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 a6de28d3..36a8e507 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.Collections.Generic; 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 @@ namespace Discord.WebSocket /// 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 c4521513..5528e07e 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 Discord.Rest; 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 @@ namespace Discord.WebSocket /// 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 914df8ac..c0c2eebd 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; 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 @@ namespace Discord.WebSocket /// 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 7f89e49a..5b453c18 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 @@ namespace Discord.WebSocket /// 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)