diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 4bd4390..36ae7a7 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -1,4 +1,7 @@ name: "CodeQL" +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true on: push: @@ -10,7 +13,7 @@ on: jobs: analyze: - name: Analyze + name: Analyze code runs-on: ubuntu-latest permissions: actions: read @@ -26,19 +29,16 @@ jobs: - name: Checkout repository uses: actions/checkout@v3 - # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL uses: github/codeql-action/init@v2 with: languages: ${{ matrix.language }} queries: +security-extended,security-and-quality - # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). - # If this step fails, then you should remove it and run the build manually - - name: Autobuild + - name: Build solution uses: github/codeql-action/autobuild@v2 - - name: Perform CodeQL Analysis + - name: Perform CodeQL analysis uses: github/codeql-action/analyze@v2 with: category: "/language:${{matrix.language}}" diff --git a/.github/workflows/resharper.yml b/.github/workflows/resharper.yml new file mode 100644 index 0000000..8c55f0f --- /dev/null +++ b/.github/workflows/resharper.yml @@ -0,0 +1,36 @@ +name: "ReSharper" +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + inspect-code: + name: Inspect code + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + + strategy: + fail-fast: false + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Restore dependencies and tools + run: dotnet restore + + - name: ReSharper CLI InspectCode + uses: muno92/resharper_inspectcode@1.6.0 + with: + solutionPath: ./Boyfriend-CSharp.sln + ignoreIssueType: InvertIf + solutionWideAnalysis: true diff --git a/Boyfriend/Boyfriend.cs b/Boyfriend/Boyfriend.cs index 839b1e8..837b962 100644 --- a/Boyfriend/Boyfriend.cs +++ b/Boyfriend/Boyfriend.cs @@ -60,7 +60,7 @@ public static class Boyfriend { private static async Task Init() { var token = (await File.ReadAllTextAsync("token.txt")).Trim(); - Client.Log += x => Log(x); + Client.Log += Log; await Client.LoginAsync(TokenType.Bot, token); await Client.StartAsync(); @@ -151,4 +151,3 @@ public static class Boyfriend { return removedRoles; } } - diff --git a/Boyfriend/CommandProcessor.cs b/Boyfriend/CommandProcessor.cs index 574ad2a..ab077ef 100644 --- a/Boyfriend/CommandProcessor.cs +++ b/Boyfriend/CommandProcessor.cs @@ -73,7 +73,8 @@ public sealed class CommandProcessor { } public void Reply(string response, string? customEmoji = null) { - Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{customEmoji ?? ReplyEmojis.Success} {response}", Context.Message); + Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{customEmoji ?? ReplyEmojis.Success} {response}", + Context.Message); } public void Audit(string action, bool isPublic = true) { @@ -127,17 +128,21 @@ public sealed class CommandProcessor { public bool HasPermission(GuildPermission permission) { if (!Context.Guild.CurrentUser.GuildPermissions.Has(permission)) { - Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{ReplyEmojis.NoPermission} {Utils.GetMessage($"BotCannot{permission}")}", + Utils.SafeAppendToBuilder(_stackedReplyMessage, + $"{ReplyEmojis.NoPermission} {Utils.GetMessage($"BotCannot{permission}")}", Context.Message); return false; } - if (Context.Guild.GetUser(Context.User.Id).GuildPermissions.Has(permission) - || Context.Guild.OwnerId == Context.User.Id) return true; + if (!Context.Guild.GetUser(Context.User.Id).GuildPermissions.Has(permission) + && Context.Guild.OwnerId != Context.User.Id) { + Utils.SafeAppendToBuilder(_stackedReplyMessage, + $"{ReplyEmojis.NoPermission} {Utils.GetMessage($"UserCannot{permission}")}", + Context.Message); + return false; + } - Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{ReplyEmojis.NoPermission} {Utils.GetMessage($"UserCannot{permission}")}", - Context.Message); - return false; + return true; } public SocketGuildUser? GetMember(SocketUser user) { @@ -202,11 +207,14 @@ public sealed class CommandProcessor { return null; } - if (i <= max) return i; - Utils.SafeAppendToBuilder(_stackedReplyMessage, - $"{ReplyEmojis.InvalidArgument} {string.Format(Utils.GetMessage($"{argument}TooLarge"), max.ToString())}", - Context.Message); - return null; + if (i > max) { + Utils.SafeAppendToBuilder(_stackedReplyMessage, + $"{ReplyEmojis.InvalidArgument} {string.Format(Utils.GetMessage($"{argument}TooLarge"), max.ToString())}", + Context.Message); + return null; + } + + return i; } public static TimeSpan GetTimeSpan(string[] args, int index) { @@ -268,9 +276,12 @@ public sealed class CommandProcessor { return false; } - if (Context.Guild.Owner.Id == Context.User.Id || GetMember().Hierarchy > user.Hierarchy) return true; - Utils.SafeAppendToBuilder(_stackedReplyMessage, - $"{ReplyEmojis.CantInteract} {Utils.GetMessage($"UserCannot{action}Target")}", Context.Message); - return false; + if (Context.Guild.Owner.Id != Context.User.Id && GetMember().Hierarchy <= user.Hierarchy) { + Utils.SafeAppendToBuilder(_stackedReplyMessage, + $"{ReplyEmojis.CantInteract} {Utils.GetMessage($"UserCannot{action}Target")}", Context.Message); + return false; + } + + return true; } } diff --git a/Boyfriend/EventHandler.cs b/Boyfriend/EventHandler.cs index f23b34a..9f5af5f 100644 --- a/Boyfriend/EventHandler.cs +++ b/Boyfriend/EventHandler.cs @@ -1,4 +1,3 @@ -using System.Diagnostics.CodeAnalysis; using Discord; using Discord.Rest; using Discord.WebSocket; @@ -9,17 +8,16 @@ public static class EventHandler { private static readonly DiscordSocketClient Client = Boyfriend.Client; private static bool _sendReadyMessages = true; - [SuppressMessage("ReSharper", "ConvertClosureToMethodGroup")] public static void InitEvents() { - Client.Ready += () => ReadyEvent(); - Client.MessageDeleted += (x, y) => MessageDeletedEvent(x, y); - Client.MessageReceived += x => MessageReceivedEvent(x); - Client.MessageUpdated += (x, y, z) => MessageUpdatedEvent(x, y, z); - Client.UserJoined += x => UserJoinedEvent(x); - Client.GuildScheduledEventCreated += x => ScheduledEventCreatedEvent(x); - Client.GuildScheduledEventCancelled += x => ScheduledEventCancelledEvent(x); - Client.GuildScheduledEventStarted += x => ScheduledEventStartedEvent(x); - Client.GuildScheduledEventCompleted += x => ScheduledEventCompletedEvent(x); + Client.Ready += ReadyEvent; + Client.MessageDeleted += MessageDeletedEvent; + Client.MessageReceived += MessageReceivedEvent; + Client.MessageUpdated += MessageUpdatedEvent; + Client.UserJoined += UserJoinedEvent; + Client.GuildScheduledEventCreated += ScheduledEventCreatedEvent; + Client.GuildScheduledEventCancelled += ScheduledEventCancelledEvent; + Client.GuildScheduledEventStarted += ScheduledEventStartedEvent; + Client.GuildScheduledEventCompleted += ScheduledEventCompletedEvent; } private static Task ReadyEvent() { @@ -174,5 +172,4 @@ public static class EventHandler { await channel.SendMessageAsync(string.Format(Messages.EventCompleted, Utils.Wrap(scheduledEvent.Name), Utils.GetHumanizedTimeOffset(DateTimeOffset.Now.Subtract(scheduledEvent.StartTime)))); } -} - +} \ No newline at end of file