From 2ce3262d59359dc6911360966d04a4979c86b8a1 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sat, 8 Jan 2022 11:51:41 +0100 Subject: [PATCH] Fixed around 140 wrong namings and other refactorings which were marked as warnings --- src/NadekoBot.Coordinator/CoordStartup.cs | 4 +- .../Services/CoordinatorRunner.cs | 18 +- .../Services/CoordinatorService.cs | 6 +- .../Shared/JsonStatusObject.cs | 4 +- .../Cloneable/CloneableGenerator.cs | 97 +++++------ .../Cloneable/SymbolExtensions.cs | 4 +- .../Command/CommandAttributesGenerator.cs | 20 ++- src/NadekoBot.Tests/BotStringsTests.cs | 2 +- src/NadekoBot.Tests/GroupGreetTests.cs | 6 +- src/NadekoBot.Tests/IndexedCollectionTests.cs | 2 +- src/NadekoBot.Tests/KwumTests.cs | 3 +- src/NadekoBot.Tests/Random.cs | 6 +- src/NadekoBot.VotesApi/Common/AuthHandler.cs | 5 +- .../Common/TopggVoteWebhookModel.cs | 2 +- .../Controllers/DiscordsController.cs | 6 +- .../Controllers/TopGgController.cs | 2 +- .../Controllers/WebhookController.cs | 8 +- src/NadekoBot.VotesApi/Program.cs | 24 +-- .../Services/FileVotesCache.cs | 41 ++--- src/NadekoBot.VotesApi/Startup.cs | 23 ++- src/NadekoBot.VotesApi/WeatherForecast.cs | 2 - src/NadekoBot/Bot.cs | 4 +- .../Common/Collections/ConcurrentHashSet.cs | 73 ++++---- .../Common/Collections/IndexedCollection.cs | 4 +- src/NadekoBot/Common/ILogCommandService.cs | 2 +- src/NadekoBot/Common/NadekoModule.cs | 4 +- .../Common/TypeReaders/Models/StoopidTime.cs | 2 - .../Yml/CommentGatheringTypeInspector.cs | 32 ++-- .../Common/Yml/CommentsObjectDescriptor.cs | 12 +- .../SelfAssignableRolesExtensions.cs | 2 +- src/NadekoBot/Db/Models/GCChannelId.cs | 2 +- .../Modules/Administration/Administration.cs | 6 +- .../Administration/AdministrationService.cs | 2 +- .../DangerousCommands/DangerousCommands.cs | 10 +- .../DangerousCommandsService.cs | 10 +- .../GameVoiceChannelService.cs | 4 +- .../Administration/GreetBye/GreetService.cs | 4 +- .../Administration/ImageOnlyChannelService.cs | 2 +- .../Administration/Mute/MuteService.cs | 9 +- .../Administration/Prefix/PrefixCommands.cs | 16 +- .../Protection/ProtectionService.cs | 4 +- .../Protection/ProtectionStats.cs | 6 +- .../Administration/Prune/PruneCommands.cs | 6 +- .../Administration/Role/RoleCommands.cs | 7 +- .../Role/RoleCommandsService.cs | 2 +- .../SelfAssignedRolesCommands.cs | 4 +- .../SelfAssignedRolesService.cs | 11 +- .../ServerLog/ServerLogCommandService.cs | 44 +++-- .../ServerLog/ServerLogCommands.cs | 2 +- .../UserPunish/UserPunishService.cs | 5 +- .../Administration/VcRole/VcRoleService.cs | 4 +- .../NadekoExpressionExtensions.cs | 8 +- .../Gambling/AnimalRacing/AnimalRace.cs | 14 +- .../AnimalRacing/AnimalRacingCommands.cs | 2 +- .../Gambling/BlackJack/BlackJackCommands.cs | 15 +- .../Modules/Gambling/BlackJack/Blackjack.cs | 10 +- .../Modules/Gambling/Connect4/Connect4.cs | 58 +++---- .../Gambling/Connect4/Connect4Commands.cs | 10 +- .../Modules/Gambling/Draw/DrawCommands.cs | 12 +- .../Gambling/Events/GameStatusEvent.cs | 46 +++-- .../Modules/Gambling/Events/ReactionEvent.cs | 48 +++--- .../Gambling/PlantPick/PlantPickService.cs | 15 +- .../Gambling/Raffle/CurrencyRaffleService.cs | 12 +- .../Modules/Gambling/Slot/SlotCommands.cs | 2 +- .../Modules/Games/Acrophobia/Acrophobia.cs | 2 +- .../Games/Acrophobia/AcropobiaCommands.cs | 2 +- src/NadekoBot/Modules/Games/Nunchi/Nunchi.cs | 6 +- .../Modules/Games/Nunchi/NunchiCommands.cs | 2 +- .../Modules/Games/Polls/PollRunner.cs | 2 +- .../Modules/Games/SpeedTyping/TypingGame.cs | 2 +- .../Modules/Games/TicTacToe/TicTacToe.cs | 2 +- .../Games/TicTacToe/TicTacToeCommands.cs | 2 +- .../Modules/Games/Trivia/TriviaGame.cs | 22 +-- .../Modules/Games/Trivia/TriviaQuestion.cs | 4 +- .../Modules/Music/Services/extractor/Misc.cs | 4 +- .../Music/Services/extractor/YtLoader.cs | 14 +- .../Modules/Music/_Common/Impl/MusicQueue.cs | 158 +++++++++--------- .../Modules/Music/_Common/Impl/VoiceProxy.cs | 18 +- .../_Common/Resolvers/LocalTrackResolver.cs | 3 +- .../Modules/Nsfw/SearchImageCacher.cs | 24 +-- .../Permissions/Blacklist/BlacklistService.cs | 18 +- .../CommandCooldown/CmdCdService.cs | 2 +- .../Permissions/Filter/FilterService.cs | 2 +- .../Modules/Searches/Crypto/CryptoService.cs | 4 +- .../Modules/Searches/Feeds/FeedsService.cs | 2 +- src/NadekoBot/Modules/Searches/Searches.cs | 4 +- .../Modules/Searches/SearchesService.cs | 6 +- .../Modules/Searches/XkcdCommands.cs | 10 +- .../Utility/CommandMap/CommandMapService.cs | 2 +- .../Modules/Utility/Quote/QuoteCommands.cs | 4 +- .../Utility/StreamRole/StreamRoleService.cs | 31 ++-- .../UnitConversion/ConverterService.cs | 3 - src/NadekoBot/Modules/Utility/Utility.cs | 17 +- src/NadekoBot/Modules/Xp/XpService.cs | 4 +- .../Services/IEmbedBuilderService.cs | 30 ++-- .../Services/Impl/BehaviorExecutor.cs | 26 +-- .../Services/Impl/GoogleApiService.cs | 36 ++-- src/NadekoBot/Services/Impl/RedisCache.cs | 81 ++++----- .../Services/Impl/RedisImageExtensions.cs | 6 +- src/NadekoBot/Services/Impl/StatsService.cs | 12 +- src/NadekoBot/Services/Impl/YtdlOperation.cs | 4 +- .../Services/Settings/BotConfigService.cs | 6 +- src/NadekoBot/_Extensions/Extensions.cs | 4 +- src/NadekoBot/_Extensions/StringExtensions.cs | 1 - src/ayu/Ayu.Discord.Voice/LibOpus.cs | 8 +- .../PoopyBufferImmortalized.cs | 10 +- src/ayu/Ayu.Discord.Voice/SocketClient.cs | 2 +- src/ayu/Ayu.Discord.Voice/VoiceClient.cs | 4 +- src/ayu/Ayu.Discord.Voice/VoiceGateway.cs | 20 +-- 109 files changed, 698 insertions(+), 760 deletions(-) diff --git a/src/NadekoBot.Coordinator/CoordStartup.cs b/src/NadekoBot.Coordinator/CoordStartup.cs index 2775c20b9..432a72341 100644 --- a/src/NadekoBot.Coordinator/CoordStartup.cs +++ b/src/NadekoBot.Coordinator/CoordStartup.cs @@ -12,9 +12,7 @@ namespace NadekoBot.Coordinator public IConfiguration Configuration { get; } public CoordStartup(IConfiguration config) - { - Configuration = config; - } + => Configuration = config; public void ConfigureServices(IServiceCollection services) { diff --git a/src/NadekoBot.Coordinator/Services/CoordinatorRunner.cs b/src/NadekoBot.Coordinator/Services/CoordinatorRunner.cs index a5f793a07..874906f3d 100644 --- a/src/NadekoBot.Coordinator/Services/CoordinatorRunner.cs +++ b/src/NadekoBot.Coordinator/Services/CoordinatorRunner.cs @@ -6,7 +6,6 @@ using System.Linq; using System.Text.Json; using System.Threading; using System.Threading.Tasks; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Serilog; using YamlDotNet.Serialization; @@ -30,7 +29,7 @@ namespace NadekoBot.Coordinator private readonly Random _rng; private bool _gracefulImminent; - public CoordinatorRunner(IConfiguration configuration) + public CoordinatorRunner() { _serializer = new(); _deserializer = new(); @@ -91,7 +90,7 @@ namespace NadekoBot.Coordinator var shardIds = Enumerable.Range(0, 1) // shard 0 is always first .Append((int)((117523346618318850 >> 22) % _config.TotalShards)) // then nadeko server shard .Concat(Enumerable.Range(1, _config.TotalShards - 1) - .OrderBy(x => _rng.Next())) // then all other shards in a random order + .OrderBy(_ => _rng.Next())) // then all other shards in a random order .Distinct() .ToList(); @@ -191,8 +190,7 @@ namespace NadekoBot.Coordinator } private Process StartShardProcess(int shardId) - { - return Process.Start(new ProcessStartInfo() + => Process.Start(new ProcessStartInfo() { FileName = _config.ShardStartCommand, Arguments = string.Format(_config.ShardStartArgs, @@ -205,7 +203,6 @@ namespace NadekoBot.Coordinator // CreateNoWindow = true, // UseShellExecute = false, }); - } public bool Heartbeat(int shardId, int guildCount, ConnState state) { @@ -239,7 +236,6 @@ namespace NadekoBot.Coordinator { lock (locker) { - ref var toSave = ref _config; SaveConfig(new Config( totalShards, _config.RecheckIntervalMs, @@ -284,7 +280,7 @@ namespace NadekoBot.Coordinator for (var shardId = 0; shardId < _shardStatuses.Length; shardId++) { var status = _shardStatuses[shardId]; - if (status.Process is Process p) + if (status.Process is { } p) { p.Kill(); p.Dispose(); @@ -346,7 +342,7 @@ namespace NadekoBot.Coordinator if (savedState.StatusObjects.Count != _config.TotalShards) { - Log.Error("Unable to restore old state because shard count doesn't match."); + Log.Error("Unable to restore old state because shard count doesn't match"); File.Move(GRACEFUL_STATE_PATH, GRACEFUL_STATE_BACKUP_PATH, overwrite: true); return false; } @@ -357,7 +353,7 @@ namespace NadekoBot.Coordinator { var statusObj = savedState.StatusObjects[shardId]; Process p = null; - if (statusObj.Pid is int pid) + if (statusObj.Pid is { } pid) { try { @@ -365,7 +361,7 @@ namespace NadekoBot.Coordinator } catch (Exception ex) { - Log.Warning(ex, $"Process for shard {shardId} is not runnning."); + Log.Warning(ex, "Process for shard {ShardId} is not runnning", shardId); } } diff --git a/src/NadekoBot.Coordinator/Services/CoordinatorService.cs b/src/NadekoBot.Coordinator/Services/CoordinatorService.cs index 3ade834f9..f4d7c5b0f 100644 --- a/src/NadekoBot.Coordinator/Services/CoordinatorService.cs +++ b/src/NadekoBot.Coordinator/Services/CoordinatorService.cs @@ -5,14 +5,12 @@ using Grpc.Core; namespace NadekoBot.Coordinator { - public sealed class CoordinatorService : NadekoBot.Coordinator.Coordinator.CoordinatorBase + public sealed class CoordinatorService : Coordinator.CoordinatorBase { private readonly CoordinatorRunner _runner; public CoordinatorService(CoordinatorRunner runner) - { - _runner = runner; - } + => _runner = runner; public override Task Heartbeat(HeartbeatRequest request, ServerCallContext context) { diff --git a/src/NadekoBot.Coordinator/Shared/JsonStatusObject.cs b/src/NadekoBot.Coordinator/Shared/JsonStatusObject.cs index 6a98c3a85..0a9d4bb9b 100644 --- a/src/NadekoBot.Coordinator/Shared/JsonStatusObject.cs +++ b/src/NadekoBot.Coordinator/Shared/JsonStatusObject.cs @@ -1,6 +1,4 @@ -using System; - -namespace NadekoBot.Coordinator +namespace NadekoBot.Coordinator { public class JsonStatusObject { diff --git a/src/NadekoBot.Generators/Cloneable/CloneableGenerator.cs b/src/NadekoBot.Generators/Cloneable/CloneableGenerator.cs index cdf607f1e..0c583fb1b 100644 --- a/src/NadekoBot.Generators/Cloneable/CloneableGenerator.cs +++ b/src/NadekoBot.Generators/Cloneable/CloneableGenerator.cs @@ -6,7 +6,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Text; -using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -16,71 +15,69 @@ namespace Cloneable [Generator] public class CloneableGenerator : ISourceGenerator { - private const string PreventDeepCopyKeyString = "PreventDeepCopy"; - private const string ExplicitDeclarationKeyString = "ExplicitDeclaration"; + private const string PREVENT_DEEP_COPY_KEY_STRING = "PreventDeepCopy"; + private const string EXPLICIT_DECLARATION_KEY_STRING = "ExplicitDeclaration"; - private const string CloneableNamespace = "Cloneable"; - private const string CloneableAttributeString = "CloneableAttribute"; - private const string CloneAttributeString = "CloneAttribute"; - private const string IgnoreCloneAttributeString = "IgnoreCloneAttribute"; + private const string CLONEABLE_NAMESPACE = "Cloneable"; + private const string CLONEABLE_ATTRIBUTE_STRING = "CloneableAttribute"; + private const string CLONE_ATTRIBUTE_STRING = "CloneAttribute"; + private const string IGNORE_CLONE_ATTRIBUTE_STRING = "IgnoreCloneAttribute"; - private const string cloneableAttributeText = @"// + private const string CLONEABLE_ATTRIBUTE_TEXT = @"// using System; -namespace " + CloneableNamespace + @" +namespace " + CLONEABLE_NAMESPACE + @" { [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = true, AllowMultiple = false)] - public sealed class " + CloneableAttributeString + @" : Attribute + public sealed class " + CLONEABLE_ATTRIBUTE_STRING + @" : Attribute { - public " + CloneableAttributeString + @"() + public " + CLONEABLE_ATTRIBUTE_STRING + @"() { } - public bool " + ExplicitDeclarationKeyString + @" { get; set; } + public bool " + EXPLICIT_DECLARATION_KEY_STRING + @" { get; set; } } } "; - private const string clonePropertyAttributeText = @"// + private const string CLONE_PROPERTY_ATTRIBUTE_TEXT = @"// using System; -namespace " + CloneableNamespace + @" +namespace " + CLONEABLE_NAMESPACE + @" { [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)] - public sealed class " + CloneAttributeString + @" : Attribute + public sealed class " + CLONE_ATTRIBUTE_STRING + @" : Attribute { - public " + CloneAttributeString + @"() + public " + CLONE_ATTRIBUTE_STRING + @"() { } - public bool " + PreventDeepCopyKeyString + @" { get; set; } + public bool " + PREVENT_DEEP_COPY_KEY_STRING + @" { get; set; } } } "; - private const string ignoreClonePropertyAttributeText = @"// + private const string IGNORE_CLONE_PROPERTY_ATTRIBUTE_TEXT = @"// using System; -namespace " + CloneableNamespace + @" +namespace " + CLONEABLE_NAMESPACE + @" { [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)] - public sealed class " + IgnoreCloneAttributeString + @" : Attribute + public sealed class " + IGNORE_CLONE_ATTRIBUTE_STRING + @" : Attribute { - public " + IgnoreCloneAttributeString + @"() + public " + IGNORE_CLONE_ATTRIBUTE_STRING + @"() { } } } "; - private INamedTypeSymbol? cloneableAttribute; - private INamedTypeSymbol? ignoreCloneAttribute; - private INamedTypeSymbol? cloneAttribute; + private INamedTypeSymbol? _cloneableAttribute; + private INamedTypeSymbol? _ignoreCloneAttribute; + private INamedTypeSymbol? _cloneAttribute; public void Initialize(GeneratorInitializationContext context) - { - context.RegisterForSyntaxNotifications(() => new SyntaxReceiver()); - } + => context.RegisterForSyntaxNotifications(() => new SyntaxReceiver()); public void Execute(GeneratorExecutionContext context) { @@ -100,36 +97,36 @@ namespace " + CloneableNamespace + @" var classSymbols = GetClassSymbols(compilation, receiver); foreach (var classSymbol in classSymbols) { - if (!classSymbol.TryGetAttribute(cloneableAttribute!, out var attributes)) + if (!classSymbol.TryGetAttribute(_cloneableAttribute!, out var attributes)) continue; var attribute = attributes.Single(); - var isExplicit = (bool?)attribute.NamedArguments.FirstOrDefault(e => e.Key.Equals(ExplicitDeclarationKeyString)).Value.Value ?? false; + var isExplicit = (bool?)attribute.NamedArguments.FirstOrDefault(e => e.Key.Equals(EXPLICIT_DECLARATION_KEY_STRING)).Value.Value ?? false; context.AddSource($"{classSymbol.Name}_cloneable.g.cs", SourceText.From(CreateCloneableCode(classSymbol, isExplicit), Encoding.UTF8)); } } private void InitAttributes(Compilation compilation) { - cloneableAttribute = compilation.GetTypeByMetadataName($"{CloneableNamespace}.{CloneableAttributeString}")!; - cloneAttribute = compilation.GetTypeByMetadataName($"{CloneableNamespace}.{CloneAttributeString}")!; - ignoreCloneAttribute = compilation.GetTypeByMetadataName($"{CloneableNamespace}.{IgnoreCloneAttributeString}")!; + _cloneableAttribute = compilation.GetTypeByMetadataName($"{CLONEABLE_NAMESPACE}.{CLONEABLE_ATTRIBUTE_STRING}")!; + _cloneAttribute = compilation.GetTypeByMetadataName($"{CLONEABLE_NAMESPACE}.{CLONE_ATTRIBUTE_STRING}")!; + _ignoreCloneAttribute = compilation.GetTypeByMetadataName($"{CLONEABLE_NAMESPACE}.{IGNORE_CLONE_ATTRIBUTE_STRING}")!; } private static Compilation GetCompilation(GeneratorExecutionContext context) { var options = context.Compilation.SyntaxTrees.First().Options as CSharpParseOptions; - var compilation = context.Compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(cloneableAttributeText, Encoding.UTF8), options)). - AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(clonePropertyAttributeText, Encoding.UTF8), options)). - AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(ignoreClonePropertyAttributeText, Encoding.UTF8), options)); + var compilation = context.Compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(CLONEABLE_ATTRIBUTE_TEXT, Encoding.UTF8), options)). + AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(CLONE_PROPERTY_ATTRIBUTE_TEXT, Encoding.UTF8), options)). + AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(IGNORE_CLONE_PROPERTY_ATTRIBUTE_TEXT, Encoding.UTF8), options)); return compilation; } private string CreateCloneableCode(INamedTypeSymbol classSymbol, bool isExplicit) { string namespaceName = classSymbol.ContainingNamespace.ToDisplayString(); - var fieldAssignmentsCode = GenerateFieldAssignmentsCode(classSymbol, isExplicit); + var fieldAssignmentsCode = GenerateFieldAssignmentsCode(classSymbol, isExplicit).ToList(); var fieldAssignmentsCodeSafe = fieldAssignmentsCode.Select(x => { if (x.isCloneable) @@ -187,9 +184,9 @@ namespace {namespaceName} { var fieldNames = GetCloneableProperties(classSymbol, isExplicit); - var fieldAssignments = fieldNames.Select(field => IsFieldCloneable(field, classSymbol)). - OrderBy(x => x.isCloneable). - Select(x => (GenerateAssignmentCode(x.item.Name, x.isCloneable), x.isCloneable)); + var fieldAssignments = fieldNames.Select(field => IsFieldCloneable(field, classSymbol)) + .OrderBy(x => x.isCloneable) + .Select(x => (GenerateAssignmentCode(x.item.Name, x.isCloneable), x.isCloneable)); return fieldAssignments; } @@ -210,19 +207,17 @@ namespace {namespaceName} return (x, false); } - if (!x.Type.TryGetAttribute(cloneableAttribute!, out var attributes)) + if (!x.Type.TryGetAttribute(_cloneableAttribute!, out var attributes)) { return (x, false); } - var preventDeepCopy = (bool?)attributes.Single().NamedArguments.FirstOrDefault(e => e.Key.Equals(PreventDeepCopyKeyString)).Value.Value ?? false; + var preventDeepCopy = (bool?)attributes.Single().NamedArguments.FirstOrDefault(e => e.Key.Equals(PREVENT_DEEP_COPY_KEY_STRING)).Value.Value ?? false; return (item: x, !preventDeepCopy); } private string GetAccessModifier(INamedTypeSymbol classSymbol) - { - return classSymbol.DeclaredAccessibility.ToString().ToLowerInvariant(); - } + => classSymbol.DeclaredAccessibility.ToString().ToLowerInvariant(); private IEnumerable GetCloneableProperties(ITypeSymbol classSymbol, bool isExplicit) { @@ -231,18 +226,16 @@ namespace {namespaceName} x.CanBeReferencedByName); if (isExplicit) { - return targetSymbolMembers.Where(x => x.HasAttribute(cloneAttribute!)); + return targetSymbolMembers.Where(x => x.HasAttribute(_cloneAttribute!)); } else { - return targetSymbolMembers.Where(x => !x.HasAttribute(ignoreCloneAttribute!)); + return targetSymbolMembers.Where(x => !x.HasAttribute(_ignoreCloneAttribute!)); } } private static IEnumerable GetClassSymbols(Compilation compilation, SyntaxReceiver receiver) - { - return receiver.CandidateClasses.Select(clazz => GetClassSymbol(compilation, clazz)); - } + => receiver.CandidateClasses.Select(clazz => GetClassSymbol(compilation, clazz)); private static INamedTypeSymbol GetClassSymbol(Compilation compilation, ClassDeclarationSyntax clazz) { @@ -253,9 +246,9 @@ namespace {namespaceName} private static void InjectCloneableAttributes(GeneratorExecutionContext context) { - context.AddSource(CloneableAttributeString, SourceText.From(cloneableAttributeText, Encoding.UTF8)); - context.AddSource(CloneAttributeString, SourceText.From(clonePropertyAttributeText, Encoding.UTF8)); - context.AddSource(IgnoreCloneAttributeString, SourceText.From(ignoreClonePropertyAttributeText, Encoding.UTF8)); + context.AddSource(CLONEABLE_ATTRIBUTE_STRING, SourceText.From(CLONEABLE_ATTRIBUTE_TEXT, Encoding.UTF8)); + context.AddSource(CLONE_ATTRIBUTE_STRING, SourceText.From(CLONE_PROPERTY_ATTRIBUTE_TEXT, Encoding.UTF8)); + context.AddSource(IGNORE_CLONE_ATTRIBUTE_STRING, SourceText.From(IGNORE_CLONE_PROPERTY_ATTRIBUTE_TEXT, Encoding.UTF8)); } } } \ No newline at end of file diff --git a/src/NadekoBot.Generators/Cloneable/SymbolExtensions.cs b/src/NadekoBot.Generators/Cloneable/SymbolExtensions.cs index 4ae97bcf5..6c0c6055e 100644 --- a/src/NadekoBot.Generators/Cloneable/SymbolExtensions.cs +++ b/src/NadekoBot.Generators/Cloneable/SymbolExtensions.cs @@ -18,9 +18,7 @@ namespace Cloneable } public static bool HasAttribute(this ISymbol symbol, INamedTypeSymbol attributeType) - { - return symbol.GetAttributes() + => symbol.GetAttributes() .Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, attributeType)); - } } } \ No newline at end of file diff --git a/src/NadekoBot.Generators/Command/CommandAttributesGenerator.cs b/src/NadekoBot.Generators/Command/CommandAttributesGenerator.cs index ad0141687..c8b6ac8be 100644 --- a/src/NadekoBot.Generators/Command/CommandAttributesGenerator.cs +++ b/src/NadekoBot.Generators/Command/CommandAttributesGenerator.cs @@ -76,14 +76,14 @@ public class CmdAttribute : System.Attribute var compilationMethods = context.CompilationProvider.Combine(methods.Collect()); context.RegisterSourceOutput(compilationMethods, - static (ctx, tuple) => RegisterAction(in ctx, tuple.Left, in tuple.Right!)); + static (ctx, tuple) => RegisterAction(in ctx, tuple.Left, in tuple.Right)); } private static void RegisterAction(in SourceProductionContext ctx, Compilation comp, - in ImmutableArray methods) + in ImmutableArray methods) { - if (methods.IsDefaultOrEmpty) + if (methods is { IsDefaultOrEmpty: true }) return; var models = GetModels(comp, methods, ctx.CancellationToken); @@ -136,12 +136,17 @@ public class CmdAttribute : System.Attribute } private static IReadOnlyCollection GetModels(Compilation compilation, - in ImmutableArray methods, + in ImmutableArray inputMethods, CancellationToken cancel) { var models = new List(); - var methodModels = methods.Select(x => MethodDeclarationToMethodModel(compilation, x)); + var methods = inputMethods + .Where(static x => x is not null) + .Distinct(); + + var methodModels = methods + .Select(x => MethodDeclarationToMethodModel(compilation, x!)); var groups = methodModels .GroupBy(static x => $"{x.Namespace}.{string.Join(".", x.Classes)}"); @@ -172,6 +177,7 @@ public class CmdAttribute : System.Attribute var semanticModel = comp.GetSemanticModel(decl.SyntaxTree); var methodModel = new MethodModel( @params: decl.ParameterList.Parameters + .Where(p => p.Type is not null) .Select(p => { var prefix = p.Modifiers.Any(static x => x.IsKind(SyntaxKind.ParamsKeyword)) @@ -180,8 +186,8 @@ public class CmdAttribute : System.Attribute var type = semanticModel .GetTypeInfo(p.Type!) - .Type! - .ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); + .Type + ?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); var name = p.Identifier.Text; diff --git a/src/NadekoBot.Tests/BotStringsTests.cs b/src/NadekoBot.Tests/BotStringsTests.cs index 7935c2abf..dc86a9cb4 100644 --- a/src/NadekoBot.Tests/BotStringsTests.cs +++ b/src/NadekoBot.Tests/BotStringsTests.cs @@ -40,7 +40,7 @@ namespace NadekoBot.Tests } private static string[] GetCommandMethodNames() - => typeof(NadekoBot.Bot).Assembly + => typeof(Bot).Assembly .GetExportedTypes() .Where(type => type.IsClass && !type.IsAbstract) .Where(type => typeof(NadekoModule).IsAssignableFrom(type) // if its a top level module diff --git a/src/NadekoBot.Tests/GroupGreetTests.cs b/src/NadekoBot.Tests/GroupGreetTests.cs index e58b4f6b7..27a5ae697 100644 --- a/src/NadekoBot.Tests/GroupGreetTests.cs +++ b/src/NadekoBot.Tests/GroupGreetTests.cs @@ -1,5 +1,3 @@ -using System.Collections; -using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using NadekoBot.Extensions; @@ -14,9 +12,7 @@ namespace NadekoBot.Tests [SetUp] public void Setup() - { - _grouper = new GreetGrouper(); - } + => _grouper = new GreetGrouper(); [Test] public void CreateTest() diff --git a/src/NadekoBot.Tests/IndexedCollectionTests.cs b/src/NadekoBot.Tests/IndexedCollectionTests.cs index eabe60332..e206f866c 100644 --- a/src/NadekoBot.Tests/IndexedCollectionTests.cs +++ b/src/NadekoBot.Tests/IndexedCollectionTests.cs @@ -115,7 +115,7 @@ namespace NadekoBot.Tests [Test] public void ContainsTest() { - var subCol = new ShopEntry[] + var subCol = new[] { new ShopEntry() { Id = 111 }, new ShopEntry() { Id = 222 }, diff --git a/src/NadekoBot.Tests/KwumTests.cs b/src/NadekoBot.Tests/KwumTests.cs index 2ffbf874d..351548786 100644 --- a/src/NadekoBot.Tests/KwumTests.cs +++ b/src/NadekoBot.Tests/KwumTests.cs @@ -1,5 +1,4 @@ -using System.Linq; -using NadekoBot.Common; +using NadekoBot.Common; using NUnit.Framework; namespace NadekoBot.Tests diff --git a/src/NadekoBot.Tests/Random.cs b/src/NadekoBot.Tests/Random.cs index 91238910d..93c329d8c 100644 --- a/src/NadekoBot.Tests/Random.cs +++ b/src/NadekoBot.Tests/Random.cs @@ -9,10 +9,8 @@ namespace NadekoBot.Tests { [SetUp] public void Setup() - { - Console.OutputEncoding = Encoding.UTF8; - } - + => Console.OutputEncoding = Encoding.UTF8; + [Test] public void Utf8CodepointsToEmoji() { diff --git a/src/NadekoBot.VotesApi/Common/AuthHandler.cs b/src/NadekoBot.VotesApi/Common/AuthHandler.cs index ee170ef50..9c52e9f2d 100644 --- a/src/NadekoBot.VotesApi/Common/AuthHandler.cs +++ b/src/NadekoBot.VotesApi/Common/AuthHandler.cs @@ -6,7 +6,6 @@ using Microsoft.AspNetCore.Authentication; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; -using NadekoBot.VotesApi.Controllers; namespace NadekoBot.VotesApi { @@ -24,9 +23,7 @@ namespace NadekoBot.VotesApi ISystemClock clock, IConfiguration conf) : base(options, logger, encoder, clock) - { - _conf = conf; - } + => _conf = conf; protected override Task HandleAuthenticateAsync() { diff --git a/src/NadekoBot.VotesApi/Common/TopggVoteWebhookModel.cs b/src/NadekoBot.VotesApi/Common/TopggVoteWebhookModel.cs index ba2332186..6ca501e91 100644 --- a/src/NadekoBot.VotesApi/Common/TopggVoteWebhookModel.cs +++ b/src/NadekoBot.VotesApi/Common/TopggVoteWebhookModel.cs @@ -23,7 +23,7 @@ public bool Weekend { get; set; } /// - /// Query string params found on the /bot/:ID/vote page. Example: ?a=1&b=2. + /// Query string params found on the /bot/:ID/vote page. Example: ?a=1&b=2. /// public string Query { get; set; } } diff --git a/src/NadekoBot.VotesApi/Controllers/DiscordsController.cs b/src/NadekoBot.VotesApi/Controllers/DiscordsController.cs index d425a72ce..0854fe3a2 100644 --- a/src/NadekoBot.VotesApi/Controllers/DiscordsController.cs +++ b/src/NadekoBot.VotesApi/Controllers/DiscordsController.cs @@ -11,10 +11,10 @@ namespace NadekoBot.VotesApi.Controllers [Route("[controller]")] public class DiscordsController : ControllerBase { - private readonly ILogger _logger; + private readonly ILogger _logger; private readonly IVotesCache _cache; - public DiscordsController(ILogger logger, IVotesCache cache) + public DiscordsController(ILogger logger, IVotesCache cache) { _logger = logger; _cache = cache; @@ -26,7 +26,7 @@ namespace NadekoBot.VotesApi.Controllers { var votes = await _cache.GetNewDiscordsVotesAsync(); if(votes.Count > 0) - _logger.LogInformation("Sending {NewDiscordsVotes} new discords votes.", votes.Count); + _logger.LogInformation("Sending {NewDiscordsVotes} new discords votes", votes.Count); return votes; } } diff --git a/src/NadekoBot.VotesApi/Controllers/TopGgController.cs b/src/NadekoBot.VotesApi/Controllers/TopGgController.cs index 857c8474a..4626ff05e 100644 --- a/src/NadekoBot.VotesApi/Controllers/TopGgController.cs +++ b/src/NadekoBot.VotesApi/Controllers/TopGgController.cs @@ -26,7 +26,7 @@ namespace NadekoBot.VotesApi.Controllers { var votes = await _cache.GetNewTopGgVotesAsync(); if(votes.Count > 0) - _logger.LogInformation("Sending {NewTopggVotes} new topgg votes.", votes.Count); + _logger.LogInformation("Sending {NewTopggVotes} new topgg votes", votes.Count); return votes; } diff --git a/src/NadekoBot.VotesApi/Controllers/WebhookController.cs b/src/NadekoBot.VotesApi/Controllers/WebhookController.cs index fc759be31..afd4d814f 100644 --- a/src/NadekoBot.VotesApi/Controllers/WebhookController.cs +++ b/src/NadekoBot.VotesApi/Controllers/WebhookController.cs @@ -1,8 +1,6 @@ -using System; -using System.Threading.Tasks; +using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using NadekoBot.VotesApi.Services; @@ -13,13 +11,11 @@ namespace NadekoBot.VotesApi.Controllers { private readonly ILogger _logger; private readonly IVotesCache _votesCache; - private readonly IConfiguration _conf; - public WebhookController(ILogger logger, IVotesCache votesCache, IConfiguration conf) + public WebhookController(ILogger logger, IVotesCache votesCache) { _logger = logger; _votesCache = votesCache; - _conf = conf; } [HttpPost("/discordswebhook")] diff --git a/src/NadekoBot.VotesApi/Program.cs b/src/NadekoBot.VotesApi/Program.cs index e8eddd1a8..9e8e09e2d 100644 --- a/src/NadekoBot.VotesApi/Program.cs +++ b/src/NadekoBot.VotesApi/Program.cs @@ -1,23 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; +using NadekoBot.VotesApi; -namespace NadekoBot.VotesApi -{ - public class Program - { - public static void Main(string[] args) - { - CreateHostBuilder(args).Build().Run(); - } +CreateHostBuilder(args).Build().Run(); - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); }); - } -} \ No newline at end of file +static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); }); \ No newline at end of file diff --git a/src/NadekoBot.VotesApi/Services/FileVotesCache.cs b/src/NadekoBot.VotesApi/Services/FileVotesCache.cs index 8729d309f..ceb79804f 100644 --- a/src/NadekoBot.VotesApi/Services/FileVotesCache.cs +++ b/src/NadekoBot.VotesApi/Services/FileVotesCache.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.Json; @@ -10,37 +9,33 @@ namespace NadekoBot.VotesApi.Services { public class FileVotesCache : IVotesCache { - private const string statsFile = "store/stats.json"; - private const string topggFile = "store/topgg.json"; - private const string discordsFile = "store/discords.json"; + // private const string STATS_FILE = "store/stats.json"; + private const string TOPGG_FILE = "store/topgg.json"; + private const string DISCORDS_FILE = "store/discords.json"; - private readonly SemaphoreSlim locker = new SemaphoreSlim(1, 1); + private readonly SemaphoreSlim _locker = new SemaphoreSlim(1, 1); public FileVotesCache() { if (!Directory.Exists("store")) Directory.CreateDirectory("store"); - if(!File.Exists(topggFile)) - File.WriteAllText(topggFile, "[]"); + if(!File.Exists(TOPGG_FILE)) + File.WriteAllText(TOPGG_FILE, "[]"); - if(!File.Exists(discordsFile)) - File.WriteAllText(discordsFile, "[]"); + if(!File.Exists(DISCORDS_FILE)) + File.WriteAllText(DISCORDS_FILE, "[]"); } public ITask AddNewTopggVote(string userId) - { - return AddNewVote(topggFile, userId); - } - + => AddNewVote(TOPGG_FILE, userId); + public ITask AddNewDiscordsVote(string userId) - { - return AddNewVote(discordsFile, userId); - } + => AddNewVote(DISCORDS_FILE, userId); private async ITask AddNewVote(string file, string userId) { - await locker.WaitAsync(); + await _locker.WaitAsync(); try { var votes = await GetVotesAsync(file); @@ -49,7 +44,7 @@ namespace NadekoBot.VotesApi.Services } finally { - locker.Release(); + _locker.Release(); } } @@ -66,14 +61,14 @@ namespace NadekoBot.VotesApi.Services } private ITask> EvictTopggVotes() - => EvictVotes(topggFile); + => EvictVotes(TOPGG_FILE); private ITask> EvictDiscordsVotes() - => EvictVotes(discordsFile); + => EvictVotes(DISCORDS_FILE); private async ITask> EvictVotes(string file) { - await locker.WaitAsync(); + await _locker.WaitAsync(); try { @@ -91,7 +86,7 @@ namespace NadekoBot.VotesApi.Services } finally { - locker.Release(); + _locker.Release(); } } diff --git a/src/NadekoBot.VotesApi/Startup.cs b/src/NadekoBot.VotesApi/Startup.cs index 1d0320b33..3a669675f 100644 --- a/src/NadekoBot.VotesApi/Startup.cs +++ b/src/NadekoBot.VotesApi/Startup.cs @@ -11,19 +11,18 @@ namespace NadekoBot.VotesApi { public class Startup { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - public IConfiguration Configuration { get; } + + public Startup(IConfiguration configuration) + => Configuration = configuration; + // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddSingleton(); - services.AddSwaggerGen(c => + services.AddSwaggerGen(static c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "NadekoBot.VotesApi", Version = "v1" }); }); @@ -36,13 +35,13 @@ namespace NadekoBot.VotesApi }); services - .AddAuthorization(opts => + .AddAuthorization(static opts => { opts.DefaultPolicy = new AuthorizationPolicyBuilder(AuthHandler.SchemeName) - .RequireAssertion(x => false) + .RequireAssertion(static _ => false) .Build(); - opts.AddPolicy(Policies.DiscordsAuth, policy => policy.RequireClaim(AuthHandler.DiscordsClaim)); - opts.AddPolicy(Policies.TopggAuth, policy => policy.RequireClaim(AuthHandler.TopggClaim)); + opts.AddPolicy(Policies.DiscordsAuth, static policy => policy.RequireClaim(AuthHandler.DiscordsClaim)); + opts.AddPolicy(Policies.TopggAuth, static policy => policy.RequireClaim(AuthHandler.TopggClaim)); }); } @@ -53,7 +52,7 @@ namespace NadekoBot.VotesApi { app.UseDeveloperExceptionPage(); app.UseSwagger(); - app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "NadekoBot.VotesApi v1")); + app.UseSwaggerUI(static c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "NadekoBot.VotesApi v1")); } app.UseHttpsRedirection(); @@ -63,7 +62,7 @@ namespace NadekoBot.VotesApi app.UseAuthentication(); app.UseAuthorization(); - app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); + app.UseEndpoints(static endpoints => { endpoints.MapControllers(); }); } } } \ No newline at end of file diff --git a/src/NadekoBot.VotesApi/WeatherForecast.cs b/src/NadekoBot.VotesApi/WeatherForecast.cs index 20f8ffb14..20fe99319 100644 --- a/src/NadekoBot.VotesApi/WeatherForecast.cs +++ b/src/NadekoBot.VotesApi/WeatherForecast.cs @@ -1,5 +1,3 @@ -using System; - namespace NadekoBot.VotesApi { public class Vote diff --git a/src/NadekoBot/Bot.cs b/src/NadekoBot/Bot.cs index 3eb94b76e..99ac16c02 100644 --- a/src/NadekoBot/Bot.cs +++ b/src/NadekoBot/Bot.cs @@ -196,7 +196,7 @@ public sealed class Bot Task SetClientReady() { - var _ = Task.Run(async () => + _= Task.Run(async () => { clientReady.TrySetResult(true); try @@ -248,7 +248,7 @@ public sealed class Bot private Task Client_JoinedGuild(SocketGuild arg) { Log.Information("Joined server: {GuildName} [{GuildId}]", arg.Name, arg.Id); - var _ = Task.Run(async () => + _= Task.Run(async () => { GuildConfig gc; await using (var uow = _db.GetDbContext()) diff --git a/src/NadekoBot/Common/Collections/ConcurrentHashSet.cs b/src/NadekoBot/Common/Collections/ConcurrentHashSet.cs index 24c3f10b2..3b2f3261f 100644 --- a/src/NadekoBot/Common/Collections/ConcurrentHashSet.cs +++ b/src/NadekoBot/Common/Collections/ConcurrentHashSet.cs @@ -1,4 +1,5 @@ #nullable disable +#pragma warning disable all // License MIT // Source: https://github.com/i3arnon/ConcurrentHashSet @@ -17,8 +18,8 @@ namespace System.Collections.Generic; [DebuggerDisplay("Count = {Count}")] public sealed class ConcurrentHashSet : IReadOnlyCollection, ICollection { - private const int DefaultCapacity = 31; - private const int MaxLockNumber = 1024; + private const int DEFAULT_CAPACITY = 31; + private const int MAX_LOCK_NUMBER = 1024; private static int DefaultConcurrencyLevel => PlatformHelper.ProcessorCount; @@ -39,8 +40,8 @@ public sealed class ConcurrentHashSet : IReadOnlyCollection, ICollection : IReadOnlyCollection, ICollection : IReadOnlyCollection, ICollection _comparer; private readonly bool _growLockArray; - private int _budget; - private volatile Tables _tables; + private int budget; + private volatile Tables tables; /// /// Initializes a new instance of the @@ -108,7 +109,7 @@ public sealed class ConcurrentHashSet : IReadOnlyCollection, ICollection public ConcurrentHashSet() - : this(DefaultConcurrencyLevel, DefaultCapacity, true, EqualityComparer.Default) + : this(DefaultConcurrencyLevel, DEFAULT_CAPACITY, true, EqualityComparer.Default) { } @@ -175,7 +176,7 @@ public sealed class ConcurrentHashSet : IReadOnlyCollection, ICollection /// is a null reference. public ConcurrentHashSet(IEqualityComparer comparer) - : this(DefaultConcurrencyLevel, DefaultCapacity, true, comparer) + : this(DefaultConcurrencyLevel, DEFAULT_CAPACITY, true, comparer) { } @@ -241,7 +242,7 @@ public sealed class ConcurrentHashSet : IReadOnlyCollection, ICollection is less than 1. /// public ConcurrentHashSet(int concurrencyLevel, IEnumerable collection, IEqualityComparer comparer) - : this(concurrencyLevel, DefaultCapacity, false, comparer) + : this(concurrencyLevel, DEFAULT_CAPACITY, false, comparer) { if (collection is null) throw new ArgumentNullException(nameof(collection)); if (comparer is null) throw new ArgumentNullException(nameof(comparer)); @@ -296,10 +297,10 @@ public sealed class ConcurrentHashSet : IReadOnlyCollection, ICollection : IReadOnlyCollection, ICollection : IReadOnlyCollection, ICollection : IReadOnlyCollection, ICollection= 0; i++) count += _tables.CountPerLock[i]; + for (var i = 0; i < tables.Locks.Length && count >= 0; i++) count += tables.CountPerLock[i]; if (array.Length - count < arrayIndex || count < 0) //"count" itself or "count + arrayIndex" can overflow throw new ArgumentException( @@ -403,7 +404,7 @@ public sealed class ConcurrentHashSet : IReadOnlyCollection, ICollection public IEnumerator GetEnumerator() { - var buckets = _tables.Buckets; + var buckets = tables.Buckets; for (var i = 0; i < buckets.Length; i++) { @@ -443,7 +444,7 @@ public sealed class ConcurrentHashSet : IReadOnlyCollection, ICollection : IReadOnlyCollection, ICollection : IReadOnlyCollection, ICollection : IReadOnlyCollection, ICollection : IReadOnlyCollection, ICollection _budget) resizeDesired = true; + if (tables.CountPerLock[lockNo] > budget) resizeDesired = true; } finally { @@ -578,7 +579,7 @@ public sealed class ConcurrentHashSet : IReadOnlyCollection, ICollection : IReadOnlyCollection, ICollection : IReadOnlyCollection, ICollection : IReadOnlyCollection, ICollection : IReadOnlyCollection, ICollection : IReadOnlyCollection, ICollection : IReadOnlyCollection, ICollection : IList - where T : class, IIndexed + where T : class, IIndexed { public List Source { get; } @@ -53,6 +53,8 @@ public class IndexedCollection : IList public void Add(T item) { + ArgumentNullException.ThrowIfNull(item); + lock (_locker) { item.Index = Source.Count; diff --git a/src/NadekoBot/Common/ILogCommandService.cs b/src/NadekoBot/Common/ILogCommandService.cs index 0fd81e140..1a3dad459 100644 --- a/src/NadekoBot/Common/ILogCommandService.cs +++ b/src/NadekoBot/Common/ILogCommandService.cs @@ -26,6 +26,6 @@ public enum LogType ChannelUpdated, UserPresence, VoicePresence, - VoicePresenceTTS, + VoicePresenceTts, UserMuted } \ No newline at end of file diff --git a/src/NadekoBot/Common/NadekoModule.cs b/src/NadekoBot/Common/NadekoModule.cs index 7f9887aed..5a92960fd 100644 --- a/src/NadekoBot/Common/NadekoModule.cs +++ b/src/NadekoBot/Common/NadekoModule.cs @@ -86,7 +86,7 @@ public abstract class NadekoModule : ModuleBase } finally { - var _ = Task.Run(() => msg.DeleteAsync()); + _= Task.Run(() => msg.DeleteAsync()); } } @@ -111,7 +111,7 @@ public abstract class NadekoModule : ModuleBase Task MessageReceived(SocketMessage arg) { - var _ = Task.Run(() => + _= Task.Run(() => { if (arg is not SocketUserMessage userMsg || userMsg.Channel is not ITextChannel diff --git a/src/NadekoBot/Common/TypeReaders/Models/StoopidTime.cs b/src/NadekoBot/Common/TypeReaders/Models/StoopidTime.cs index fc9cbfab9..312fc8e0a 100644 --- a/src/NadekoBot/Common/TypeReaders/Models/StoopidTime.cs +++ b/src/NadekoBot/Common/TypeReaders/Models/StoopidTime.cs @@ -20,7 +20,6 @@ public class StoopidTime if (m.Length == 0) throw new ArgumentException("Invalid string input format."); - var output = string.Empty; var namesAndValues = new Dictionary(); foreach (var groupName in _regex.GetGroupNames()) @@ -35,7 +34,6 @@ public class StoopidTime if (value < 1) throw new ArgumentException($"Invalid {groupName} value."); namesAndValues[groupName] = value; - output += m.Groups[groupName].Value + " " + groupName + " "; } var ts = new TimeSpan((30 * namesAndValues["months"]) + (7 * namesAndValues["weeks"]) + namesAndValues["days"], diff --git a/src/NadekoBot/Common/Yml/CommentGatheringTypeInspector.cs b/src/NadekoBot/Common/Yml/CommentGatheringTypeInspector.cs index 15cf539d7..d64a736e2 100644 --- a/src/NadekoBot/Common/Yml/CommentGatheringTypeInspector.cs +++ b/src/NadekoBot/Common/Yml/CommentGatheringTypeInspector.cs @@ -7,59 +7,59 @@ namespace NadekoBot.Common.Yml; public class CommentGatheringTypeInspector : TypeInspectorSkeleton { - private readonly ITypeInspector innerTypeDescriptor; + private readonly ITypeInspector _innerTypeDescriptor; public CommentGatheringTypeInspector(ITypeInspector innerTypeDescriptor) - => this.innerTypeDescriptor = innerTypeDescriptor ?? throw new ArgumentNullException("innerTypeDescriptor"); + => this._innerTypeDescriptor = innerTypeDescriptor ?? throw new ArgumentNullException("innerTypeDescriptor"); public override IEnumerable GetProperties(Type type, object container) - => innerTypeDescriptor.GetProperties(type, container).Select(d => new CommentsPropertyDescriptor(d)); + => _innerTypeDescriptor.GetProperties(type, container).Select(d => new CommentsPropertyDescriptor(d)); private sealed class CommentsPropertyDescriptor : IPropertyDescriptor { public string Name { get; } public Type Type - => baseDescriptor.Type; + => _baseDescriptor.Type; public Type TypeOverride { - get => baseDescriptor.TypeOverride; - set => baseDescriptor.TypeOverride = value; + get => _baseDescriptor.TypeOverride; + set => _baseDescriptor.TypeOverride = value; } public int Order { get; set; } public ScalarStyle ScalarStyle { - get => baseDescriptor.ScalarStyle; - set => baseDescriptor.ScalarStyle = value; + get => _baseDescriptor.ScalarStyle; + set => _baseDescriptor.ScalarStyle = value; } public bool CanWrite - => baseDescriptor.CanWrite; + => _baseDescriptor.CanWrite; - private readonly IPropertyDescriptor baseDescriptor; + private readonly IPropertyDescriptor _baseDescriptor; public CommentsPropertyDescriptor(IPropertyDescriptor baseDescriptor) { - this.baseDescriptor = baseDescriptor; + this._baseDescriptor = baseDescriptor; Name = baseDescriptor.Name; } public void Write(object target, object value) - => baseDescriptor.Write(target, value); + => _baseDescriptor.Write(target, value); public T GetCustomAttribute() where T : Attribute - => baseDescriptor.GetCustomAttribute(); + => _baseDescriptor.GetCustomAttribute(); public IObjectDescriptor Read(object target) { - var comment = baseDescriptor.GetCustomAttribute(); + var comment = _baseDescriptor.GetCustomAttribute(); return comment is not null - ? new CommentsObjectDescriptor(baseDescriptor.Read(target), comment.Comment) - : baseDescriptor.Read(target); + ? new CommentsObjectDescriptor(_baseDescriptor.Read(target), comment.Comment) + : _baseDescriptor.Read(target); } } } \ No newline at end of file diff --git a/src/NadekoBot/Common/Yml/CommentsObjectDescriptor.cs b/src/NadekoBot/Common/Yml/CommentsObjectDescriptor.cs index 21c6270cf..1408f2243 100644 --- a/src/NadekoBot/Common/Yml/CommentsObjectDescriptor.cs +++ b/src/NadekoBot/Common/Yml/CommentsObjectDescriptor.cs @@ -9,22 +9,22 @@ public sealed class CommentsObjectDescriptor : IObjectDescriptor public string Comment { get; } public object Value - => innerDescriptor.Value; + => _innerDescriptor.Value; public Type Type - => innerDescriptor.Type; + => _innerDescriptor.Type; public Type StaticType - => innerDescriptor.StaticType; + => _innerDescriptor.StaticType; public ScalarStyle ScalarStyle - => innerDescriptor.ScalarStyle; + => _innerDescriptor.ScalarStyle; - private readonly IObjectDescriptor innerDescriptor; + private readonly IObjectDescriptor _innerDescriptor; public CommentsObjectDescriptor(IObjectDescriptor innerDescriptor, string comment) { - this.innerDescriptor = innerDescriptor; + this._innerDescriptor = innerDescriptor; Comment = comment; } } \ No newline at end of file diff --git a/src/NadekoBot/Db/Extensions/SelfAssignableRolesExtensions.cs b/src/NadekoBot/Db/Extensions/SelfAssignableRolesExtensions.cs index e13c4559e..08a89bfae 100644 --- a/src/NadekoBot/Db/Extensions/SelfAssignableRolesExtensions.cs +++ b/src/NadekoBot/Db/Extensions/SelfAssignableRolesExtensions.cs @@ -17,6 +17,6 @@ public static class SelfAssignableRolesExtensions return true; } - public static IEnumerable GetFromGuild(this DbSet roles, ulong guildId) + public static IReadOnlyCollection GetFromGuild(this DbSet roles, ulong guildId) => roles.AsQueryable().Where(s => s.GuildId == guildId).ToArray(); } \ No newline at end of file diff --git a/src/NadekoBot/Db/Models/GCChannelId.cs b/src/NadekoBot/Db/Models/GCChannelId.cs index b1a180258..bed2198b5 100644 --- a/src/NadekoBot/Db/Models/GCChannelId.cs +++ b/src/NadekoBot/Db/Models/GCChannelId.cs @@ -7,7 +7,7 @@ public class GCChannelId : DbEntity public ulong ChannelId { get; set; } public override bool Equals(object obj) - => obj is GCChannelId gc ? gc.ChannelId == ChannelId : false; + => obj is GCChannelId gc && gc.ChannelId == ChannelId; public override int GetHashCode() => ChannelId.GetHashCode(); diff --git a/src/NadekoBot/Modules/Administration/Administration.cs b/src/NadekoBot/Modules/Administration/Administration.cs index 6c8cc4463..79e7e736d 100644 --- a/src/NadekoBot/Modules/Administration/Administration.cs +++ b/src/NadekoBot/Modules/Administration/Administration.cs @@ -87,8 +87,8 @@ public partial class Administration : NadekoModule channels.Select(x => { var ch = guild.GetChannel(x.ChannelId)?.ToString() ?? x.ChannelId.ToString(); - var prefix = x.State ? "✅ " : "❌ "; - return prefix + ch; + var prefixSign = x.State ? "✅ " : "❌ "; + return prefixSign + ch; })); if (string.IsNullOrWhiteSpace(str)) @@ -319,7 +319,7 @@ public partial class Administration : NadekoModule } else if (time.Time <= TimeSpan.FromDays(7)) { - var _ = Task.Run(async () => + _= Task.Run(async () => { await Task.Delay(time.Time); await msg.DeleteAsync(); diff --git a/src/NadekoBot/Modules/Administration/AdministrationService.cs b/src/NadekoBot/Modules/Administration/AdministrationService.cs index deb74cd5e..365140900 100644 --- a/src/NadekoBot/Modules/Administration/AdministrationService.cs +++ b/src/NadekoBot/Modules/Administration/AdministrationService.cs @@ -41,7 +41,7 @@ public class AdministrationService : INService private Task DelMsgOnCmd_Handler(IUserMessage msg, CommandInfo cmd) { - var _ = Task.Run(async () => + _= Task.Run(async () => { if (msg.Channel is not SocketTextChannel channel) return; diff --git a/src/NadekoBot/Modules/Administration/DangerousCommands/DangerousCommands.cs b/src/NadekoBot/Modules/Administration/DangerousCommands/DangerousCommands.cs index c267778cf..3b60c7e2e 100644 --- a/src/NadekoBot/Modules/Administration/DangerousCommands/DangerousCommands.cs +++ b/src/NadekoBot/Modules/Administration/DangerousCommands/DangerousCommands.cs @@ -63,7 +63,7 @@ namespace NadekoBot.Modules.Administration [Cmd] [OwnerOnly] public partial Task DeleteWaifus() - => SqlExec(DangerousCommandsService.WaifusDeleteSql); + => SqlExec(DangerousCommandsService.WAIFUS_DELETE_SQL); [Cmd] [OwnerOnly] @@ -73,22 +73,22 @@ namespace NadekoBot.Modules.Administration [Cmd] [OwnerOnly] public partial Task DeleteWaifu(ulong userId) - => InternalExecSql(DangerousCommandsService.WaifuDeleteSql, userId); + => InternalExecSql(DangerousCommandsService.WAIFU_DELETE_SQL, userId); [Cmd] [OwnerOnly] public partial Task DeleteCurrency() - => SqlExec(DangerousCommandsService.CurrencyDeleteSql); + => SqlExec(DangerousCommandsService.CURRENCY_DELETE_SQL); [Cmd] [OwnerOnly] public partial Task DeletePlaylists() - => SqlExec(DangerousCommandsService.MusicPlaylistDeleteSql); + => SqlExec(DangerousCommandsService.MUSIC_PLAYLIST_DELETE_SQL); [Cmd] [OwnerOnly] public partial Task DeleteXp() - => SqlExec(DangerousCommandsService.XpDeleteSql); + => SqlExec(DangerousCommandsService.XP_DELETE_SQL); [Cmd] [OwnerOnly] diff --git a/src/NadekoBot/Modules/Administration/DangerousCommands/DangerousCommandsService.cs b/src/NadekoBot/Modules/Administration/DangerousCommands/DangerousCommandsService.cs index 6264852f2..61f9ba255 100644 --- a/src/NadekoBot/Modules/Administration/DangerousCommands/DangerousCommandsService.cs +++ b/src/NadekoBot/Modules/Administration/DangerousCommands/DangerousCommandsService.cs @@ -8,22 +8,22 @@ namespace NadekoBot.Modules.Administration.Services; public class DangerousCommandsService : INService { - public const string WaifusDeleteSql = @"DELETE FROM WaifuUpdates; + public const string WAIFUS_DELETE_SQL = @"DELETE FROM WaifuUpdates; DELETE FROM WaifuItem; DELETE FROM WaifuInfo;"; - public const string WaifuDeleteSql = + public const string WAIFU_DELETE_SQL = @"DELETE FROM WaifuUpdates WHERE UserId=(SELECT Id FROM DiscordUser WHERE UserId={0}); DELETE FROM WaifuItem WHERE WaifuInfoId=(SELECT Id FROM WaifuInfo WHERE WaifuId=(SELECT Id FROM DiscordUser WHERE UserId={0})); UPDATE WaifuInfo SET ClaimerId=NULL WHERE ClaimerId=(SELECT Id FROM DiscordUser WHERE UserId={0}); DELETE FROM WaifuInfo WHERE WaifuId=(SELECT Id FROM DiscordUser WHERE UserId={0});"; - public const string CurrencyDeleteSql = + public const string CURRENCY_DELETE_SQL = "UPDATE DiscordUser SET CurrencyAmount=0; DELETE FROM CurrencyTransactions; DELETE FROM PlantedCurrency;"; - public const string MusicPlaylistDeleteSql = "DELETE FROM MusicPlaylists;"; + public const string MUSIC_PLAYLIST_DELETE_SQL = "DELETE FROM MusicPlaylists;"; - public const string XpDeleteSql = @"DELETE FROM UserXpStats; + public const string XP_DELETE_SQL = @"DELETE FROM UserXpStats; UPDATE DiscordUser SET ClubId=NULL, IsClubAdmin=0, diff --git a/src/NadekoBot/Modules/Administration/GameVoiceChannel/GameVoiceChannelService.cs b/src/NadekoBot/Modules/Administration/GameVoiceChannel/GameVoiceChannelService.cs index 266032291..e270f8c0f 100644 --- a/src/NadekoBot/Modules/Administration/GameVoiceChannel/GameVoiceChannelService.cs +++ b/src/NadekoBot/Modules/Administration/GameVoiceChannel/GameVoiceChannelService.cs @@ -27,7 +27,7 @@ public class GameVoiceChannelService : INService private Task OnPresenceUpdate(SocketUser socketUser, SocketPresence before, SocketPresence after) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -83,7 +83,7 @@ public class GameVoiceChannelService : INService private Task OnUserVoiceStateUpdated(SocketUser usr, SocketVoiceState oldState, SocketVoiceState newState) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { diff --git a/src/NadekoBot/Modules/Administration/GreetBye/GreetService.cs b/src/NadekoBot/Modules/Administration/GreetBye/GreetService.cs index 6401676fb..5a35551ea 100644 --- a/src/NadekoBot/Modules/Administration/GreetBye/GreetService.cs +++ b/src/NadekoBot/Modules/Administration/GreetBye/GreetService.cs @@ -107,7 +107,7 @@ public class GreetService : INService, IReadyExecutor private Task OnUserLeft(SocketGuild guild, SocketUser user) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -271,7 +271,7 @@ public class GreetService : INService, IReadyExecutor private Task OnUserJoined(IGuildUser user) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { diff --git a/src/NadekoBot/Modules/Administration/ImageOnlyChannelService.cs b/src/NadekoBot/Modules/Administration/ImageOnlyChannelService.cs index 5acd2f6ec..919570a3d 100644 --- a/src/NadekoBot/Modules/Administration/ImageOnlyChannelService.cs +++ b/src/NadekoBot/Modules/Administration/ImageOnlyChannelService.cs @@ -31,7 +31,7 @@ public sealed class ImageOnlyChannelService : IEarlyBehavior var uow = _db.GetDbContext(); _enabledOn = uow.ImageOnlyChannels.ToList() .GroupBy(x => x.GuildId) - .ToDictionary(x => x.Key, x => new ConcurrentHashSet(x.Select(x => x.ChannelId))) + .ToDictionary(x => x.Key, x => new ConcurrentHashSet(x.Select(y => y.ChannelId))) .ToConcurrent(); _ = Task.Run(DeleteQueueRunner); diff --git a/src/NadekoBot/Modules/Administration/Mute/MuteService.cs b/src/NadekoBot/Modules/Administration/Mute/MuteService.cs index 716cee325..a520f1879 100644 --- a/src/NadekoBot/Modules/Administration/Mute/MuteService.cs +++ b/src/NadekoBot/Modules/Administration/Mute/MuteService.cs @@ -126,7 +126,7 @@ public class MuteService : INService if (string.IsNullOrWhiteSpace(reason)) return; - var _ = Task.Run(() => user.SendMessageAsync(embed: _eb.Create() + _= Task.Run(() => user.SendMessageAsync(embed: _eb.Create() .WithDescription( $"You've been muted in {user.Guild} server") .AddField("Mute Type", type.ToString()) @@ -144,7 +144,7 @@ public class MuteService : INService if (string.IsNullOrWhiteSpace(reason)) return; - var _ = Task.Run(() => user.SendMessageAsync(embed: _eb.Create() + _= Task.Run(() => user.SendMessageAsync(embed: _eb.Create() .WithDescription( $"You've been unmuted in {user.Guild} server") .AddField("Unmute Type", type.ToString()) @@ -161,7 +161,7 @@ public class MuteService : INService if (muted is null || !muted.Contains(usr.Id)) return Task.CompletedTask; - var _ = Task.Run(() => MuteUser(usr, _client.CurrentUser, reason: "Sticky mute")); + _= Task.Run(() => MuteUser(usr, _client.CurrentUser, reason: "Sticky mute")); } catch (Exception ex) { @@ -417,6 +417,9 @@ public class MuteService : INService { try { + if (roleId is null) + return; + RemoveTimerFromDb(guildId, userId, type); StopTimer(guildId, userId, type); var guild = _client.GetGuild(guildId); diff --git a/src/NadekoBot/Modules/Administration/Prefix/PrefixCommands.cs b/src/NadekoBot/Modules/Administration/Prefix/PrefixCommands.cs index 8e4355ee5..c1b49061f 100644 --- a/src/NadekoBot/Modules/Administration/Prefix/PrefixCommands.cs +++ b/src/NadekoBot/Modules/Administration/Prefix/PrefixCommands.cs @@ -20,36 +20,36 @@ public partial class Administration [RequireContext(ContextType.Guild)] [UserPerm(GuildPerm.Administrator)] [Priority(0)] - public partial Task Prefix(Set _, [Leftover] string prefix) - => Prefix(prefix); + public partial Task Prefix(Set _, [Leftover] string newPrefix) + => Prefix(newPrefix); [Cmd] [RequireContext(ContextType.Guild)] [UserPerm(GuildPerm.Administrator)] [Priority(0)] - public async partial Task Prefix([Leftover] string prefix) + public async partial Task Prefix([Leftover] string toSet) { if (string.IsNullOrWhiteSpace(prefix)) return; - var oldPrefix = base.prefix; - var newPrefix = CmdHandler.SetPrefix(ctx.Guild, prefix); + var oldPrefix = prefix; + var newPrefix = CmdHandler.SetPrefix(ctx.Guild, toSet); await ReplyConfirmLocalizedAsync(strs.prefix_new(Format.Code(oldPrefix), Format.Code(newPrefix))); } [Cmd] [OwnerOnly] - public async partial Task DefPrefix([Leftover] string prefix = null) + public async partial Task DefPrefix([Leftover] string toSet = null) { - if (string.IsNullOrWhiteSpace(prefix)) + if (string.IsNullOrWhiteSpace(toSet)) { await ReplyConfirmLocalizedAsync(strs.defprefix_current(CmdHandler.GetPrefix())); return; } var oldPrefix = CmdHandler.GetPrefix(); - var newPrefix = CmdHandler.SetDefaultPrefix(prefix); + var newPrefix = CmdHandler.SetDefaultPrefix(toSet); await ReplyConfirmLocalizedAsync(strs.defprefix_new(Format.Code(oldPrefix), Format.Code(newPrefix))); } diff --git a/src/NadekoBot/Modules/Administration/Protection/ProtectionService.cs b/src/NadekoBot/Modules/Administration/Protection/ProtectionService.cs index 6e2e60c31..7cac8182e 100644 --- a/src/NadekoBot/Modules/Administration/Protection/ProtectionService.cs +++ b/src/NadekoBot/Modules/Administration/Protection/ProtectionService.cs @@ -94,7 +94,7 @@ public class ProtectionService : INService private Task _client_LeftGuild(SocketGuild guild) { - var _ = Task.Run(async () => + _= Task.Run(async () => { TryStopAntiRaid(guild.Id); TryStopAntiSpam(guild.Id); @@ -202,7 +202,7 @@ public class ProtectionService : INService if (msg.Channel is not ITextChannel channel) return Task.CompletedTask; - var _ = Task.Run(async () => + _= Task.Run(async () => { try { diff --git a/src/NadekoBot/Modules/Administration/Protection/ProtectionStats.cs b/src/NadekoBot/Modules/Administration/Protection/ProtectionStats.cs index a9444a404..ce1ce310d 100644 --- a/src/NadekoBot/Modules/Administration/Protection/ProtectionStats.cs +++ b/src/NadekoBot/Modules/Administration/Protection/ProtectionStats.cs @@ -38,15 +38,15 @@ public class AntiAltStats => _setting.MinAge; public int Counter - => _counter; + => counter; private readonly AntiAltSetting _setting; - private int _counter; + private int counter; public AntiAltStats(AntiAltSetting setting) => _setting = setting; public void Increment() - => Interlocked.Increment(ref _counter); + => Interlocked.Increment(ref counter); } \ No newline at end of file diff --git a/src/NadekoBot/Modules/Administration/Prune/PruneCommands.cs b/src/NadekoBot/Modules/Administration/Prune/PruneCommands.cs index ba0732576..a72f10650 100644 --- a/src/NadekoBot/Modules/Administration/Prune/PruneCommands.cs +++ b/src/NadekoBot/Modules/Administration/Prune/PruneCommands.cs @@ -8,7 +8,7 @@ public partial class Administration [Group] public partial class PruneCommands : NadekoSubmodule { - private static readonly TimeSpan twoWeeks = TimeSpan.FromDays(14); + private static readonly TimeSpan _twoWeeks = TimeSpan.FromDays(14); //delets her own messages, no perm required [Cmd] @@ -73,11 +73,11 @@ public partial class Administration if (parameter is "-s" or "--safe") await _service.PruneWhere((ITextChannel)ctx.Channel, count, - m => m.Author.Id == userId && DateTime.UtcNow - m.CreatedAt < twoWeeks && !m.IsPinned); + m => m.Author.Id == userId && DateTime.UtcNow - m.CreatedAt < _twoWeeks && !m.IsPinned); else await _service.PruneWhere((ITextChannel)ctx.Channel, count, - m => m.Author.Id == userId && DateTime.UtcNow - m.CreatedAt < twoWeeks); + m => m.Author.Id == userId && DateTime.UtcNow - m.CreatedAt < _twoWeeks); } } } \ No newline at end of file diff --git a/src/NadekoBot/Modules/Administration/Role/RoleCommands.cs b/src/NadekoBot/Modules/Administration/Role/RoleCommands.cs index 7624e3842..74fe8ed2b 100644 --- a/src/NadekoBot/Modules/Administration/Role/RoleCommands.cs +++ b/src/NadekoBot/Modules/Administration/Role/RoleCommands.cs @@ -13,7 +13,7 @@ public partial class Administration { public enum Exclude { Excl } - private IServiceProvider _services; + private readonly IServiceProvider _services; public RoleCommands(IServiceProvider services) => _services = services; @@ -24,7 +24,7 @@ public partial class Administration ? await ctx.Channel.GetMessageAsync(msgId) : (await ctx.Channel.GetMessagesAsync(2).FlattenAsync()).Skip(1).FirstOrDefault(); - if (input.Length % 2 != 0) + if (input.Length % 2 != 0 || target is null) return; var all = await input.Chunk(input.Length / 2) @@ -35,7 +35,7 @@ public partial class Administration var roleResult = await roleReader.ReadAsync(ctx, inputRoleStr, _services); if (!roleResult.IsSuccess) { - Log.Warning("Role {0} not found.", inputRoleStr); + Log.Warning("Role {Role} not found", inputRoleStr); return null; } @@ -163,7 +163,6 @@ public partial class Administration if (index < 1 || !_service.Get(ctx.Guild.Id, out var rrs) || !rrs.Any() || rrs.Count < index) return; index--; - var rr = rrs[index]; _service.Remove(ctx.Guild.Id, index); await ReplyConfirmLocalizedAsync(strs.reaction_role_removed(index + 1)); } diff --git a/src/NadekoBot/Modules/Administration/Role/RoleCommandsService.cs b/src/NadekoBot/Modules/Administration/Role/RoleCommandsService.cs index 03fa037cf..597f7d8f0 100644 --- a/src/NadekoBot/Modules/Administration/Role/RoleCommandsService.cs +++ b/src/NadekoBot/Modules/Administration/Role/RoleCommandsService.cs @@ -90,7 +90,7 @@ public class RoleCommandsService : INService await dl.RemoveReactionAsync(reaction.Emote, dl.Author, new() { RetryMode = RetryMode.RetryRatelimit | RetryMode.Retry502 }); - Log.Warning("User {0} is adding unrelated reactions to the reaction roles message.", dl.Author); + Log.Warning("User {Author} is adding unrelated reactions to the reaction roles message", dl.Author); } }); diff --git a/src/NadekoBot/Modules/Administration/SelfAssignableRoles/SelfAssignedRolesCommands.cs b/src/NadekoBot/Modules/Administration/SelfAssignableRoles/SelfAssignedRolesCommands.cs index 52185f8fd..a42a3fc7d 100644 --- a/src/NadekoBot/Modules/Administration/SelfAssignableRoles/SelfAssignedRolesCommands.cs +++ b/src/NadekoBot/Modules/Administration/SelfAssignableRoles/SelfAssignedRolesCommands.cs @@ -58,8 +58,6 @@ public partial class Administration [Priority(0)] public async partial Task Sargn(int group, [Leftover] string name = null) { - var guser = (IGuildUser)ctx.User; - var set = await _service.SetNameAsync(ctx.Guild.Id, group, name); if (set) @@ -106,7 +104,7 @@ public partial class Administration foreach (var kvp in roleGroups) { - var groupNameText = string.Empty; + string groupNameText; if (!groups.TryGetValue(kvp.Key, out var name)) groupNameText = Format.Bold(GetText(strs.self_assign_group(kvp.Key))); else diff --git a/src/NadekoBot/Modules/Administration/SelfAssignableRoles/SelfAssignedRolesService.cs b/src/NadekoBot/Modules/Administration/SelfAssignableRoles/SelfAssignedRolesService.cs index daf3e9ed6..bd72cf3b4 100644 --- a/src/NadekoBot/Modules/Administration/SelfAssignableRoles/SelfAssignedRolesService.cs +++ b/src/NadekoBot/Modules/Administration/SelfAssignableRoles/SelfAssignedRolesService.cs @@ -158,7 +158,7 @@ public class SelfAssignedRolesService : INService return success; } - public (bool AutoDelete, bool Exclusive, IEnumerable) GetAdAndRoles(ulong guildId) + public (bool AutoDelete, bool Exclusive, IReadOnlyCollection) GetAdAndRoles(ulong guildId) { using var uow = _db.GetDbContext(); var gc = uow.GuildConfigsForId(guildId, set => set); @@ -198,12 +198,12 @@ public class SelfAssignedRolesService : INService return areExclusive; } - public (bool Exclusive, IEnumerable<(SelfAssignedRole Model, IRole Role)> Roles, IDictionary GroupNames + public (bool Exclusive, IReadOnlyCollection<(SelfAssignedRole Model, IRole Role)> Roles, IDictionary GroupNames ) GetRoles(IGuild guild) { var exclusive = false; - IEnumerable<(SelfAssignedRole Model, IRole Role)> roles; + IReadOnlyCollection<(SelfAssignedRole Model, IRole Role)> roles; IDictionary groupNames; using (var uow = _db.GetDbContext()) { @@ -211,11 +211,12 @@ public class SelfAssignedRolesService : INService exclusive = gc.ExclusiveSelfAssignedRoles; groupNames = gc.SelfAssignableRoleGroupNames.ToDictionary(x => x.Number, x => x.Name); var roleModels = uow.SelfAssignableRoles.GetFromGuild(guild.Id); - roles = roleModels.Select(x => (Model: x, Role: guild.GetRole(x.RoleId))); + roles = roleModels.Select(x => (Model: x, Role: guild.GetRole(x.RoleId))) + .ToList(); uow.SelfAssignableRoles.RemoveRange(roles.Where(x => x.Role is null).Select(x => x.Model).ToArray()); uow.SaveChanges(); } - return (exclusive, roles.Where(x => x.Role is not null), groupNames); + return (exclusive, roles.Where(x => x.Role is not null).ToList(), groupNames); } } \ No newline at end of file diff --git a/src/NadekoBot/Modules/Administration/ServerLog/ServerLogCommandService.cs b/src/NadekoBot/Modules/Administration/ServerLog/ServerLogCommandService.cs index be0e40c1f..97c3494b4 100644 --- a/src/NadekoBot/Modules/Administration/ServerLog/ServerLogCommandService.cs +++ b/src/NadekoBot/Modules/Administration/ServerLog/ServerLogCommandService.cs @@ -183,7 +183,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor private Task _client_UserUpdated(SocketUser before, SocketUser uAfter) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -292,7 +292,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor case LogType.VoicePresence: channelId = logSetting.LogVoicePresenceId = logSetting.LogVoicePresenceId is null ? cid : default; break; - case LogType.VoicePresenceTTS: + case LogType.VoicePresenceTts: channelId = logSetting.LogVoicePresenceTTSId = logSetting.LogVoicePresenceTTSId is null ? cid : default; break; @@ -306,7 +306,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor private Task _client_UserVoiceStateUpdated_TTS(SocketUser iusr, SocketVoiceState before, SocketVoiceState after) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -324,7 +324,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor return; ITextChannel? logChannel; - if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.VoicePresenceTTS)) is null) + if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.VoicePresenceTts)) is null) return; var str = string.Empty; @@ -351,8 +351,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor IUser mod, MuteType muteType, string reason) - { - var _ = Task.Run(async () => + => _= Task.Run(async () => { try { @@ -391,15 +390,13 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor // ignored } }); - } private void MuteCommands_UserUnmuted( IGuildUser usr, IUser mod, MuteType muteType, string reason) - { - var _ = Task.Run(async () => + => _= Task.Run(async () => { try { @@ -443,11 +440,10 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor // ignored } }); - } public Task TriggeredAntiProtection(PunishmentAction action, ProtectionType protection, params IGuildUser[] users) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -517,7 +513,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor private Task _client_GuildUserUpdated(Cacheable optBefore, SocketGuildUser after) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -620,7 +616,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor private Task _client_ChannelUpdated(IChannel cbefore, IChannel cafter) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -668,7 +664,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor private Task _client_ChannelDestroyed(IChannel ich) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -706,7 +702,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor private Task _client_ChannelCreated(IChannel ich) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -742,7 +738,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor private Task _client_UserVoiceStateUpdated(SocketUser iusr, SocketVoiceState before, SocketVoiceState after) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -805,7 +801,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor private Task _client_UserLeft(SocketGuild guild, SocketUser usr) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -840,7 +836,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor private Task _client_UserJoined(IGuildUser usr) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -879,7 +875,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor private Task _client_UserUnbanned(IUser usr, IGuild guild) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -914,7 +910,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor private Task _client_UserBanned(IUser usr, IGuild guild) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -951,7 +947,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor private Task _client_MessageDeleted(Cacheable optMsg, Cacheable optCh) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -1005,7 +1001,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor SocketMessage imsg2, ISocketMessageChannel ch) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -1104,7 +1100,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor case LogType.VoicePresence: id = logSetting.LogVoicePresenceId; break; - case LogType.VoicePresenceTTS: + case LogType.VoicePresenceTts: id = logSetting.LogVoicePresenceTTSId; break; case LogType.UserMuted: @@ -1177,7 +1173,7 @@ public sealed class LogCommandService : ILogCommandService, IReadyExecutor case LogType.VoicePresence: newLogSetting.LogVoicePresenceId = null; break; - case LogType.VoicePresenceTTS: + case LogType.VoicePresenceTts: newLogSetting.LogVoicePresenceTTSId = null; break; } diff --git a/src/NadekoBot/Modules/Administration/ServerLog/ServerLogCommands.cs b/src/NadekoBot/Modules/Administration/ServerLog/ServerLogCommands.cs index ef4499ee8..d00a882e7 100644 --- a/src/NadekoBot/Modules/Administration/ServerLog/ServerLogCommands.cs +++ b/src/NadekoBot/Modules/Administration/ServerLog/ServerLogCommands.cs @@ -131,7 +131,7 @@ public partial class Administration return l.LogUserPresenceId; case LogType.VoicePresence: return l.LogVoicePresenceId; - case LogType.VoicePresenceTTS: + case LogType.VoicePresenceTts: return l.LogVoicePresenceTTSId; case LogType.UserMuted: return l.UserMutedId; diff --git a/src/NadekoBot/Modules/Administration/UserPunish/UserPunishService.cs b/src/NadekoBot/Modules/Administration/UserPunish/UserPunishService.cs index 8de88582d..706bb25f5 100644 --- a/src/NadekoBot/Modules/Administration/UserPunish/UserPunishService.cs +++ b/src/NadekoBot/Modules/Administration/UserPunish/UserPunishService.cs @@ -315,6 +315,9 @@ WHERE GuildId={guildId} if (number <= 0 || (time is not null && time.Time > TimeSpan.FromDays(49))) return false; + if (punish is PunishmentAction.AddRole && role is null) + return false; + using var uow = _db.GetDbContext(); var ps = uow.GuildConfigsForId(guildId, set => set.Include(x => x.WarnPunishments)).WarnPunishments; var toDelete = ps.Where(x => x.Count == number); @@ -326,7 +329,7 @@ WHERE GuildId={guildId} Count = number, Punishment = punish, Time = (int?)time?.Time.TotalMinutes ?? 0, - RoleId = punish == PunishmentAction.AddRole ? role.Id : default(ulong?) + RoleId = punish == PunishmentAction.AddRole ? role!.Id : default(ulong?) }); uow.SaveChanges(); return true; diff --git a/src/NadekoBot/Modules/Administration/VcRole/VcRoleService.cs b/src/NadekoBot/Modules/Administration/VcRole/VcRoleService.cs index 88afa7533..9404bef22 100644 --- a/src/NadekoBot/Modules/Administration/VcRole/VcRoleService.cs +++ b/src/NadekoBot/Modules/Administration/VcRole/VcRoleService.cs @@ -80,7 +80,7 @@ public class VcRoleService : INService using (var uow = _db.GetDbContext()) { var configWithVcRole = uow.GuildConfigsForId(arg.GuildId, set => set.Include(x => x.VcRoleInfos)); - var _ = InitializeVcRole(configWithVcRole); + _= InitializeVcRole(configWithVcRole); } return Task.CompletedTask; @@ -166,7 +166,7 @@ public class VcRoleService : INService var oldVc = oldState.VoiceChannel; var newVc = newState.VoiceChannel; - var _ = Task.Run(() => + _= Task.Run(() => { try { diff --git a/src/NadekoBot/Modules/CustomReactions/NadekoExpressionExtensions.cs b/src/NadekoBot/Modules/CustomReactions/NadekoExpressionExtensions.cs index be1273ccd..c4155e362 100644 --- a/src/NadekoBot/Modules/CustomReactions/NadekoExpressionExtensions.cs +++ b/src/NadekoBot/Modules/CustomReactions/NadekoExpressionExtensions.cs @@ -55,15 +55,15 @@ public static class NadekoExpressionExtensions if (wordIndex == 0) { - if (word.Length < str.Length && str.isValidWordDivider(word.Length)) + if (word.Length < str.Length && str.IsValidWordDivider(word.Length)) return WordPosition.Start; } else if (wordIndex + word.Length == str.Length) { - if (str.isValidWordDivider(wordIndex - 1)) + if (str.IsValidWordDivider(wordIndex - 1)) return WordPosition.End; } - else if (str.isValidWordDivider(wordIndex - 1) && str.isValidWordDivider(wordIndex + word.Length)) + else if (str.IsValidWordDivider(wordIndex - 1) && str.IsValidWordDivider(wordIndex + word.Length)) { return WordPosition.Middle; } @@ -71,7 +71,7 @@ public static class NadekoExpressionExtensions return WordPosition.None; } - private static bool isValidWordDivider(this in ReadOnlySpan str, int index) + private static bool IsValidWordDivider(this in ReadOnlySpan str, int index) { var ch = str[index]; if (ch is >= 'a' and <= 'z' or >= 'A' and <= 'Z' or >= '1' and <= '9') diff --git a/src/NadekoBot/Modules/Gambling/AnimalRacing/AnimalRace.cs b/src/NadekoBot/Modules/Gambling/AnimalRacing/AnimalRace.cs index 1e8ec58b6..d446f8c4a 100644 --- a/src/NadekoBot/Modules/Gambling/AnimalRacing/AnimalRace.cs +++ b/src/NadekoBot/Modules/Gambling/AnimalRacing/AnimalRace.cs @@ -44,8 +44,7 @@ public sealed class AnimalRace : IDisposable } public void Initialize() //lame name - { - var _t = Task.Run(async () => + => _ = Task.Run(async () => { await Task.Delay(_options.StartTime * 1000); @@ -59,7 +58,6 @@ public sealed class AnimalRace : IDisposable } finally { _locker.Release(); } }); - } public async Task JoinRace(ulong userId, string userName, long bet = 0) { @@ -104,13 +102,13 @@ public sealed class AnimalRace : IDisposable if (user.Bet > 0) await _currency.AddAsync(user.UserId, "Race refund", user.Bet); - var _sf = OnStartingFailed?.Invoke(this); + _ = OnStartingFailed?.Invoke(this); CurrentPhase = Phase.Ended; return; } - var _ = OnStarted?.Invoke(this); - var _t = Task.Run(async () => + _ = OnStarted?.Invoke(this); + _ = Task.Run(async () => { var rng = new NadekoRandom(); while (!_users.All(x => x.Progress >= 60)) @@ -126,7 +124,7 @@ public sealed class AnimalRace : IDisposable FinishedUsers.AddRange(finished); - var _ignore = OnStateUpdate?.Invoke(this); + _ = OnStateUpdate?.Invoke(this); await Task.Delay(2500); } @@ -135,7 +133,7 @@ public sealed class AnimalRace : IDisposable "Won a Race", FinishedUsers[0].Bet * (_users.Count - 1)); - var _ended = OnEnded?.Invoke(this); + _ = OnEnded?.Invoke(this); }); } diff --git a/src/NadekoBot/Modules/Gambling/AnimalRacing/AnimalRacingCommands.cs b/src/NadekoBot/Modules/Gambling/AnimalRacing/AnimalRacingCommands.cs index cd84ccce4..f0b53cc76 100644 --- a/src/NadekoBot/Modules/Gambling/AnimalRacing/AnimalRacingCommands.cs +++ b/src/NadekoBot/Modules/Gambling/AnimalRacing/AnimalRacingCommands.cs @@ -48,7 +48,7 @@ public partial class Gambling Task _client_MessageReceived(SocketMessage arg) { - var _ = Task.Run(() => + _= Task.Run(() => { try { diff --git a/src/NadekoBot/Modules/Gambling/BlackJack/BlackJackCommands.cs b/src/NadekoBot/Modules/Gambling/BlackJack/BlackJackCommands.cs index 6ed9caff9..50fe14d6b 100644 --- a/src/NadekoBot/Modules/Gambling/BlackJack/BlackJackCommands.cs +++ b/src/NadekoBot/Modules/Gambling/BlackJack/BlackJackCommands.cs @@ -18,7 +18,7 @@ public partial class Gambling private readonly ICurrencyService _cs; private readonly DbService _db; - private IUserMessage _msg; + private IUserMessage msg; public BlackJackCommands(ICurrencyService cs, DbService db, GamblingConfigService gamblingConf) : base(gamblingConf) @@ -74,12 +74,13 @@ public partial class Gambling { try { - if (_msg is not null) + if (msg is not null) { - var _ = _msg.DeleteAsync(); + _= msg.DeleteAsync(); } - var c = bj.Dealer.Cards.Select(x => x.GetEmojiString()); + var c = bj.Dealer.Cards.Select(x => x.GetEmojiString()) + .ToList(); var dealerIcon = "❔ "; if (bj.State == Blackjack.GameState.Ended) { @@ -102,7 +103,7 @@ public partial class Gambling foreach (var p in bj.Players) { - c = p.Cards.Select(x => x.GetEmojiString()); + c = p.Cards.Select(x => x.GetEmojiString()).ToList(); cStr = "-\t" + string.Concat(c.Select(x => x[..^1] + " ")); cStr += "\n-\t" + string.Concat(c.Select(x => x.Last() + " ")); var full = $"{p.DiscordUser.ToString().TrimTo(20)} | Bet: {p.Bet} | Value: {p.GetHandValue()}"; @@ -133,7 +134,7 @@ public partial class Gambling embed.AddField(full, cStr); } - _msg = await ctx.Channel.EmbedAsync(embed); + msg = await ctx.Channel.EmbedAsync(embed); } catch { @@ -146,7 +147,7 @@ public partial class Gambling ? Format.Strikethrough(x.DiscordUser.ToString().TrimTo(30)) : x.DiscordUser.ToString(); - var hand = $"{string.Concat(x.Cards.Select(y => "〖" + y.GetEmojiString() + "〗"))}"; + // var hand = $"{string.Concat(x.Cards.Select(y => "〖" + y.GetEmojiString() + "〗"))}"; return $"{playerName} | Bet: {x.Bet}\n"; diff --git a/src/NadekoBot/Modules/Gambling/BlackJack/Blackjack.cs b/src/NadekoBot/Modules/Gambling/BlackJack/Blackjack.cs index 176ce8703..b7482e1c2 100644 --- a/src/NadekoBot/Modules/Gambling/BlackJack/Blackjack.cs +++ b/src/NadekoBot/Modules/Gambling/BlackJack/Blackjack.cs @@ -35,9 +35,7 @@ public class Blackjack } public void Start() - { - var _ = GameLoop(); - } + => _= GameLoop(); public async Task GameLoop() { @@ -89,13 +87,13 @@ public class Blackjack Log.Information("Dealer moves"); await DealerMoves(); await PrintState(); - var _ = GameEnded?.Invoke(this); + _= GameEnded?.Invoke(this); } catch (Exception ex) { Log.Error(ex, "REPORT THE MESSAGE BELOW IN #NadekoLog SERVER PLEASE"); State = GameState.Ended; - var _ = GameEnded?.Invoke(this); + _= GameEnded?.Invoke(this); } } @@ -127,7 +125,7 @@ public class Blackjack if (!await _cs.RemoveAsync(user, "BlackJack-gamble", bet, gamble: true)) return false; Players.Add(new(user, bet)); - var _ = PrintState(); + _= PrintState(); return true; } finally diff --git a/src/NadekoBot/Modules/Gambling/Connect4/Connect4.cs b/src/NadekoBot/Modules/Gambling/Connect4/Connect4.cs index c4cee9786..06d84917c 100644 --- a/src/NadekoBot/Modules/Gambling/Connect4/Connect4.cs +++ b/src/NadekoBot/Modules/Gambling/Connect4/Connect4.cs @@ -28,8 +28,8 @@ public sealed class Connect4Game : IDisposable OtherPlayerWon } - public const int NumberOfColumns = 7; - public const int NumberOfRows = 6; + public const int NUMBER_OF_COLUMNS = 7; + public const int NUMBER_OF_ROWS = 6; //public event Func OnGameStarted; public event Func OnGameStateUpdated; @@ -51,7 +51,7 @@ public sealed class Connect4Game : IDisposable => CurrentPhase == Phase.P2Move ? _players[0].Value : _players[1].Value; //state is bottom to top, left to right - private readonly Field[] _gameState = new Field[NumberOfRows * NumberOfColumns]; + private readonly Field[] _gameState = new Field[NUMBER_OF_ROWS * NUMBER_OF_COLUMNS]; private readonly (ulong UserId, string Username)?[] _players = new (ulong, string)?[2]; private readonly SemaphoreSlim _locker = new(1, 1); @@ -81,14 +81,14 @@ public sealed class Connect4Game : IDisposable _cs = cs; _rng = new(); - for (var i = 0; i < NumberOfColumns * NumberOfRows; i++) _gameState[i] = Field.Empty; + for (var i = 0; i < NUMBER_OF_COLUMNS * NUMBER_OF_ROWS; i++) _gameState[i] = Field.Empty; } public void Initialize() { if (CurrentPhase != Phase.Joining) return; - var _ = Task.Run(async () => + _= Task.Run(async () => { await Task.Delay(15000); await _locker.WaitAsync(); @@ -165,14 +165,14 @@ public sealed class Connect4Game : IDisposable || (_players[1].Value.UserId == userId && CurrentPhase == Phase.P2Move))) return false; - if (inputCol is < 0 or > NumberOfColumns) //invalid input + if (inputCol is < 0 or > NUMBER_OF_COLUMNS) //invalid input return false; if (IsColumnFull(inputCol)) //can't play there event? return false; - var start = NumberOfRows * inputCol; - for (var i = start; i < start + NumberOfRows; i++) + var start = NUMBER_OF_ROWS * inputCol; + for (var i = start; i < start + NUMBER_OF_ROWS; i++) if (_gameState[i] == Field.Empty) { _gameState[i] = GetPlayerPiece(userId); @@ -182,21 +182,21 @@ public sealed class Connect4Game : IDisposable //check winnning condition // ok, i'll go from [0-2] in rows (and through all columns) and check upward if 4 are connected - for (var i = 0; i < NumberOfRows - 3; i++) + for (var i = 0; i < NUMBER_OF_ROWS - 3; i++) { if (CurrentPhase == Phase.Ended) break; - for (var j = 0; j < NumberOfColumns; j++) + for (var j = 0; j < NUMBER_OF_COLUMNS; j++) { if (CurrentPhase == Phase.Ended) break; - var first = _gameState[i + (j * NumberOfRows)]; + var first = _gameState[i + (j * NUMBER_OF_ROWS)]; if (first != Field.Empty) for (var k = 1; k < 4; k++) { - var next = _gameState[i + k + (j * NumberOfRows)]; + var next = _gameState[i + k + (j * NUMBER_OF_ROWS)]; if (next == first) { if (k == 3) @@ -213,21 +213,21 @@ public sealed class Connect4Game : IDisposable } // i'll go [0-1] in columns (and through all rows) and check to the right if 4 are connected - for (var i = 0; i < NumberOfColumns - 3; i++) + for (var i = 0; i < NUMBER_OF_COLUMNS - 3; i++) { if (CurrentPhase == Phase.Ended) break; - for (var j = 0; j < NumberOfRows; j++) + for (var j = 0; j < NUMBER_OF_ROWS; j++) { if (CurrentPhase == Phase.Ended) break; - var first = _gameState[j + (i * NumberOfRows)]; + var first = _gameState[j + (i * NUMBER_OF_ROWS)]; if (first != Field.Empty) for (var k = 1; k < 4; k++) { - var next = _gameState[j + ((i + k) * NumberOfRows)]; + var next = _gameState[j + ((i + k) * NUMBER_OF_ROWS)]; if (next == first) if (k == 3) EndGame(Result.CurrentPlayerWon, CurrentPlayer.UserId); @@ -239,17 +239,17 @@ public sealed class Connect4Game : IDisposable } //need to check diagonal now - for (var col = 0; col < NumberOfColumns; col++) + for (var col = 0; col < NUMBER_OF_COLUMNS; col++) { if (CurrentPhase == Phase.Ended) break; - for (var row = 0; row < NumberOfRows; row++) + for (var row = 0; row < NUMBER_OF_ROWS; row++) { if (CurrentPhase == Phase.Ended) break; - var first = _gameState[row + (col * NumberOfRows)]; + var first = _gameState[row + (col * NUMBER_OF_ROWS)]; if (first != Field.Empty) { @@ -263,12 +263,12 @@ public sealed class Connect4Game : IDisposable var curCol = col - i; //check if current values are in range - if (curRow is >= NumberOfRows or < 0) + if (curRow is >= NUMBER_OF_ROWS or < 0) break; - if (curCol is < 0 or >= NumberOfColumns) + if (curCol is < 0 or >= NUMBER_OF_COLUMNS) break; - var cur = _gameState[curRow + (curCol * NumberOfRows)]; + var cur = _gameState[curRow + (curCol * NUMBER_OF_ROWS)]; if (cur == first) same++; else break; @@ -290,12 +290,12 @@ public sealed class Connect4Game : IDisposable var curCol = col + i; //check if current values are in range - if (curRow is >= NumberOfRows or < 0) + if (curRow is >= NUMBER_OF_ROWS or < 0) break; - if (curCol is < 0 or >= NumberOfColumns) + if (curCol is < 0 or >= NUMBER_OF_COLUMNS) break; - var cur = _gameState[curRow + (curCol * NumberOfRows)]; + var cur = _gameState[curRow + (curCol * NUMBER_OF_ROWS)]; if (cur == first) same++; else break; @@ -323,7 +323,7 @@ public sealed class Connect4Game : IDisposable ResetTimer(); } - var _ = OnGameStateUpdated?.Invoke(this); + _= OnGameStateUpdated?.Invoke(this); return true; } finally { _locker.Release(); } @@ -337,7 +337,7 @@ public sealed class Connect4Game : IDisposable { if (CurrentPhase == Phase.Ended) return; - var _ = OnGameEnded?.Invoke(this, result); + _= OnGameEnded?.Invoke(this, result); CurrentPhase = Phase.Ended; if (result == Result.Draw) @@ -357,8 +357,8 @@ public sealed class Connect4Game : IDisposable //column is full if there are no empty fields private bool IsColumnFull(int column) { - var start = NumberOfRows * column; - for (var i = start; i < start + NumberOfRows; i++) + var start = NUMBER_OF_ROWS * column; + for (var i = start; i < start + NUMBER_OF_ROWS; i++) if (_gameState[i] == Field.Empty) return false; return true; diff --git a/src/NadekoBot/Modules/Gambling/Connect4/Connect4Commands.cs b/src/NadekoBot/Modules/Gambling/Connect4/Connect4Commands.cs index d0ba6990e..5a0ebcb04 100644 --- a/src/NadekoBot/Modules/Gambling/Connect4/Connect4Commands.cs +++ b/src/NadekoBot/Modules/Gambling/Connect4/Connect4Commands.cs @@ -88,7 +88,7 @@ public partial class Gambling if (ctx.Channel.Id != arg.Channel.Id) return Task.CompletedTask; - var _ = Task.Run(async () => + _= Task.Run(async () => { var success = false; if (int.TryParse(arg.Content, out var col)) success = await game.Input(arg.Author.Id, col); @@ -168,11 +168,11 @@ public partial class Gambling if (game.CurrentPhase is Connect4Game.Phase.P1Move or Connect4Game.Phase.P2Move) sb.AppendLine(GetText(strs.connect4_player_to_move(Format.Bold(game.CurrentPlayer.Username)))); - for (var i = Connect4Game.NumberOfRows; i > 0; i--) + for (var i = Connect4Game.NUMBER_OF_ROWS; i > 0; i--) { - for (var j = 0; j < Connect4Game.NumberOfColumns; j++) + for (var j = 0; j < Connect4Game.NUMBER_OF_COLUMNS; j++) { - var cur = game.GameState[i + (j * Connect4Game.NumberOfRows) - 1]; + var cur = game.GameState[i + (j * Connect4Game.NUMBER_OF_ROWS) - 1]; if (cur == Connect4Game.Field.Empty) sb.Append("⚫"); //black circle @@ -185,7 +185,7 @@ public partial class Gambling sb.AppendLine(); } - for (var i = 0; i < Connect4Game.NumberOfColumns; i++) sb.Append(numbers[i]); + for (var i = 0; i < Connect4Game.NUMBER_OF_COLUMNS; i++) sb.Append(numbers[i]); return sb.ToString(); } } diff --git a/src/NadekoBot/Modules/Gambling/Draw/DrawCommands.cs b/src/NadekoBot/Modules/Gambling/Draw/DrawCommands.cs index 166229e80..9f49dee14 100644 --- a/src/NadekoBot/Modules/Gambling/Draw/DrawCommands.cs +++ b/src/NadekoBot/Modules/Gambling/Draw/DrawCommands.cs @@ -68,10 +68,10 @@ public partial class Gambling if (num > 10) num = 10; - var (ImageStream, ToSend) = await InternalDraw(num, ctx.Guild.Id); - await using (ImageStream) + var (imageStream, toSend) = await InternalDraw(num, ctx.Guild.Id); + await using (imageStream) { - await ctx.Channel.SendFileAsync(ImageStream, num + " cards.jpg", ToSend); + await ctx.Channel.SendFileAsync(imageStream, num + " cards.jpg", toSend); } } @@ -83,10 +83,10 @@ public partial class Gambling if (num > 10) num = 10; - var (ImageStream, ToSend) = await InternalDraw(num); - await using (ImageStream) + var (imageStream, toSend) = await InternalDraw(num); + await using (imageStream) { - await ctx.Channel.SendFileAsync(ImageStream, num + " cards.jpg", ToSend); + await ctx.Channel.SendFileAsync(imageStream, num + " cards.jpg", toSend); } } diff --git a/src/NadekoBot/Modules/Gambling/Events/GameStatusEvent.cs b/src/NadekoBot/Modules/Gambling/Events/GameStatusEvent.cs index dbc1eb6b9..8323b7e85 100644 --- a/src/NadekoBot/Modules/Gambling/Events/GameStatusEvent.cs +++ b/src/NadekoBot/Modules/Gambling/Events/GameStatusEvent.cs @@ -11,7 +11,7 @@ public class GameStatusEvent : ICurrencyEvent public bool PotEmptied { get; private set; } private readonly DiscordSocketClient _client; private readonly IGuild _guild; - private IUserMessage _msg; + private IUserMessage msg; private readonly ICurrencyService _cs; private readonly long _amount; @@ -32,9 +32,9 @@ public class GameStatusEvent : ICurrencyEvent .Select(x => (char)x) .ToArray(); - private readonly object stopLock = new(); + private readonly object _stopLock = new(); - private readonly object potLock = new(); + private readonly object _potLock = new(); public GameStatusEvent( DiscordSocketClient client, @@ -62,9 +62,7 @@ public class GameStatusEvent : ICurrencyEvent } private void EventTimeout(object state) - { - var _ = StopEvent(); - } + => _ = StopEvent(); private async void OnTimerTick(object state) { @@ -83,20 +81,20 @@ public class GameStatusEvent : ICurrencyEvent true); if (_isPotLimited) - await _msg.ModifyAsync(m => + await msg.ModifyAsync(m => { m.Embed = GetEmbed(PotSize).Build(); }, new() { RetryMode = RetryMode.AlwaysRetry }); - Log.Information("Awarded {0} users {1} currency.{2}", + Log.Information("Awarded {Count} users {Amount} currency.{Remaining}", toAward.Count, _amount, _isPotLimited ? $" {PotSize} left." : ""); if (potEmpty) { - var _ = StopEvent(); + _= StopEvent(); } } catch (Exception ex) @@ -107,7 +105,7 @@ public class GameStatusEvent : ICurrencyEvent public async Task StartEvent() { - _msg = await _channel.EmbedAsync(GetEmbed(_opts.PotSize)); + msg = await _channel.EmbedAsync(GetEmbed(_opts.PotSize)); await _client.SetGameAsync(_code); _client.MessageDeleted += OnMessageDeleted; _client.MessageReceived += HandleMessage; @@ -117,55 +115,55 @@ public class GameStatusEvent : ICurrencyEvent private IEmbedBuilder GetEmbed(long pot) => _embedFunc(CurrencyEvent.Type.GameStatus, _opts, pot); - private async Task OnMessageDeleted(Cacheable msg, Cacheable cacheable) + private async Task OnMessageDeleted(Cacheable message, Cacheable cacheable) { - if (msg.Id == _msg.Id) await StopEvent(); + if (message.Id == this.msg.Id) await StopEvent(); } public async Task StopEvent() { await Task.Yield(); - lock (stopLock) + lock (_stopLock) { if (Stopped) return; Stopped = true; _client.MessageDeleted -= OnMessageDeleted; _client.MessageReceived -= HandleMessage; - _client.SetGameAsync(null); _t.Change(Timeout.Infinite, Timeout.Infinite); _timeout?.Change(Timeout.Infinite, Timeout.Infinite); + _ = _client.SetGameAsync(null); try { - var _ = _msg.DeleteAsync(); + _= msg.DeleteAsync(); } catch { } - var os = OnEnded(_guild.Id); + _ = OnEnded?.Invoke(_guild.Id); } } - private Task HandleMessage(SocketMessage msg) + private Task HandleMessage(SocketMessage message) { - var _ = Task.Run(async () => + _= Task.Run(async () => { - if (msg.Author is not IGuildUser gu // no unknown users, as they could be bots, or alts + if (message.Author is not IGuildUser gu // no unknown users, as they could be bots, or alts || gu.IsBot // no bots - || msg.Content != _code // code has to be the same + || message.Content != _code // code has to be the same || (DateTime.UtcNow - gu.CreatedAt).TotalDays <= 5) // no recently created accounts return; // there has to be money left in the pot // and the user wasn't rewarded - if (_awardedUsers.Add(msg.Author.Id) && TryTakeFromPot()) + if (_awardedUsers.Add(message.Author.Id) && TryTakeFromPot()) { - _toAward.Enqueue(msg.Author.Id); + _toAward.Enqueue(message.Author.Id); if (_isPotLimited && PotSize < _amount) PotEmptied = true; } try { - await msg.DeleteAsync(new() { RetryMode = RetryMode.AlwaysFail }); + await message.DeleteAsync(new() { RetryMode = RetryMode.AlwaysFail }); } catch { } }); @@ -175,7 +173,7 @@ public class GameStatusEvent : ICurrencyEvent private bool TryTakeFromPot() { if (_isPotLimited) - lock (potLock) + lock (_potLock) { if (PotSize < _amount) return false; diff --git a/src/NadekoBot/Modules/Gambling/Events/ReactionEvent.cs b/src/NadekoBot/Modules/Gambling/Events/ReactionEvent.cs index 37e38726e..b46ecd6fe 100644 --- a/src/NadekoBot/Modules/Gambling/Events/ReactionEvent.cs +++ b/src/NadekoBot/Modules/Gambling/Events/ReactionEvent.cs @@ -11,8 +11,8 @@ public class ReactionEvent : ICurrencyEvent public bool PotEmptied { get; private set; } private readonly DiscordSocketClient _client; private readonly IGuild _guild; - private IUserMessage _msg; - private IEmote _emote; + private IUserMessage msg; + private IEmote emote; private readonly ICurrencyService _cs; private readonly long _amount; @@ -27,9 +27,9 @@ public class ReactionEvent : ICurrencyEvent private readonly EventOptions _opts; private readonly GamblingConfig _config; - private readonly object stopLock = new(); + private readonly object _stopLock = new(); - private readonly object potLock = new(); + private readonly object _potLock = new(); public ReactionEvent( DiscordSocketClient client, @@ -58,9 +58,7 @@ public class ReactionEvent : ICurrencyEvent } private void EventTimeout(object state) - { - var _ = StopEvent(); - } + => _= StopEvent(); private async void OnTimerTick(object state) { @@ -76,20 +74,20 @@ public class ReactionEvent : ICurrencyEvent await _cs.AddBulkAsync(toAward, toAward.Select(_ => "Reaction Event"), toAward.Select(_ => _amount), true); if (_isPotLimited) - await _msg.ModifyAsync(m => + await msg.ModifyAsync(m => { m.Embed = GetEmbed(PotSize).Build(); }, new() { RetryMode = RetryMode.AlwaysRetry }); - Log.Information("Awarded {0} users {1} currency.{2}", + Log.Information("Awarded {Count} users {Amount} currency.{Remaining}", toAward.Count, _amount, _isPotLimited ? $" {PotSize} left." : ""); if (potEmpty) { - var _ = StopEvent(); + _= StopEvent(); } } catch (Exception ex) @@ -100,12 +98,12 @@ public class ReactionEvent : ICurrencyEvent public async Task StartEvent() { - if (Emote.TryParse(_config.Currency.Sign, out var emote)) - _emote = emote; + if (Emote.TryParse(_config.Currency.Sign, out var parsedEmote)) + this.emote = parsedEmote; else - _emote = new Emoji(_config.Currency.Sign); - _msg = await _channel.EmbedAsync(GetEmbed(_opts.PotSize)); - await _msg.AddReactionAsync(_emote); + this.emote = new Emoji(_config.Currency.Sign); + msg = await _channel.EmbedAsync(GetEmbed(_opts.PotSize)); + await msg.AddReactionAsync(this.emote); _client.MessageDeleted += OnMessageDeleted; _client.ReactionAdded += HandleReaction; _t.Change(TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2)); @@ -114,15 +112,15 @@ public class ReactionEvent : ICurrencyEvent private IEmbedBuilder GetEmbed(long pot) => _embedFunc(CurrencyEvent.Type.Reaction, _opts, pot); - private async Task OnMessageDeleted(Cacheable msg, Cacheable cacheable) + private async Task OnMessageDeleted(Cacheable message, Cacheable cacheable) { - if (msg.Id == _msg.Id) await StopEvent(); + if (message.Id == this.msg.Id) await StopEvent(); } public async Task StopEvent() { await Task.Yield(); - lock (stopLock) + lock (_stopLock) { if (Stopped) return; @@ -133,27 +131,27 @@ public class ReactionEvent : ICurrencyEvent _timeout?.Change(Timeout.Infinite, Timeout.Infinite); try { - var _ = _msg.DeleteAsync(); + _= msg.DeleteAsync(); } catch { } - var os = OnEnded(_guild.Id); + _ = OnEnded?.Invoke(_guild.Id); } } private Task HandleReaction( - Cacheable msg, + Cacheable message, Cacheable cacheable, SocketReaction r) { - var _ = Task.Run(() => + _= Task.Run(() => { - if (_emote.Name != r.Emote.Name) + if (emote.Name != r.Emote.Name) return; if ((r.User.IsSpecified ? r.User.Value : null) is not IGuildUser gu // no unknown users, as they could be bots, or alts - || msg.Id != _msg.Id // same message + || message.Id != this.msg.Id // same message || gu.IsBot // no bots || (DateTime.UtcNow - gu.CreatedAt).TotalDays <= 5 // no recently created accounts || (_noRecentlyJoinedServer @@ -177,7 +175,7 @@ public class ReactionEvent : ICurrencyEvent private bool TryTakeFromPot() { if (_isPotLimited) - lock (potLock) + lock (_potLock) { if (PotSize < _amount) return false; diff --git a/src/NadekoBot/Modules/Gambling/PlantPick/PlantPickService.cs b/src/NadekoBot/Modules/Gambling/PlantPick/PlantPickService.cs index 366c434c8..70933e12f 100644 --- a/src/NadekoBot/Modules/Gambling/PlantPick/PlantPickService.cs +++ b/src/NadekoBot/Modules/Gambling/PlantPick/PlantPickService.cs @@ -26,8 +26,8 @@ public class PlantPickService : INService private readonly DiscordSocketClient _client; private readonly GamblingConfigService _gss; - public readonly ConcurrentHashSet _generationChannels = new(); - private readonly SemaphoreSlim pickLock = new(1, 1); + private readonly ConcurrentHashSet _generationChannels; + private readonly SemaphoreSlim _pickLock = new(1, 1); public PlantPickService( DbService db, @@ -101,6 +101,7 @@ public class PlantPickService : INService /// Get a random currency image stream, with an optional password sticked onto it. /// /// Optional password to add to top left corner. + /// Extension of the file, defaults to png /// Stream of the currency image public Stream GetRandomCurrencyImage(string pass, out string extension) { @@ -140,7 +141,7 @@ public class PlantPickService : INService pass = pass.TrimTo(10, true).ToLowerInvariant(); using var img = Image.Load(curImg, out var format); // choose font size based on the image height, so that it's visible - var font = _fonts.NotoSans.CreateFont(img.Height / 12, FontStyle.Bold); + var font = _fonts.NotoSans.CreateFont(img.Height / 12.0f, FontStyle.Bold); img.Mutate(x => { // measure the size of the text to be drawing @@ -171,7 +172,7 @@ public class PlantPickService : INService if (!_generationChannels.Contains(channel.Id)) return Task.CompletedTask; - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -245,7 +246,7 @@ public class PlantPickService : INService ulong uid, string pass) { - await pickLock.WaitAsync(); + await _pickLock.WaitAsync(); try { long amount; @@ -276,7 +277,7 @@ public class PlantPickService : INService try { // delete all of the plant messages which have just been picked - var _ = ch.DeleteMessagesAsync(ids); + _= ch.DeleteMessagesAsync(ids); } catch { } @@ -285,7 +286,7 @@ public class PlantPickService : INService } finally { - pickLock.Release(); + _pickLock.Release(); } } diff --git a/src/NadekoBot/Modules/Gambling/Raffle/CurrencyRaffleService.cs b/src/NadekoBot/Modules/Gambling/Raffle/CurrencyRaffleService.cs index 38c0a2db7..6d456797b 100644 --- a/src/NadekoBot/Modules/Gambling/Raffle/CurrencyRaffleService.cs +++ b/src/NadekoBot/Modules/Gambling/Raffle/CurrencyRaffleService.cs @@ -13,14 +13,10 @@ public class CurrencyRaffleService : INService public Dictionary Games { get; } = new(); private readonly SemaphoreSlim _locker = new(1, 1); - private readonly DbService _db; private readonly ICurrencyService _cs; - public CurrencyRaffleService(DbService db, ICurrencyService cs) - { - _db = db; - _cs = cs; - } + public CurrencyRaffleService(ICurrencyService cs) + => _cs = cs; public async Task<(CurrencyRaffleGame, JoinErrorType?)> JoinOrCreateGame( ulong channelId, @@ -57,7 +53,7 @@ public class CurrencyRaffleService : INService if (newGame) { - var _t = Task.Run(async () => + _ = Task.Run(async () => { await Task.Delay(60000); await _locker.WaitAsync(); @@ -68,7 +64,7 @@ public class CurrencyRaffleService : INService await _cs.AddAsync(winner.DiscordUser.Id, "Currency Raffle Win", won); Games.Remove(channelId, out _); - var oe = onEnded(winner.DiscordUser, won); + _ = onEnded(winner.DiscordUser, won); } catch { } finally { _locker.Release(); } diff --git a/src/NadekoBot/Modules/Gambling/Slot/SlotCommands.cs b/src/NadekoBot/Modules/Gambling/Slot/SlotCommands.cs index 020c31764..96ad8db69 100644 --- a/src/NadekoBot/Modules/Gambling/Slot/SlotCommands.cs +++ b/src/NadekoBot/Modules/Gambling/Slot/SlotCommands.cs @@ -211,7 +211,7 @@ public partial class Gambling } finally { - var _ = Task.Run(async () => + _= Task.Run(async () => { await Task.Delay(1000); _runningUsers.Remove(ctx.User.Id); diff --git a/src/NadekoBot/Modules/Games/Acrophobia/Acrophobia.cs b/src/NadekoBot/Modules/Games/Acrophobia/Acrophobia.cs index cac8d5ba6..46e9e1db8 100644 --- a/src/NadekoBot/Modules/Games/Acrophobia/Acrophobia.cs +++ b/src/NadekoBot/Modules/Games/Acrophobia/Acrophobia.cs @@ -127,7 +127,7 @@ public sealed class AcrophobiaGame : IDisposable || !_usersWhoVoted.Add(userId)) break; ++submissions[toVoteFor]; - var _ = Task.Run(() => OnUserVoted(userName)); + _= Task.Run(() => OnUserVoted(userName)); return true; } diff --git a/src/NadekoBot/Modules/Games/Acrophobia/AcropobiaCommands.cs b/src/NadekoBot/Modules/Games/Acrophobia/AcropobiaCommands.cs index 97093d261..65d65fc94 100644 --- a/src/NadekoBot/Modules/Games/Acrophobia/AcropobiaCommands.cs +++ b/src/NadekoBot/Modules/Games/Acrophobia/AcropobiaCommands.cs @@ -48,7 +48,7 @@ public partial class Games if (msg.Channel.Id != ctx.Channel.Id) return Task.CompletedTask; - var _ = Task.Run(async () => + _= Task.Run(async () => { try { diff --git a/src/NadekoBot/Modules/Games/Nunchi/Nunchi.cs b/src/NadekoBot/Modules/Games/Nunchi/Nunchi.cs index 5a5b8d227..f7757ec55 100644 --- a/src/NadekoBot/Modules/Games/Nunchi/Nunchi.cs +++ b/src/NadekoBot/Modules/Games/Nunchi/Nunchi.cs @@ -85,7 +85,7 @@ public sealed class NunchiGame : IDisposable _killTimeout); CurrentPhase = Phase.Playing; - var _ = OnGameStarted?.Invoke(this); + _= OnGameStarted?.Invoke(this); var __ = OnRoundStarted?.Invoke(this, CurrentNumber); return true; } @@ -122,7 +122,7 @@ public sealed class NunchiGame : IDisposable { _killTimer.Change(Timeout.Infinite, Timeout.Infinite); CurrentPhase = Phase.Ended; - var _ = OnGameEnded?.Invoke(this, userTuple.Name); + _= OnGameEnded?.Invoke(this, userTuple.Name); } else // else just start the new round without the user who was the last { @@ -159,7 +159,7 @@ public sealed class NunchiGame : IDisposable { _killTimer.Change(Timeout.Infinite, Timeout.Infinite); CurrentPhase = Phase.Ended; - var _ = OnGameEnded?.Invoke(this, _participants.Count > 0 ? _participants.First().Name : null); + _= OnGameEnded?.Invoke(this, _participants.Count > 0 ? _participants.First().Name : null); return; } diff --git a/src/NadekoBot/Modules/Games/Nunchi/NunchiCommands.cs b/src/NadekoBot/Modules/Games/Nunchi/NunchiCommands.cs index 03450ca35..d99d005d1 100644 --- a/src/NadekoBot/Modules/Games/Nunchi/NunchiCommands.cs +++ b/src/NadekoBot/Modules/Games/Nunchi/NunchiCommands.cs @@ -55,7 +55,7 @@ public partial class Games Task _client_MessageReceived(SocketMessage arg) { - var _ = Task.Run(async () => + _= Task.Run(async () => { if (arg.Channel.Id != ctx.Channel.Id) return; diff --git a/src/NadekoBot/Modules/Games/Polls/PollRunner.cs b/src/NadekoBot/Modules/Games/Polls/PollRunner.cs index 197c46d1a..b6bf69499 100644 --- a/src/NadekoBot/Modules/Games/Polls/PollRunner.cs +++ b/src/NadekoBot/Modules/Games/Polls/PollRunner.cs @@ -43,7 +43,7 @@ public class PollRunner if (!Poll.Votes.Add(voteObj)) return false; - var _ = OnVoted?.Invoke(msg, usr); + _= OnVoted?.Invoke(msg, usr); } finally { _locker.Release(); } diff --git a/src/NadekoBot/Modules/Games/SpeedTyping/TypingGame.cs b/src/NadekoBot/Modules/Games/SpeedTyping/TypingGame.cs index 0ee4d2838..9545b945f 100644 --- a/src/NadekoBot/Modules/Games/SpeedTyping/TypingGame.cs +++ b/src/NadekoBot/Modules/Games/SpeedTyping/TypingGame.cs @@ -117,7 +117,7 @@ public class TypingGame private Task AnswerReceived(SocketMessage imsg) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { diff --git a/src/NadekoBot/Modules/Games/TicTacToe/TicTacToe.cs b/src/NadekoBot/Modules/Games/TicTacToe/TicTacToe.cs index 967530b74..063972804 100644 --- a/src/NadekoBot/Modules/Games/TicTacToe/TicTacToe.cs +++ b/src/NadekoBot/Modules/Games/TicTacToe/TicTacToe.cs @@ -182,7 +182,7 @@ public class TicTacToe private Task Client_MessageReceived(SocketMessage msg) { - var _ = Task.Run(async () => + _= Task.Run(async () => { await _moveLock.WaitAsync(); try diff --git a/src/NadekoBot/Modules/Games/TicTacToe/TicTacToeCommands.cs b/src/NadekoBot/Modules/Games/TicTacToe/TicTacToeCommands.cs index f8a2a3f5c..964accac5 100644 --- a/src/NadekoBot/Modules/Games/TicTacToe/TicTacToeCommands.cs +++ b/src/NadekoBot/Modules/Games/TicTacToe/TicTacToeCommands.cs @@ -28,7 +28,7 @@ public partial class Games { if (_service.TicTacToeGames.TryGetValue(channel.Id, out var game)) { - var _ = Task.Run(async () => + _= Task.Run(async () => { await game.Start((IGuildUser)ctx.User); }); diff --git a/src/NadekoBot/Modules/Games/Trivia/TriviaGame.cs b/src/NadekoBot/Modules/Games/Trivia/TriviaGame.cs index 3145e6787..d22b34431 100644 --- a/src/NadekoBot/Modules/Games/Trivia/TriviaGame.cs +++ b/src/NadekoBot/Modules/Games/Trivia/TriviaGame.cs @@ -24,10 +24,10 @@ public class TriviaGame private readonly ICurrencyService _cs; private readonly TriviaOptions _options; - private CancellationTokenSource _triviaCancelSource; + private CancellationTokenSource triviaCancelSource; private readonly TriviaQuestionPool _questionPool; - private int _timeoutCount; + private int timeoutCount; private readonly string _quitCommand; private readonly IEmbedBuilderService _eb; @@ -66,7 +66,7 @@ public class TriviaGame while (!ShouldStopGame) { // reset the cancellation source - _triviaCancelSource = new(); + triviaCancelSource = new(); showHowToQuit = !showHowToQuit; // load question @@ -121,7 +121,7 @@ public class TriviaGame try { //hint - await Task.Delay(_options.QuestionTimer * 1000 / 2, _triviaCancelSource.Token); + await Task.Delay(_options.QuestionTimer * 1000 / 2, triviaCancelSource.Token); if (!_options.NoHint) try { @@ -136,9 +136,9 @@ public class TriviaGame catch (Exception ex) { Log.Warning(ex, "Error editing triva message"); } //timeout - await Task.Delay(_options.QuestionTimer * 1000 / 2, _triviaCancelSource.Token); + await Task.Delay(_options.QuestionTimer * 1000 / 2, triviaCancelSource.Token); } - catch (TaskCanceledException) { _timeoutCount = 0; } //means someone guessed the answer + catch (TaskCanceledException) { timeoutCount = 0; } //means someone guessed the answer } finally { @@ -146,7 +146,7 @@ public class TriviaGame _client.MessageReceived -= PotentialGuess; } - if (!_triviaCancelSource.IsCancellationRequested) + if (!triviaCancelSource.IsCancellationRequested) try { var embed = _eb.Create() @@ -158,7 +158,7 @@ public class TriviaGame await Channel.EmbedAsync(embed); - if (_options.Timeout != 0 && ++_timeoutCount >= _options.Timeout) + if (_options.Timeout != 0 && ++timeoutCount >= _options.Timeout) await StopGame(); } catch (Exception ex) @@ -198,7 +198,7 @@ public class TriviaGame private Task PotentialGuess(SocketMessage imsg) { - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -218,7 +218,7 @@ public class TriviaGame { if (GameActive && CurrentQuestion.IsAnswerCorrect(umsg.Content) - && !_triviaCancelSource.IsCancellationRequested) + && !triviaCancelSource.IsCancellationRequested) { Users.AddOrUpdate(guildUser, 1, (_, old) => ++old); guess = true; @@ -227,7 +227,7 @@ public class TriviaGame finally { _guessLock.Release(); } if (!guess) return; - _triviaCancelSource.Cancel(); + triviaCancelSource.Cancel(); if (_options.WinRequirement != 0 && Users[guildUser] == _options.WinRequirement) diff --git a/src/NadekoBot/Modules/Games/Trivia/TriviaQuestion.cs b/src/NadekoBot/Modules/Games/Trivia/TriviaQuestion.cs index c7d08a0f9..26f7d7bf5 100644 --- a/src/NadekoBot/Modules/Games/Trivia/TriviaQuestion.cs +++ b/src/NadekoBot/Modules/Games/Trivia/TriviaQuestion.cs @@ -21,9 +21,9 @@ public class TriviaQuestion public string Answer { get; set; } public string CleanAnswer - => _cleanAnswer ?? (_cleanAnswer = Clean(Answer)); + => cleanAnswer ?? (cleanAnswer = Clean(Answer)); - private string _cleanAnswer; + private string cleanAnswer; public TriviaQuestion( string q, diff --git a/src/NadekoBot/Modules/Music/Services/extractor/Misc.cs b/src/NadekoBot/Modules/Music/Services/extractor/Misc.cs index e064fcb8f..2e8b270be 100644 --- a/src/NadekoBot/Modules/Music/Services/extractor/Misc.cs +++ b/src/NadekoBot/Modules/Music/Services/extractor/Misc.cs @@ -52,7 +52,7 @@ public sealed partial class YtLoader public sealed class YtTrackInfo : TrackInfo { - private const string BaseYoutubeUrl = "https://youtube.com/watch?v="; + private const string BASE_YOUTUBE_URL = "https://youtube.com/watch?v="; public override string Url { get; } public override string Title { get; } public override TimeSpan Duration { get; } @@ -62,7 +62,7 @@ public sealed partial class YtLoader public YtTrackInfo(string title, string videoId, TimeSpan duration) { Title = title; - Url = BaseYoutubeUrl + videoId; + Url = BASE_YOUTUBE_URL + videoId; Duration = duration; _videoId = videoId; diff --git a/src/NadekoBot/Modules/Music/Services/extractor/YtLoader.cs b/src/NadekoBot/Modules/Music/Services/extractor/YtLoader.cs index e1fd93b6b..4f2aece64 100644 --- a/src/NadekoBot/Modules/Music/Services/extractor/YtLoader.cs +++ b/src/NadekoBot/Modules/Music/Services/extractor/YtLoader.cs @@ -7,10 +7,10 @@ namespace NadekoBot.Modules.Music.Services; public sealed partial class YtLoader { - private static readonly byte[] YT_RESULT_INITIAL_DATA = Encoding.UTF8.GetBytes("var ytInitialData = "); - private static readonly byte[] YT_RESULT_JSON_END = Encoding.UTF8.GetBytes(";<"); + private static readonly byte[] _ytResultInitialData = Encoding.UTF8.GetBytes("var ytInitialData = "); + private static readonly byte[] _ytResultJsonEnd = Encoding.UTF8.GetBytes(";<"); - private static readonly string[] durationFormats = + private static readonly string[] _durationFormats = { @"m\:ss", @"mm\:ss", @"h\:mm\:ss", @"hh\:mm\:ss", @"hhh\:mm\:ss" }; @@ -98,7 +98,7 @@ public sealed partial class YtLoader var durationString = elem.GetProperty("lengthText").GetProperty("simpleText").GetString(); if (!TimeSpan.TryParseExact(durationString, - durationFormats, + _durationFormats, CultureInfo.InvariantCulture, out var duration)) { @@ -117,13 +117,13 @@ public sealed partial class YtLoader private Memory GetScriptResponseSpan(byte[] response) { var responseSpan = response.AsSpan()[140_000..]; - var startIndex = responseSpan.IndexOf(YT_RESULT_INITIAL_DATA); + var startIndex = responseSpan.IndexOf(_ytResultInitialData); if (startIndex == -1) return null; // todo future try selecting html - startIndex += YT_RESULT_INITIAL_DATA.Length; + startIndex += _ytResultInitialData.Length; var endIndex = - 140_000 + startIndex + responseSpan[(startIndex + 20_000)..].IndexOf(YT_RESULT_JSON_END) + 20_000; + 140_000 + startIndex + responseSpan[(startIndex + 20_000)..].IndexOf(_ytResultJsonEnd) + 20_000; startIndex += 140_000; return response.AsMemory(startIndex, endIndex - startIndex); } diff --git a/src/NadekoBot/Modules/Music/_Common/Impl/MusicQueue.cs b/src/NadekoBot/Modules/Music/_Common/Impl/MusicQueue.cs index 0e5b6c541..02b5a2d62 100644 --- a/src/NadekoBot/Modules/Music/_Common/Impl/MusicQueue.cs +++ b/src/NadekoBot/Modules/Music/_Common/Impl/MusicQueue.cs @@ -42,9 +42,9 @@ public sealed partial class MusicQueue : IMusicQueue { // just make sure the internal logic runs first // to make sure that some potential indermediate value is not returned - lock (locker) + lock (_locker) { - return _index; + return index; } } } @@ -53,128 +53,128 @@ public sealed partial class MusicQueue : IMusicQueue { get { - lock (locker) + lock (_locker) { - return _tracks.Count; + return tracks.Count; } } } - private LinkedList _tracks; + private LinkedList tracks; - private int _index; + private int index; - private readonly object locker = new(); + private readonly object _locker = new(); public MusicQueue() { - _index = 0; - _tracks = new(); + index = 0; + tracks = new(); } - public IQueuedTrackInfo Enqueue(ITrackInfo trackInfo, string queuer, out int index) + public IQueuedTrackInfo Enqueue(ITrackInfo trackInfo, string queuer, out int enqueuedAt) { - lock (locker) + lock (_locker) { var added = new QueuedTrackInfo(trackInfo, queuer); - index = _tracks.Count; - _tracks.AddLast(added); + enqueuedAt = tracks.Count; + tracks.AddLast(added); return added; } } - public IQueuedTrackInfo EnqueueNext(ITrackInfo trackInfo, string queuer, out int index) + public IQueuedTrackInfo EnqueueNext(ITrackInfo trackInfo, string queuer, out int trackIndex) { - lock (locker) + lock (_locker) { - if (_tracks.Count == 0) return Enqueue(trackInfo, queuer, out index); + if (tracks.Count == 0) return Enqueue(trackInfo, queuer, out trackIndex); - var currentNode = _tracks.First!; + var currentNode = tracks.First!; int i; - for (i = 1; i <= _index; i++) + for (i = 1; i <= this.index; i++) currentNode = currentNode.Next!; // can't be null because index is always in range of the count var added = new QueuedTrackInfo(trackInfo, queuer); - index = i; + trackIndex = i; - _tracks.AddAfter(currentNode, added); + tracks.AddAfter(currentNode, added); return added; } } - public void EnqueueMany(IEnumerable tracks, string queuer) + public void EnqueueMany(IEnumerable toEnqueue, string queuer) { - lock (locker) + lock (_locker) { - foreach (var track in tracks) + foreach (var track in toEnqueue) { var added = new QueuedTrackInfo(track, queuer); - _tracks.AddLast(added); + this.tracks.AddLast(added); } } } public IReadOnlyCollection List() { - lock (locker) + lock (_locker) { - return _tracks.ToList(); + return tracks.ToList(); } } - public IQueuedTrackInfo? GetCurrent(out int index) + public IQueuedTrackInfo? GetCurrent(out int currentIndex) { - lock (locker) + lock (_locker) { - index = _index; - return _tracks.ElementAtOrDefault(_index); + currentIndex = index; + return tracks.ElementAtOrDefault(index); } } public void Advance() { - lock (locker) + lock (_locker) { - if (++_index >= _tracks.Count) - _index = 0; + if (++index >= tracks.Count) + index = 0; } } public void Clear() { - lock (locker) + lock (_locker) { - _tracks.Clear(); + tracks.Clear(); } } - public bool SetIndex(int index) + public bool SetIndex(int newIndex) { - lock (locker) + lock (_locker) { - if (index < 0 || index >= _tracks.Count) + if (newIndex < 0 || newIndex >= tracks.Count) return false; - _index = index; + this.index = newIndex; return true; } } - private void RemoveAtInternal(int index, out IQueuedTrackInfo trackInfo) + private void RemoveAtInternal(int remoteAtIndex, out IQueuedTrackInfo trackInfo) { - var removedNode = _tracks.First!; + var removedNode = tracks.First!; int i; - for (i = 0; i < index; i++) removedNode = removedNode.Next!; + for (i = 0; i < remoteAtIndex; i++) removedNode = removedNode.Next!; trackInfo = removedNode.Value; - _tracks.Remove(removedNode); + tracks.Remove(removedNode); - if (i <= _index) - --_index; + if (i <= this.index) + --this.index; - if (_index < 0) - _index = Count; + if (this.index < 0) + this.index = Count; // if it was the last song in the queue // // wrap back to start @@ -188,10 +188,10 @@ public sealed partial class MusicQueue : IMusicQueue public void RemoveCurrent() { - lock (locker) + lock (_locker) { - if (_index < _tracks.Count) - RemoveAtInternal(_index, out _); + if (index < tracks.Count) + RemoveAtInternal(index, out _); } } @@ -204,29 +204,29 @@ public sealed partial class MusicQueue : IMusicQueue if (to == from) throw new ArgumentException($"{nameof(from)} and {nameof(to)} must be different"); - lock (locker) + lock (_locker) { if (from >= Count || to >= Count) return null; // update current track index - if (from == _index) + if (from == index) { // if the song being moved is the current track // it means that it will for sure end up on the destination - _index = to; + index = to; } else { // moving a track from below the current track means // means it will drop down - if (from < _index) - _index--; + if (from < index) + index--; // moving a track to below the current track // means it will rise up - if (to <= _index) - _index++; + if (to <= index) + index++; // if both from and to are below _index - net change is + 1 - 1 = 0 @@ -236,78 +236,76 @@ public sealed partial class MusicQueue : IMusicQueue } // get the node which needs to be moved - var fromNode = _tracks.First!; + var fromNode = tracks.First!; for (var i = 0; i < from; i++) fromNode = fromNode.Next!; // remove it from the queue - _tracks.Remove(fromNode); + tracks.Remove(fromNode); // if it needs to be added as a first node, // add it directly and return if (to == 0) { - _tracks.AddFirst(fromNode); + tracks.AddFirst(fromNode); return fromNode.Value; } // else find the node at the index before the specified target - var addAfterNode = _tracks.First!; + var addAfterNode = tracks.First!; for (var i = 1; i < to; i++) addAfterNode = addAfterNode.Next!; // and add after it - _tracks.AddAfter(addAfterNode, fromNode); + tracks.AddAfter(addAfterNode, fromNode); return fromNode.Value; } } public void Shuffle(Random rng) { - lock (locker) + lock (_locker) { - var list = _tracks.ToList(); + var list = tracks.ToList(); for (var i = 0; i < list.Count; i++) { var struck = rng.Next(i, list.Count); - var temp = list[struck]; - list[struck] = list[i]; - list[i] = temp; + (list[struck], list[i]) = (list[i], list[struck]); // could preserving the index during shuffling be done better? - if (i == _index) - _index = struck; - else if (struck == _index) - _index = i; + if (i == index) + index = struck; + else if (struck == index) + index = i; } - _tracks = new(list); + tracks = new(list); } } public bool IsLast() { - lock (locker) + lock (_locker) { - return _index == _tracks.Count // if there are no tracks - || _index == _tracks.Count - 1; + return index == tracks.Count // if there are no tracks + || index == tracks.Count - 1; } } - public bool TryRemoveAt(int index, out IQueuedTrackInfo? trackInfo, out bool isCurrent) + public bool TryRemoveAt(int remoteAt, out IQueuedTrackInfo? trackInfo, out bool isCurrent) { - lock (locker) + lock (_locker) { isCurrent = false; trackInfo = null; - if (index < 0 || index >= _tracks.Count) + if (remoteAt < 0 || remoteAt >= tracks.Count) return false; - if (index == _index) isCurrent = true; + if (remoteAt == this.index) isCurrent = true; - RemoveAtInternal(index, out trackInfo); + RemoveAtInternal(remoteAt, out trackInfo); return true; } diff --git a/src/NadekoBot/Modules/Music/_Common/Impl/VoiceProxy.cs b/src/NadekoBot/Modules/Music/_Common/Impl/VoiceProxy.cs index 2b74424a9..33842d5b6 100644 --- a/src/NadekoBot/Modules/Music/_Common/Impl/VoiceProxy.cs +++ b/src/NadekoBot/Modules/Music/_Common/Impl/VoiceProxy.cs @@ -17,7 +17,7 @@ public sealed class VoiceProxy : IVoiceProxy private const int DELAY_ON_ERROR_MILISECONDS = 200; public VoiceProxyState State - => _gateway switch + => gateway switch { { Started: true, Stopped: false } => VoiceProxyState.Started, { Stopped: false } => VoiceProxyState.Created, @@ -25,16 +25,16 @@ public sealed class VoiceProxy : IVoiceProxy }; - private VoiceGateway _gateway; + private VoiceGateway gateway; public VoiceProxy(VoiceGateway initial) - => _gateway = initial; + => gateway = initial; public bool SendPcmFrame(VoiceClient vc, Span data, int length) { try { - var gw = _gateway; + var gw = gateway; if (gw is null || gw.Stopped || !gw.Started) return false; vc.SendPcmFrame(gw, data, 0, length); @@ -55,7 +55,7 @@ public sealed class VoiceProxy : IVoiceProxy try { - var gw = _gateway; + var gw = gateway; if (gw is null || !gw.ConnectingFinished.Task.IsCompleted) { ++errorCount; @@ -78,8 +78,8 @@ public sealed class VoiceProxy : IVoiceProxy return State != VoiceProxyState.Stopped && errorCount <= MAX_ERROR_COUNT; } - public void SetGateway(VoiceGateway gateway) - => _gateway = gateway; + public void SetGateway(VoiceGateway newGateway) + => gateway = newGateway; public Task StartSpeakingAsync() => RunGatewayAction(gw => gw.SendSpeakingAsync(VoiceSpeaking.State.Microphone)); @@ -88,11 +88,11 @@ public sealed class VoiceProxy : IVoiceProxy => RunGatewayAction(gw => gw.SendSpeakingAsync(VoiceSpeaking.State.None)); public async Task StartGateway() - => await _gateway.Start(); + => await gateway.Start(); public Task StopGateway() { - if (_gateway is { } gw) + if (gateway is { } gw) return gw.StopAsync(); return Task.CompletedTask; diff --git a/src/NadekoBot/Modules/Music/_Common/Resolvers/LocalTrackResolver.cs b/src/NadekoBot/Modules/Music/_Common/Resolvers/LocalTrackResolver.cs index 3a6ddec13..850ee5420 100644 --- a/src/NadekoBot/Modules/Music/_Common/Resolvers/LocalTrackResolver.cs +++ b/src/NadekoBot/Modules/Music/_Common/Resolvers/LocalTrackResolver.cs @@ -45,7 +45,8 @@ public sealed class LocalTrackResolver : ILocalTrackResolver if (!x.Attributes.HasFlag(FileAttributes.Hidden | FileAttributes.System) && _musicExtensions.Contains(x.Extension.ToUpperInvariant())) return true; return false; - }); + }) + .ToList(); var firstFile = files.FirstOrDefault()?.FullName; if (firstFile is null) diff --git a/src/NadekoBot/Modules/Nsfw/SearchImageCacher.cs b/src/NadekoBot/Modules/Nsfw/SearchImageCacher.cs index 2d0ef23f1..7c2d498da 100644 --- a/src/NadekoBot/Modules/Nsfw/SearchImageCacher.cs +++ b/src/NadekoBot/Modules/Nsfw/SearchImageCacher.cs @@ -5,7 +5,7 @@ namespace NadekoBot.Modules.Nsfw.Common; public class SearchImageCacher : INService { - private static readonly ISet defaultTagBlacklist = new HashSet + private static readonly ISet _defaultTagBlacklist = new HashSet { "loli", "lolicon", @@ -21,7 +21,7 @@ public class SearchImageCacher : INService private readonly Dictionary> _usedTags = new(); private readonly IMemoryCache _cache; - private readonly ConcurrentDictionary<(Booru, string), int> maxPages = new(); + private readonly ConcurrentDictionary<(Booru, string), int> _maxPages = new(); public SearchImageCacher(IHttpClientFactory httpFactory, IMemoryCache cache) { @@ -59,7 +59,7 @@ public class SearchImageCacher : INService // Log.Warning("Got no images for {0}, tags: {1}", type, string.Join(", ", tags)); return false; - Log.Information("Updating {0}...", type); + Log.Information("Updating {Type}...", type); lock (_typeLocks[type]) { var typeUsedTags = _usedTags[type]; @@ -75,7 +75,7 @@ public class SearchImageCacher : INService { // if any of the tags is a tag banned by discord // do not put that image in the cache - if (defaultTagBlacklist.Overlaps(img.Tags)) + if (_defaultTagBlacklist.Overlaps(img.Tags)) continue; // if image doesn't have a proper absolute uri, skip it @@ -190,7 +190,7 @@ public class SearchImageCacher : INService tags = tags[..2]; // use both tags banned by discord and tags banned on the server - if (blacklistedTags.Overlaps(tags) || defaultTagBlacklist.Overlaps(tags)) + if (blacklistedTags.Overlaps(tags) || _defaultTagBlacklist.Overlaps(tags)) return default; // query for an image @@ -223,16 +223,16 @@ public class SearchImageCacher : INService CancellationToken cancel) { var tagStr = string.Join(' ', tags.OrderByDescending(x => x)); - var page = 0; var attempt = 0; while (attempt++ <= 10) { - if (maxPages.TryGetValue((type, tagStr), out var maxPage)) + int page; + if (_maxPages.TryGetValue((type, tagStr), out var maxPage)) { if (maxPage == 0) { - Log.Information("Tag {0} yields no result on {1}, skipping.", tagStr, type); + Log.Information("Tag {Tags} yields no result on {Type}, skipping", tagStr, type); return new(); } @@ -247,7 +247,7 @@ public class SearchImageCacher : INService if (result is null or { Count: 0 }) { - Log.Information("Tag {0}, page {1} has no result on {2}.", + Log.Information("Tag {Tags}, page {Page} has no result on {Type}", string.Join(", ", tags), page, type.ToString()); @@ -284,7 +284,7 @@ public class SearchImageCacher : INService { try { - Log.Information("Downloading from {0} (page {1})...", type, page); + Log.Information("Downloading from {Type} (page {Page})...", type, page); using var http = _httpFactory.CreateClient(); var downloader = GetImageDownloader(type, http); @@ -293,7 +293,7 @@ public class SearchImageCacher : INService if (images.Count == 0) { var tagStr = string.Join(' ', tags.OrderByDescending(x => x)); - maxPages[(type, tagStr)] = page; + _maxPages[(type, tagStr)] = page; } return images; @@ -305,7 +305,7 @@ public class SearchImageCacher : INService catch (Exception ex) { Log.Error(ex, - "Error downloading an image:\nTags: {0}\nType: {1}\nPage: {2}\nMessage: {3}", + "Error downloading an image:\nTags: {Tags}\nType: {Type}\nPage: {Page}\nMessage: {Message}", string.Join(", ", tags), type, page, diff --git a/src/NadekoBot/Modules/Permissions/Blacklist/BlacklistService.cs b/src/NadekoBot/Modules/Permissions/Blacklist/BlacklistService.cs index cc10f0ff2..f47f4ad76 100644 --- a/src/NadekoBot/Modules/Permissions/Blacklist/BlacklistService.cs +++ b/src/NadekoBot/Modules/Permissions/Blacklist/BlacklistService.cs @@ -14,9 +14,9 @@ public sealed class BlacklistService : IEarlyBehavior private readonly DbService _db; private readonly IPubSub _pubSub; private readonly IBotCredentials _creds; - private IReadOnlyList _blacklist; + private IReadOnlyList blacklist; - private readonly TypedKey blPubKey = new("blacklist.reload"); + private readonly TypedKey _blPubKey = new("blacklist.reload"); public BlacklistService(DbService db, IPubSub pubSub, IBotCredentials creds) { @@ -25,18 +25,18 @@ public sealed class BlacklistService : IEarlyBehavior _creds = creds; Reload(false); - _pubSub.Sub(blPubKey, OnReload); + _pubSub.Sub(_blPubKey, OnReload); } - private ValueTask OnReload(BlacklistEntry[] blacklist) + private ValueTask OnReload(BlacklistEntry[] newBlacklist) { - _blacklist = blacklist; + this.blacklist = newBlacklist; return default; } public Task RunBehavior(IGuild guild, IUserMessage usrMsg) { - foreach (var bl in _blacklist) + foreach (var bl in blacklist) { if (guild is not null && bl.Type == BlacklistType.Server && bl.ItemId == guild.Id) { @@ -68,14 +68,14 @@ public sealed class BlacklistService : IEarlyBehavior } public IReadOnlyList GetBlacklist() - => _blacklist; + => blacklist; public void Reload(bool publish = true) { using var uow = _db.GetDbContext(); var toPublish = uow.Blacklist.AsNoTracking().ToArray(); - _blacklist = toPublish; - if (publish) _pubSub.Pub(blPubKey, toPublish); + blacklist = toPublish; + if (publish) _pubSub.Pub(_blPubKey, toPublish); } public void Blacklist(BlacklistType type, ulong id) diff --git a/src/NadekoBot/Modules/Permissions/CommandCooldown/CmdCdService.cs b/src/NadekoBot/Modules/Permissions/CommandCooldown/CmdCdService.cs index bb0cd3fcb..4a019c37e 100644 --- a/src/NadekoBot/Modules/Permissions/CommandCooldown/CmdCdService.cs +++ b/src/NadekoBot/Modules/Permissions/CommandCooldown/CmdCdService.cs @@ -30,7 +30,7 @@ public class CmdCdService : ILateBlocker, INService activeCdsForGuild.Add(new() { UserId = user.Id, Command = commandName }); - var _ = Task.Run(async () => + _= Task.Run(async () => { try { diff --git a/src/NadekoBot/Modules/Permissions/Filter/FilterService.cs b/src/NadekoBot/Modules/Permissions/Filter/FilterService.cs index 5f82d6dee..79d72c6a5 100644 --- a/src/NadekoBot/Modules/Permissions/Filter/FilterService.cs +++ b/src/NadekoBot/Modules/Permissions/Filter/FilterService.cs @@ -62,7 +62,7 @@ public sealed class FilterService : IEarlyBehavior client.MessageUpdated += (oldData, newMsg, channel) => { - var _ = Task.Run(() => + _= Task.Run(() => { var guild = (channel as ITextChannel)?.Guild; diff --git a/src/NadekoBot/Modules/Searches/Crypto/CryptoService.cs b/src/NadekoBot/Modules/Searches/Crypto/CryptoService.cs index 632bdf652..e11dfa930 100644 --- a/src/NadekoBot/Modules/Searches/Crypto/CryptoService.cs +++ b/src/NadekoBot/Modules/Searches/Crypto/CryptoService.cs @@ -60,8 +60,8 @@ public class CryptoService : INService { try { - using var _http = _httpFactory.CreateClient(); - var strData = await _http.GetStringAsync( + using var http = _httpFactory.CreateClient(); + var strData = await http.GetStringAsync( "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?" + $"CMC_PRO_API_KEY={_creds.CoinmarketcapApiKey}" + "&start=1" diff --git a/src/NadekoBot/Modules/Searches/Feeds/FeedsService.cs b/src/NadekoBot/Modules/Searches/Feeds/FeedsService.cs index a6bb16d12..8c878c045 100644 --- a/src/NadekoBot/Modules/Searches/Feeds/FeedsService.cs +++ b/src/NadekoBot/Modules/Searches/Feeds/FeedsService.cs @@ -40,7 +40,7 @@ public class FeedsService : INService _client = client; _eb = eb; - var _ = Task.Run(TrackFeeds); + _= Task.Run(TrackFeeds); } public async Task TrackFeeds() diff --git a/src/NadekoBot/Modules/Searches/Searches.cs b/src/NadekoBot/Modules/Searches/Searches.cs index a2753dd56..30a0cecc4 100644 --- a/src/NadekoBot/Modules/Searches/Searches.cs +++ b/src/NadekoBot/Modules/Searches/Searches.cs @@ -477,7 +477,7 @@ public partial class Searches : NadekoModule if (!await ValidateQuery(word)) return; - using var _http = _httpFactory.CreateClient(); + using var http = _httpFactory.CreateClient(); string res; try { @@ -485,7 +485,7 @@ public partial class Searches : NadekoModule e => { e.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(12); - return _http.GetStringAsync("https://api.pearson.com/v2/dictionaries/entries?headword=" + return http.GetStringAsync("https://api.pearson.com/v2/dictionaries/entries?headword=" + WebUtility.UrlEncode(word)); }); diff --git a/src/NadekoBot/Modules/Searches/SearchesService.cs b/src/NadekoBot/Modules/Searches/SearchesService.cs index 63457f374..33606803b 100644 --- a/src/NadekoBot/Modules/Searches/SearchesService.cs +++ b/src/NadekoBot/Modules/Searches/SearchesService.cs @@ -430,8 +430,8 @@ public class SearchesService : INService { var redis = _cache.Redis; var db = redis.GetDatabase(); - const string STEAM_GAME_IDS_KEY = "steam_names_to_appid"; - var exists = await db.KeyExistsAsync(STEAM_GAME_IDS_KEY); + const string steamGameIdsKey = "steam_names_to_appid"; + // var exists = await db.KeyExistsAsync(steamGameIdsKey); // if we didn't get steam name to id map already, get it //if (!exists) @@ -448,7 +448,7 @@ public class SearchesService : INService // } //} - var gamesMap = await _cache.GetOrAddCachedDataAsync(STEAM_GAME_IDS_KEY, + var gamesMap = await _cache.GetOrAddCachedDataAsync(steamGameIdsKey, async _ => { using var http = _httpFactory.CreateClient(); diff --git a/src/NadekoBot/Modules/Searches/XkcdCommands.cs b/src/NadekoBot/Modules/Searches/XkcdCommands.cs index ad3f9d474..8df65047b 100644 --- a/src/NadekoBot/Modules/Searches/XkcdCommands.cs +++ b/src/NadekoBot/Modules/Searches/XkcdCommands.cs @@ -8,7 +8,7 @@ public partial class Searches [Group] public partial class XkcdCommands : NadekoSubmodule { - private const string _xkcdUrl = "https://xkcd.com"; + private const string XKCD_URL = "https://xkcd.com"; private readonly IHttpClientFactory _httpFactory; public XkcdCommands(IHttpClientFactory factory) @@ -23,12 +23,12 @@ public partial class Searches try { using var http = _httpFactory.CreateClient(); - var res = await http.GetStringAsync($"{_xkcdUrl}/info.0.json"); + var res = await http.GetStringAsync($"{XKCD_URL}/info.0.json"); var comic = JsonConvert.DeserializeObject(res); var embed = _eb.Create() .WithOkColor() .WithImageUrl(comic.ImageLink) - .WithAuthor(comic.Title, "https://xkcd.com/s/919f27.ico", $"{_xkcdUrl}/{comic.Num}") + .WithAuthor(comic.Title, "https://xkcd.com/s/919f27.ico", $"{XKCD_URL}/{comic.Num}") .AddField(GetText(strs.comic_number), comic.Num.ToString(), true) .AddField(GetText(strs.date), $"{comic.Month}/{comic.Year}", true); var sent = await ctx.Channel.EmbedAsync(embed); @@ -57,13 +57,13 @@ public partial class Searches try { using var http = _httpFactory.CreateClient(); - var res = await http.GetStringAsync($"{_xkcdUrl}/{num}/info.0.json"); + var res = await http.GetStringAsync($"{XKCD_URL}/{num}/info.0.json"); var comic = JsonConvert.DeserializeObject(res); var embed = _eb.Create() .WithOkColor() .WithImageUrl(comic.ImageLink) - .WithAuthor(comic.Title, "https://xkcd.com/s/919f27.ico", $"{_xkcdUrl}/{num}") + .WithAuthor(comic.Title, "https://xkcd.com/s/919f27.ico", $"{XKCD_URL}/{num}") .AddField(GetText(strs.comic_number), comic.Num.ToString(), true) .AddField(GetText(strs.date), $"{comic.Month}/{comic.Year}", true); diff --git a/src/NadekoBot/Modules/Utility/CommandMap/CommandMapService.cs b/src/NadekoBot/Modules/Utility/CommandMap/CommandMapService.cs index fda02a724..9db69d29d 100644 --- a/src/NadekoBot/Modules/Utility/CommandMap/CommandMapService.cs +++ b/src/NadekoBot/Modules/Utility/CommandMap/CommandMapService.cs @@ -74,7 +74,7 @@ public class CommandMapService : IInputTransformer, INService try { var toDelete = await channel.SendConfirmAsync(_eb, $"{input} => {newInput}"); - var _ = Task.Run(async () => + _= Task.Run(async () => { await Task.Delay(1500); await toDelete.DeleteAsync(new() { RetryMode = RetryMode.AlwaysRetry }); diff --git a/src/NadekoBot/Modules/Utility/Quote/QuoteCommands.cs b/src/NadekoBot/Modules/Utility/Quote/QuoteCommands.cs index fb33ec976..2eab53b1f 100644 --- a/src/NadekoBot/Modules/Utility/Quote/QuoteCommands.cs +++ b/src/NadekoBot/Modules/Utility/Quote/QuoteCommands.cs @@ -12,7 +12,7 @@ public partial class Utility [Group] public partial class QuoteCommands : NadekoSubmodule { - private const string _prependExport = + private const string PREPEND_EXPORT = @"# Keys are keywords, Each key has a LIST of quotes in the following format: # - id: Alphanumeric id used for commands related to the quote. (Note, when using .quotesimport, a new id will be generated.) # an: Author name @@ -279,7 +279,7 @@ public partial class Utility var exprsDict = quotes.GroupBy(x => x.Keyword) .ToDictionary(x => x.Key, x => x.Select(ExportedQuote.FromModel)); - var text = _prependExport + _exportSerializer.Serialize(exprsDict).UnescapeUnicodeCodePoints(); + var text = PREPEND_EXPORT + _exportSerializer.Serialize(exprsDict).UnescapeUnicodeCodePoints(); await using var stream = await text.ToStream(); await ctx.Channel.SendFileAsync(stream, "quote-export.yml"); diff --git a/src/NadekoBot/Modules/Utility/StreamRole/StreamRoleService.cs b/src/NadekoBot/Modules/Utility/StreamRole/StreamRoleService.cs index 9e21aa3a5..ef2721b63 100644 --- a/src/NadekoBot/Modules/Utility/StreamRole/StreamRoleService.cs +++ b/src/NadekoBot/Modules/Utility/StreamRole/StreamRoleService.cs @@ -11,20 +11,20 @@ public class StreamRoleService : INService { private readonly DbService _db; private readonly DiscordSocketClient _client; - private readonly ConcurrentDictionary guildSettings; + private readonly ConcurrentDictionary _guildSettings; public StreamRoleService(DiscordSocketClient client, DbService db, Bot bot) { _db = db; _client = client; - guildSettings = bot.AllGuildConfigs.ToDictionary(x => x.GuildId, x => x.StreamRole) + _guildSettings = bot.AllGuildConfigs.ToDictionary(x => x.GuildId, x => x.StreamRole) .Where(x => x.Value is { Enabled: true }) .ToConcurrent(); _client.GuildMemberUpdated += Client_GuildMemberUpdated; - var _ = Task.Run(async () => + _= Task.Run(async () => { try { @@ -39,10 +39,10 @@ public class StreamRoleService : INService private Task Client_GuildMemberUpdated(Cacheable cacheable, SocketGuildUser after) { - var _ = Task.Run(async () => + _= Task.Run(async () => { //if user wasn't streaming or didn't have a game status at all - if (guildSettings.TryGetValue(after.Guild.Id, out var setting)) await RescanUser(after, setting); + if (_guildSettings.TryGetValue(after.Guild.Id, out var setting)) await RescanUser(after, setting); }); return Task.CompletedTask; @@ -51,6 +51,7 @@ public class StreamRoleService : INService /// /// Adds or removes a user from a blacklist or a whitelist in the specified guild. /// + /// List type /// Guild /// Add or rem action /// User's Id @@ -97,7 +98,6 @@ public class StreamRoleService : INService var toRemove = streamRoleSettings.Blacklist.FirstOrDefault(x => x.Equals(userObj)); if (toRemove is not null) { - success = true; success = streamRoleSettings.Blacklist.Remove(toRemove); } } @@ -123,7 +123,7 @@ public class StreamRoleService : INService /// The keyword set public async Task SetKeyword(IGuild guild, string keyword) { - keyword = keyword?.Trim()?.ToLowerInvariant(); + keyword = keyword?.Trim().ToLowerInvariant(); await using (var uow = _db.GetDbContext()) { @@ -145,7 +145,7 @@ public class StreamRoleService : INService /// The keyword set public string GetKeyword(ulong guildId) { - if (guildSettings.TryGetValue(guildId, out var outSetting)) + if (_guildSettings.TryGetValue(guildId, out var outSetting)) return outSetting.Keyword; StreamRoleSettings setting; @@ -193,7 +193,8 @@ public class StreamRoleService : INService /// /// Stops the stream role feature on the specified guild. /// - /// Guild's Id + /// Guild + /// Whether to rescan users public async Task StopStreamRole(IGuild guild, bool cleanup = false) { await using (var uow = _db.GetDbContext()) @@ -205,7 +206,7 @@ public class StreamRoleService : INService await uow.SaveChangesAsync(); } - if (guildSettings.TryRemove(guild.Id, out var setting) && cleanup) + if (_guildSettings.TryRemove(guild.Id, out _) && cleanup) await RescanUsers(guild); } @@ -231,7 +232,7 @@ public class StreamRoleService : INService if (addRole is null) { await StopStreamRole(user.Guild); - Log.Warning("Stream role in server {0} no longer exists. Stopping.", setting.AddRoleId); + Log.Warning("Stream role in server {RoleId} no longer exists. Stopping", setting.AddRoleId); return; } @@ -239,7 +240,7 @@ public class StreamRoleService : INService if (!user.RoleIds.Contains(addRole.Id)) { await user.AddRoleAsync(addRole); - Log.Information("Added stream role to user {0} in {1} server", + Log.Information("Added stream role to user {User} in {Server} server", user.ToString(), user.Guild.ToString()); } @@ -266,7 +267,7 @@ public class StreamRoleService : INService throw new StreamRoleNotFoundException(); await user.RemoveRoleAsync(addRole); - Log.Information("Removed stream role from the user {0} in {1} server", + Log.Information("Removed stream role from the user {User} in {Server} server", user.ToString(), user.Guild.ToString()); } @@ -281,7 +282,7 @@ public class StreamRoleService : INService private async Task RescanUsers(IGuild guild) { - if (!guildSettings.TryGetValue(guild.Id, out var setting)) + if (!_guildSettings.TryGetValue(guild.Id, out var setting)) return; var addRole = guild.GetRole(setting.AddRoleId); @@ -299,5 +300,5 @@ public class StreamRoleService : INService } private void UpdateCache(ulong guildId, StreamRoleSettings setting) - => guildSettings.AddOrUpdate(guildId, _ => setting, (_, _) => setting); + => _guildSettings.AddOrUpdate(guildId, _ => setting, (_, _) => setting); } \ No newline at end of file diff --git a/src/NadekoBot/Modules/Utility/UnitConversion/ConverterService.cs b/src/NadekoBot/Modules/Utility/UnitConversion/ConverterService.cs index 6ebc0bbbb..f01144c17 100644 --- a/src/NadekoBot/Modules/Utility/UnitConversion/ConverterService.cs +++ b/src/NadekoBot/Modules/Utility/UnitConversion/ConverterService.cs @@ -11,17 +11,14 @@ public class ConverterService : INService private readonly Timer _currencyUpdater; private readonly TimeSpan _updateInterval = new(12, 0, 0); - private readonly DbService _db; private readonly IDataCache _cache; private readonly IHttpClientFactory _httpFactory; public ConverterService( DiscordSocketClient client, - DbService db, IDataCache cache, IHttpClientFactory factory) { - _db = db; _cache = cache; _httpFactory = factory; diff --git a/src/NadekoBot/Modules/Utility/Utility.cs b/src/NadekoBot/Modules/Utility/Utility.cs index 553d0f2d2..73f43bebd 100644 --- a/src/NadekoBot/Modules/Utility/Utility.cs +++ b/src/NadekoBot/Modules/Utility/Utility.cs @@ -81,7 +81,7 @@ public partial class Utility : NadekoModule if (ctx.Guild is not SocketGuild socketGuild) { - Log.Warning("Can't cast guild to socket guild."); + Log.Warning("Can't cast guild to socket guild"); return; } @@ -154,7 +154,15 @@ public partial class Utility : NadekoModule var builder = new StringBuilder(); var user = who == MeOrBot.Me ? (IGuildUser)ctx.User : ((SocketGuild)ctx.Guild).CurrentUser; var perms = user.GetPermissions((ITextChannel)ctx.Channel); - foreach (var p in perms.GetType().GetProperties().Where(p => !p.GetGetMethod().GetParameters().Any())) + foreach (var p in perms.GetType() + .GetProperties() + .Where(static p => + { + var method = p.GetGetMethod(); + if (method is null) + return false; + return !method.GetParameters().Any(); + })) builder.AppendLine($"{p.Name} : {p.GetValue(perms, null)}"); await SendConfirmAsync(builder.ToString()); } @@ -355,7 +363,10 @@ public partial class Utility : NadekoModule if (page < 0) return; - var guilds = await Task.Run(() => _client.Guilds.OrderBy(g => g.Name).Skip(page * 15).Take(15)); + var guilds = _client.Guilds.OrderBy(g => g.Name) + .Skip(page * 15) + .Take(15) + .ToList(); if (!guilds.Any()) { diff --git a/src/NadekoBot/Modules/Xp/XpService.cs b/src/NadekoBot/Modules/Xp/XpService.cs index a455c716a..bfd069859 100644 --- a/src/NadekoBot/Modules/Xp/XpService.cs +++ b/src/NadekoBot/Modules/Xp/XpService.cs @@ -422,7 +422,7 @@ public class XpService : INService if (socketUser is not SocketGuildUser user || user.IsBot) return Task.CompletedTask; - var _ = Task.Run(() => + _= Task.Run(() => { if (before.VoiceChannel is not null) ScanChannelForVoiceXp(before.VoiceChannel); @@ -514,7 +514,7 @@ public class XpService : INService if (arg.Author is not SocketGuildUser user || user.IsBot) return Task.CompletedTask; - var _ = Task.Run(() => + _= Task.Run(() => { if (!ShouldTrackXp(user, arg.Channel.Id)) return; diff --git a/src/NadekoBot/Services/IEmbedBuilderService.cs b/src/NadekoBot/Services/IEmbedBuilderService.cs index eb766ff02..f345ff28a 100644 --- a/src/NadekoBot/Services/IEmbedBuilderService.cs +++ b/src/NadekoBot/Services/IEmbedBuilderService.cs @@ -26,53 +26,53 @@ public class EmbedBuilderService : IEmbedBuilderService, INService public sealed class DiscordEmbedBuilderWrapper : IEmbedBuilder { private readonly BotConfig _botConfig; - private EmbedBuilder _embed; + private EmbedBuilder embed; public DiscordEmbedBuilderWrapper(in BotConfig botConfig, EmbedBuilder embed = null) { _botConfig = botConfig; - _embed = embed ?? new EmbedBuilder(); + this.embed = embed ?? new EmbedBuilder(); } public IEmbedBuilder WithDescription(string desc) - => Wrap(_embed.WithDescription(desc)); + => Wrap(embed.WithDescription(desc)); public IEmbedBuilder WithTitle(string title) - => Wrap(_embed.WithTitle(title)); + => Wrap(embed.WithTitle(title)); public IEmbedBuilder AddField(string title, object value, bool isInline = false) - => Wrap(_embed.AddField(title, value, isInline)); + => Wrap(embed.AddField(title, value, isInline)); public IEmbedBuilder WithFooter(string text, string iconUrl = null) - => Wrap(_embed.WithFooter(text, iconUrl)); + => Wrap(embed.WithFooter(text, iconUrl)); public IEmbedBuilder WithAuthor(string name, string iconUrl = null, string url = null) - => Wrap(_embed.WithAuthor(name, iconUrl, url)); + => Wrap(embed.WithAuthor(name, iconUrl, url)); public IEmbedBuilder WithUrl(string url) - => Wrap(_embed.WithUrl(url)); + => Wrap(embed.WithUrl(url)); public IEmbedBuilder WithImageUrl(string url) - => Wrap(_embed.WithImageUrl(url)); + => Wrap(embed.WithImageUrl(url)); public IEmbedBuilder WithThumbnailUrl(string url) - => Wrap(_embed.WithThumbnailUrl(url)); + => Wrap(embed.WithThumbnailUrl(url)); public IEmbedBuilder WithColor(EmbedColor color) => color switch { - EmbedColor.Ok => Wrap(_embed.WithColor(_botConfig.Color.Ok.ToDiscordColor())), - EmbedColor.Pending => Wrap(_embed.WithColor(_botConfig.Color.Pending.ToDiscordColor())), - EmbedColor.Error => Wrap(_embed.WithColor(_botConfig.Color.Error.ToDiscordColor())), + EmbedColor.Ok => Wrap(embed.WithColor(_botConfig.Color.Ok.ToDiscordColor())), + EmbedColor.Pending => Wrap(embed.WithColor(_botConfig.Color.Pending.ToDiscordColor())), + EmbedColor.Error => Wrap(embed.WithColor(_botConfig.Color.Error.ToDiscordColor())), _ => throw new ArgumentOutOfRangeException(nameof(color), "Unsupported EmbedColor type") }; public Embed Build() - => _embed.Build(); + => embed.Build(); private IEmbedBuilder Wrap(EmbedBuilder eb) { - _embed = eb; + embed = eb; return this; } } \ No newline at end of file diff --git a/src/NadekoBot/Services/Impl/BehaviorExecutor.cs b/src/NadekoBot/Services/Impl/BehaviorExecutor.cs index d18dca808..cc4c8b195 100644 --- a/src/NadekoBot/Services/Impl/BehaviorExecutor.cs +++ b/src/NadekoBot/Services/Impl/BehaviorExecutor.cs @@ -7,25 +7,25 @@ namespace NadekoBot.Services; public sealed class BehaviorExecutor : IBehaviourExecutor, INService { private readonly IServiceProvider _services; - private IEnumerable _lateExecutors; - private IEnumerable _lateBlockers; - private IEnumerable _earlyBehaviors; - private IEnumerable _transformers; + private IEnumerable lateExecutors; + private IEnumerable lateBlockers; + private IEnumerable earlyBehaviors; + private IEnumerable transformers; public BehaviorExecutor(IServiceProvider services) => _services = services; public void Initialize() { - _lateExecutors = _services.GetServices(); - _lateBlockers = _services.GetServices(); - _earlyBehaviors = _services.GetServices().OrderByDescending(x => x.Priority); - _transformers = _services.GetServices(); + lateExecutors = _services.GetServices(); + lateBlockers = _services.GetServices(); + earlyBehaviors = _services.GetServices().OrderByDescending(x => x.Priority); + transformers = _services.GetServices(); } public async Task RunEarlyBehavioursAsync(SocketGuild guild, IUserMessage usrMsg) { - foreach (var beh in _earlyBehaviors) + foreach (var beh in earlyBehaviors) if (await beh.RunBehavior(guild, usrMsg)) return true; @@ -35,7 +35,7 @@ public sealed class BehaviorExecutor : IBehaviourExecutor, INService public async Task RunInputTransformersAsync(SocketGuild guild, IUserMessage usrMsg) { var messageContent = usrMsg.Content; - foreach (var exec in _transformers) + foreach (var exec in transformers) { string newContent; if ((newContent = await exec.TransformInput(guild, usrMsg.Channel, usrMsg.Author, messageContent)) @@ -51,10 +51,10 @@ public sealed class BehaviorExecutor : IBehaviourExecutor, INService public async Task RunLateBlockersAsync(ICommandContext ctx, CommandInfo cmd) { - foreach (var exec in _lateBlockers) + foreach (var exec in lateBlockers) if (await exec.TryBlockLate(ctx, cmd.Module.GetTopLevelModule().Name, cmd)) { - Log.Information("Late blocking User [{0}] Command: [{1}] in [{2}]", + Log.Information("Late blocking User [{User}] Command: [{Command}] in [{Module}]", ctx.User, cmd.Aliases[0], exec.GetType().Name); @@ -66,7 +66,7 @@ public sealed class BehaviorExecutor : IBehaviourExecutor, INService public async Task RunLateExecutorsAsync(SocketGuild guild, IUserMessage usrMsg) { - foreach (var exec in _lateExecutors) + foreach (var exec in lateExecutors) try { await exec.LateExecute(guild, usrMsg); diff --git a/src/NadekoBot/Services/Impl/GoogleApiService.cs b/src/NadekoBot/Services/Impl/GoogleApiService.cs index 7a742a4bf..c5f9a4c43 100644 --- a/src/NadekoBot/Services/Impl/GoogleApiService.cs +++ b/src/NadekoBot/Services/Impl/GoogleApiService.cs @@ -13,10 +13,10 @@ namespace NadekoBot.Services; public class GoogleApiService : IGoogleApiService, INService { - private const string SearchEngineId = "018084019232060951019:hs5piey28-e"; + private const string SEARCH_ENGINE_ID = "018084019232060951019:hs5piey28-e"; private static readonly Regex - plRegex = new("(?:youtu\\.be\\/|list=)(?[\\da-zA-Z\\-_]*)", RegexOptions.Compiled); + _plRegex = new("(?:youtu\\.be\\/|list=)(?[\\da-zA-Z\\-_]*)", RegexOptions.Compiled); public IReadOnlyDictionary Languages { get; } = new Dictionary { @@ -151,9 +151,9 @@ public class GoogleApiService : IGoogleApiService, INService { "yi", "yi" } }; - private readonly YouTubeService yt; - private readonly UrlshortenerService sh; - private readonly CustomsearchService cs; + private readonly YouTubeService _yt; + private readonly UrlshortenerService _sh; + private readonly CustomsearchService _cs; //private readonly Regex YtVideoIdRegex = new Regex(@"(?:youtube\.com\/\S*(?:(?:\/e(?:mbed))?\/|watch\?(?:\S*?&?v\=))|youtu\.be\/)(?[a-zA-Z0-9_-]{6,11})", RegexOptions.Compiled); private readonly IBotCredentials _creds; @@ -166,9 +166,9 @@ public class GoogleApiService : IGoogleApiService, INService var bcs = new BaseClientService.Initializer { ApplicationName = "Nadeko Bot", ApiKey = _creds.GoogleApiKey }; - yt = new(bcs); - sh = new(bcs); - cs = new(bcs); + _yt = new(bcs); + _sh = new(bcs); + _cs = new(bcs); } public async Task> GetPlaylistIdsByKeywordsAsync(string keywords, int count = 1) @@ -180,9 +180,9 @@ public class GoogleApiService : IGoogleApiService, INService if (count <= 0) throw new ArgumentOutOfRangeException(nameof(count)); - var match = plRegex.Match(keywords); + var match = _plRegex.Match(keywords); if (match.Length > 1) return new[] { match.Groups["id"].Value }; - var query = yt.Search.List("snippet"); + var query = _yt.Search.List("snippet"); query.MaxResults = count; query.Type = "playlist"; query.Q = keywords; @@ -199,7 +199,7 @@ public class GoogleApiService : IGoogleApiService, INService if (count <= 0) throw new ArgumentOutOfRangeException(nameof(count)); - var query = yt.Search.List("snippet"); + var query = _yt.Search.List("snippet"); query.MaxResults = count; query.RelatedToVideoId = id; query.Type = "video"; @@ -215,7 +215,7 @@ public class GoogleApiService : IGoogleApiService, INService if (count <= 0) throw new ArgumentOutOfRangeException(nameof(count)); - var query = yt.Search.List("snippet"); + var query = _yt.Search.List("snippet"); query.MaxResults = count; query.Q = keywords; query.Type = "video"; @@ -234,7 +234,7 @@ public class GoogleApiService : IGoogleApiService, INService if (count <= 0) throw new ArgumentOutOfRangeException(nameof(count)); - var query = yt.Search.List("snippet"); + var query = _yt.Search.List("snippet"); query.MaxResults = count; query.Q = keywords; query.Type = "video"; @@ -256,7 +256,7 @@ public class GoogleApiService : IGoogleApiService, INService try { - var response = await sh.Url.Insert(new() { LongUrl = url }).ExecuteAsync(); + var response = await _sh.Url.Insert(new() { LongUrl = url }).ExecuteAsync(); return response.Id; } catch (GoogleApiException ex) when (ex.HttpStatusCode == HttpStatusCode.Forbidden) @@ -288,7 +288,7 @@ public class GoogleApiService : IGoogleApiService, INService var toGet = count > 50 ? 50 : count; count -= toGet; - var query = yt.PlaylistItems.List("contentDetails"); + var query = _yt.PlaylistItems.List("contentDetails"); query.MaxResults = toGet; query.PlaylistId = playlistId; query.PageToken = nextPageToken; @@ -318,7 +318,7 @@ public class GoogleApiService : IGoogleApiService, INService var toGet = remaining > 50 ? 50 : remaining; remaining -= toGet; - var q = yt.Videos.List("contentDetails"); + var q = _yt.Videos.List("contentDetails"); q.Id = string.Join(",", videoIdsList.Take(toGet)); videoIdsList = videoIdsList.Skip(toGet).ToList(); var items = (await q.ExecuteAsync()).Items; @@ -334,9 +334,9 @@ public class GoogleApiService : IGoogleApiService, INService if (string.IsNullOrWhiteSpace(query)) throw new ArgumentNullException(nameof(query)); - var req = cs.Cse.List(); + var req = _cs.Cse.List(); req.Q = query; - req.Cx = SearchEngineId; + req.Cx = SEARCH_ENGINE_ID; req.Num = 1; req.Fields = "items(image(contextLink,thumbnailLink),link)"; req.SearchType = CseResource.ListRequest.SearchTypeEnum.Image; diff --git a/src/NadekoBot/Services/Impl/RedisCache.cs b/src/NadekoBot/Services/Impl/RedisCache.cs index 8ebc0cf8a..869323154 100644 --- a/src/NadekoBot/Services/Impl/RedisCache.cs +++ b/src/NadekoBot/Services/Impl/RedisCache.cs @@ -15,7 +15,7 @@ public class RedisCache : IDataCache private readonly string _redisKey; private readonly EndPoint _redisEndpoint; - private readonly object timelyLock = new(); + private readonly object _timelyLock = new(); public RedisCache( ConnectionMultiplexer redis, @@ -36,77 +36,77 @@ public class RedisCache : IDataCache // can re-use the same image/anime data public async Task<(bool Success, byte[] Data)> TryGetImageDataAsync(Uri key) { - var _db = Redis.GetDatabase(); - byte[] x = await _db.StringGetAsync("image_" + key); + var db = Redis.GetDatabase(); + byte[] x = await db.StringGetAsync("image_" + key); return (x is not null, x); } public Task SetImageDataAsync(Uri key, byte[] data) { - var _db = Redis.GetDatabase(); - return _db.StringSetAsync("image_" + key, data); + var db = Redis.GetDatabase(); + return db.StringSetAsync("image_" + key, data); } public async Task<(bool Success, string Data)> TryGetAnimeDataAsync(string key) { - var _db = Redis.GetDatabase(); - string x = await _db.StringGetAsync("anime_" + key); + var db = Redis.GetDatabase(); + string x = await db.StringGetAsync("anime_" + key); return (x is not null, x); } public Task SetAnimeDataAsync(string key, string data) { - var _db = Redis.GetDatabase(); - return _db.StringSetAsync("anime_" + key, data, TimeSpan.FromHours(3)); + var db = Redis.GetDatabase(); + return db.StringSetAsync("anime_" + key, data, TimeSpan.FromHours(3)); } public async Task<(bool Success, string Data)> TryGetNovelDataAsync(string key) { - var _db = Redis.GetDatabase(); - string x = await _db.StringGetAsync("novel_" + key); + var db = Redis.GetDatabase(); + string x = await db.StringGetAsync("novel_" + key); return (x is not null, x); } public Task SetNovelDataAsync(string key, string data) { - var _db = Redis.GetDatabase(); - return _db.StringSetAsync("novel_" + key, data, TimeSpan.FromHours(3)); + var db = Redis.GetDatabase(); + return db.StringSetAsync("novel_" + key, data, TimeSpan.FromHours(3)); } public TimeSpan? AddTimelyClaim(ulong id, int period) { if (period == 0) return null; - lock (timelyLock) + lock (_timelyLock) { var time = TimeSpan.FromHours(period); - var _db = Redis.GetDatabase(); - if ((bool?)_db.StringGet($"{_redisKey}_timelyclaim_{id}") is null) + var db = Redis.GetDatabase(); + if ((bool?)db.StringGet($"{_redisKey}_timelyclaim_{id}") is null) { - _db.StringSet($"{_redisKey}_timelyclaim_{id}", true, time); + db.StringSet($"{_redisKey}_timelyclaim_{id}", true, time); return null; } - return _db.KeyTimeToLive($"{_redisKey}_timelyclaim_{id}"); + return db.KeyTimeToLive($"{_redisKey}_timelyclaim_{id}"); } } public void RemoveAllTimelyClaims() { var server = Redis.GetServer(_redisEndpoint); - var _db = Redis.GetDatabase(); + var db = Redis.GetDatabase(); foreach (var k in server.Keys(pattern: $"{_redisKey}_timelyclaim_*")) - _db.KeyDelete(k, CommandFlags.FireAndForget); + db.KeyDelete(k, CommandFlags.FireAndForget); } public bool TryAddAffinityCooldown(ulong userId, out TimeSpan? time) { - var _db = Redis.GetDatabase(); - time = _db.KeyTimeToLive($"{_redisKey}_affinity_{userId}"); + var db = Redis.GetDatabase(); + time = db.KeyTimeToLive($"{_redisKey}_affinity_{userId}"); if (time is null) { time = TimeSpan.FromMinutes(30); - _db.StringSet($"{_redisKey}_affinity_{userId}", true, time); + db.StringSet($"{_redisKey}_affinity_{userId}", true, time); return true; } @@ -115,12 +115,12 @@ public class RedisCache : IDataCache public bool TryAddDivorceCooldown(ulong userId, out TimeSpan? time) { - var _db = Redis.GetDatabase(); - time = _db.KeyTimeToLive($"{_redisKey}_divorce_{userId}"); + var db = Redis.GetDatabase(); + time = db.KeyTimeToLive($"{_redisKey}_divorce_{userId}"); if (time is null) { time = TimeSpan.FromHours(6); - _db.StringSet($"{_redisKey}_divorce_{userId}", true, time); + db.StringSet($"{_redisKey}_divorce_{userId}", true, time); return true; } @@ -129,42 +129,43 @@ public class RedisCache : IDataCache public Task SetStreamDataAsync(string url, string data) { - var _db = Redis.GetDatabase(); - return _db.StringSetAsync($"{_redisKey}_stream_{url}", data, TimeSpan.FromHours(6)); + var db = Redis.GetDatabase(); + return db.StringSetAsync($"{_redisKey}_stream_{url}", data, TimeSpan.FromHours(6)); } public bool TryGetStreamData(string url, out string dataStr) { - var _db = Redis.GetDatabase(); - dataStr = _db.StringGet($"{_redisKey}_stream_{url}"); + var db = Redis.GetDatabase(); + dataStr = db.StringGet($"{_redisKey}_stream_{url}"); return !string.IsNullOrWhiteSpace(dataStr); } public TimeSpan? TryAddRatelimit(ulong id, string name, int expireIn) { - var _db = Redis.GetDatabase(); - if (_db.StringSet($"{_redisKey}_ratelimit_{id}_{name}", + var db = Redis.GetDatabase(); + if (db.StringSet($"{_redisKey}_ratelimit_{id}_{name}", 0, // i don't use the value TimeSpan.FromSeconds(expireIn), When.NotExists)) return null; - return _db.KeyTimeToLive($"{_redisKey}_ratelimit_{id}_{name}"); + return db.KeyTimeToLive($"{_redisKey}_ratelimit_{id}_{name}"); } public bool TryGetEconomy(out string data) { - var _db = Redis.GetDatabase(); - if ((data = _db.StringGet($"{_redisKey}_economy")) is not null) return true; + var db = Redis.GetDatabase(); + data = db.StringGet($"{_redisKey}_economy"); + if (data is not null) return true; return false; } public void SetEconomy(string data) { - var _db = Redis.GetDatabase(); - _db.StringSet($"{_redisKey}_economy", data, TimeSpan.FromMinutes(3)); + var db = Redis.GetDatabase(); + db.StringSet($"{_redisKey}_economy", data, TimeSpan.FromMinutes(3)); } public async Task GetOrAddCachedDataAsync( @@ -174,9 +175,9 @@ public class RedisCache : IDataCache TimeSpan expiry) where TOut : class { - var _db = Redis.GetDatabase(); + var db = Redis.GetDatabase(); - var data = await _db.StringGetAsync(key); + var data = await db.StringGetAsync(key); if (!data.HasValue) { var obj = await factory(param); @@ -184,7 +185,7 @@ public class RedisCache : IDataCache if (obj is null) return default; - await _db.StringSetAsync(key, JsonConvert.SerializeObject(obj), expiry); + await db.StringSetAsync(key, JsonConvert.SerializeObject(obj), expiry); return obj; } diff --git a/src/NadekoBot/Services/Impl/RedisImageExtensions.cs b/src/NadekoBot/Services/Impl/RedisImageExtensions.cs index 138935c32..4cd31f9c6 100644 --- a/src/NadekoBot/Services/Impl/RedisImageExtensions.cs +++ b/src/NadekoBot/Services/Impl/RedisImageExtensions.cs @@ -3,9 +3,9 @@ namespace NadekoBot.Services; public static class RedisImageExtensions { - private const string OldCdnUrl = "nadeko-pictures.nyc3.digitaloceanspaces.com"; - private const string NewCdnUrl = "cdn.nadeko.bot"; + private const string OLD_CDN_URL = "nadeko-pictures.nyc3.digitaloceanspaces.com"; + private const string NEW_CDN_URL = "cdn.nadeko.bot"; public static Uri ToNewCdn(this Uri uri) - => new(uri.ToString().Replace(OldCdnUrl, NewCdnUrl)); + => new(uri.ToString().Replace(OLD_CDN_URL, NEW_CDN_URL)); } \ No newline at end of file diff --git a/src/NadekoBot/Services/Impl/StatsService.cs b/src/NadekoBot/Services/Impl/StatsService.cs index ffca0ca53..9f50e89ce 100644 --- a/src/NadekoBot/Services/Impl/StatsService.cs +++ b/src/NadekoBot/Services/Impl/StatsService.cs @@ -58,7 +58,7 @@ public class StatsService : IStatsService, IReadyExecutor, INService, IDisposabl _client.ChannelCreated += c => { - var _ = Task.Run(() => + _= Task.Run(() => { if (c is ITextChannel) Interlocked.Increment(ref textChannels); @@ -71,7 +71,7 @@ public class StatsService : IStatsService, IReadyExecutor, INService, IDisposabl _client.ChannelDestroyed += c => { - var _ = Task.Run(() => + _= Task.Run(() => { if (c is ITextChannel) Interlocked.Decrement(ref textChannels); @@ -84,7 +84,7 @@ public class StatsService : IStatsService, IReadyExecutor, INService, IDisposabl _client.GuildAvailable += g => { - var _ = Task.Run(() => + _= Task.Run(() => { var tc = g.Channels.Count(cx => cx is ITextChannel); var vc = g.Channels.Count - tc; @@ -96,7 +96,7 @@ public class StatsService : IStatsService, IReadyExecutor, INService, IDisposabl _client.JoinedGuild += g => { - var _ = Task.Run(() => + _= Task.Run(() => { var tc = g.Channels.Count(cx => cx is ITextChannel); var vc = g.Channels.Count - tc; @@ -108,7 +108,7 @@ public class StatsService : IStatsService, IReadyExecutor, INService, IDisposabl _client.GuildUnavailable += g => { - var _ = Task.Run(() => + _= Task.Run(() => { var tc = g.Channels.Count(cx => cx is ITextChannel); var vc = g.Channels.Count - tc; @@ -121,7 +121,7 @@ public class StatsService : IStatsService, IReadyExecutor, INService, IDisposabl _client.LeftGuild += g => { - var _ = Task.Run(() => + _= Task.Run(() => { var tc = g.Channels.Count(cx => cx is ITextChannel); var vc = g.Channels.Count - tc; diff --git a/src/NadekoBot/Services/Impl/YtdlOperation.cs b/src/NadekoBot/Services/Impl/YtdlOperation.cs index eaeebe49a..886bbf5f5 100644 --- a/src/NadekoBot/Services/Impl/YtdlOperation.cs +++ b/src/NadekoBot/Services/Impl/YtdlOperation.cs @@ -14,13 +14,13 @@ public class YtdlOperation private Process CreateProcess(string[] args) { - args = args.Map(arg => arg.Replace("\"", "")); + var newArgs = args.Map(arg => (object)arg.Replace("\"", "")); return new() { StartInfo = new() { FileName = "youtube-dl", - Arguments = string.Format(_baseArgString, args), + Arguments = string.Format(_baseArgString, newArgs), UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true, diff --git a/src/NadekoBot/Services/Settings/BotConfigService.cs b/src/NadekoBot/Services/Settings/BotConfigService.cs index 0570d4a3b..d77f20775 100644 --- a/src/NadekoBot/Services/Settings/BotConfigService.cs +++ b/src/NadekoBot/Services/Settings/BotConfigService.cs @@ -9,12 +9,12 @@ namespace NadekoBot.Services; /// public sealed class BotConfigService : ConfigServiceBase { - private const string FilePath = "data/bot.yml"; - private static readonly TypedKey changeKey = new("config.bot.updated"); + private const string FILE_PATH = "data/bot.yml"; + private static readonly TypedKey _changeKey = new("config.bot.updated"); public override string Name { get; } = "bot"; public BotConfigService(IConfigSeria serializer, IPubSub pubSub) - : base(FilePath, serializer, pubSub, changeKey) + : base(FILE_PATH, serializer, pubSub, _changeKey) { AddParsedProp("color.ok", bs => bs.Color.Ok, Rgba32.TryParseHex, ConfigPrinters.Color); AddParsedProp("color.error", bs => bs.Color.Error, Rgba32.TryParseHex, ConfigPrinters.Color); diff --git a/src/NadekoBot/_Extensions/Extensions.cs b/src/NadekoBot/_Extensions/Extensions.cs index ffbbe4420..de2a8a70c 100644 --- a/src/NadekoBot/_Extensions/Extensions.cs +++ b/src/NadekoBot/_Extensions/Extensions.cs @@ -112,11 +112,11 @@ public static class Extensions var wrap = new ReactionEventWrapper(client, msg); wrap.OnReactionAdded += r => { - var _ = Task.Run(() => reactionAdded(r)); + _= Task.Run(() => reactionAdded(r)); }; wrap.OnReactionRemoved += r => { - var _ = Task.Run(() => reactionRemoved(r)); + _= Task.Run(() => reactionRemoved(r)); }; return wrap; } diff --git a/src/NadekoBot/_Extensions/StringExtensions.cs b/src/NadekoBot/_Extensions/StringExtensions.cs index 9d91a38bb..80a717a26 100644 --- a/src/NadekoBot/_Extensions/StringExtensions.cs +++ b/src/NadekoBot/_Extensions/StringExtensions.cs @@ -1,6 +1,5 @@ using NadekoBot.Common.Yml; using Newtonsoft.Json; -using System.Diagnostics.CodeAnalysis; using System.Text; using System.Text.RegularExpressions; diff --git a/src/ayu/Ayu.Discord.Voice/LibOpus.cs b/src/ayu/Ayu.Discord.Voice/LibOpus.cs index b0eb740b5..cab50de88 100644 --- a/src/ayu/Ayu.Discord.Voice/LibOpus.cs +++ b/src/ayu/Ayu.Discord.Voice/LibOpus.cs @@ -64,9 +64,7 @@ namespace Ayu.Discord.Voice } public int SetControl(OpusCtl ctl, int value) - { - return LibOpus.EncoderCtl(_encoderPtr, ctl, value); - } + => LibOpus.EncoderCtl(_encoderPtr, ctl, value); public int Encode(Span input, byte[] output) { @@ -84,9 +82,7 @@ namespace Ayu.Discord.Voice public void Dispose() - { - LibOpus.DestroyEncoder(_encoderPtr); - } + => LibOpus.DestroyEncoder(_encoderPtr); } public enum OpusCtl diff --git a/src/ayu/Ayu.Discord.Voice/PoopyBufferImmortalized.cs b/src/ayu/Ayu.Discord.Voice/PoopyBufferImmortalized.cs index 34e300ed2..39bbb2dff 100644 --- a/src/ayu/Ayu.Discord.Voice/PoopyBufferImmortalized.cs +++ b/src/ayu/Ayu.Discord.Voice/PoopyBufferImmortalized.cs @@ -8,7 +8,6 @@ namespace Ayu.Discord.Voice { public sealed class PoopyBufferImmortalized : ISongBuffer { - private readonly int _frameSize; private readonly byte[] _buffer; private readonly byte[] _outputArray; private CancellationToken _cancellationToken; @@ -27,7 +26,6 @@ namespace Ayu.Discord.Voice public PoopyBufferImmortalized(int frameSize) { - _frameSize = frameSize; _buffer = ArrayPool.Shared.Rent(1_000_000); _outputArray = new byte[frameSize]; @@ -36,9 +34,7 @@ namespace Ayu.Discord.Voice } public void Stop() - { - _isStopped = true; - } + => _isStopped = true; // this method needs a rewrite public Task BufferAsync(ITrackDataSource source, CancellationToken cancellationToken) @@ -129,9 +125,7 @@ namespace Ayu.Discord.Voice } public void Dispose() - { - ArrayPool.Shared.Return(_buffer); - } + => ArrayPool.Shared.Return(_buffer); public void Reset() { diff --git a/src/ayu/Ayu.Discord.Voice/SocketClient.cs b/src/ayu/Ayu.Discord.Voice/SocketClient.cs index 093af50fa..f4d6713a6 100644 --- a/src/ayu/Ayu.Discord.Voice/SocketClient.cs +++ b/src/ayu/Ayu.Discord.Voice/SocketClient.cs @@ -9,7 +9,7 @@ namespace Ayu.Discord.Gateway { public class SocketClient : IDisposable { - private ClientWebSocket? _ws = null; + private ClientWebSocket? _ws; public event Func? PayloadReceived = delegate { return Task.CompletedTask; }; public event Func? WebsocketClosed = delegate { return Task.CompletedTask; }; diff --git a/src/ayu/Ayu.Discord.Voice/VoiceClient.cs b/src/ayu/Ayu.Discord.Voice/VoiceClient.cs index 86c9667ca..58d143de1 100644 --- a/src/ayu/Ayu.Discord.Voice/VoiceClient.cs +++ b/src/ayu/Ayu.Discord.Voice/VoiceClient.cs @@ -163,9 +163,7 @@ namespace Ayu.Discord.Voice } public void Dispose() - { - Encoder.Dispose(); - } + => Encoder.Dispose(); } public enum SendPcmError diff --git a/src/ayu/Ayu.Discord.Voice/VoiceGateway.cs b/src/ayu/Ayu.Discord.Voice/VoiceGateway.cs index cca0935b1..51d9b11a9 100644 --- a/src/ayu/Ayu.Discord.Voice/VoiceGateway.cs +++ b/src/ayu/Ayu.Discord.Voice/VoiceGateway.cs @@ -55,7 +55,7 @@ namespace Ayu.Discord.Voice public uint Timestamp { get; set; } public string MyIp { get; private set; } = string.Empty; public ushort MyPort { get; private set; } - private bool shouldResume = false; + private bool _shouldResume; private readonly CancellationTokenSource _stopCancellationSource; private readonly CancellationToken _stopCancellationToken; @@ -123,6 +123,8 @@ namespace Ayu.Discord.Voice private async Task _ws_PayloadReceived(byte[] arg) { var payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(arg)); + if (payload is null) + return; try { //Log.Information("Received payload with opcode {OpCode}", payload.OpCode); @@ -138,7 +140,7 @@ namespace Ayu.Discord.Voice case VoiceOpCode.Ready: var ready = payload.Data.ToObject(); await HandleReadyAsync(ready!); - shouldResume = true; + _shouldResume = true; break; case VoiceOpCode.Heartbeat: // sent, not received @@ -161,7 +163,7 @@ namespace Ayu.Discord.Voice await HandleHelloAsync(hello!); break; case VoiceOpCode.Resumed: - shouldResume = true; + _shouldResume = true; break; case VoiceOpCode.ClientDisconnect: break; @@ -183,7 +185,7 @@ namespace Ayu.Discord.Voice hbt?.Change(Timeout.Infinite, Timeout.Infinite); _heartbeatTimer = null; - if (!_stopCancellationToken.IsCancellationRequested && shouldResume) + if (!_stopCancellationToken.IsCancellationRequested && _shouldResume) { _ = _ws.RunAndBlockAsync(_websocketUrl, _stopCancellationToken); return Task.CompletedTask; @@ -199,9 +201,7 @@ namespace Ayu.Discord.Voice } public void SendRtpData(byte[] rtpData, int length) - { - _udpClient.Send(rtpData, length, _udpEp); - } + => _udpClient.Send(rtpData, length, _udpEp); private Task HandleSessionDescription(VoiceSessionDescription sd) { @@ -215,7 +215,7 @@ namespace Ayu.Discord.Voice private Task ResumeAsync() { - shouldResume = false; + _shouldResume = false; return SendCommandPayloadAsync(new() { OpCode = VoiceOpCode.Resume, @@ -270,7 +270,7 @@ namespace Ayu.Discord.Voice await SendHeartbeatAsync(); }, default, data.HeartbeatInterval, data.HeartbeatInterval); - if (shouldResume) + if (_shouldResume) { return ResumeAsync(); } @@ -341,7 +341,7 @@ namespace Ayu.Discord.Voice public Task StopAsync() { Started = false; - shouldResume = false; + _shouldResume = false; if(!_stopCancellationSource.IsCancellationRequested) try { _stopCancellationSource.Cancel(); } catch { } return _ws.CloseAsync("Stopped by the user.");