Skip to content

Commit

Permalink
BRGD-277 Ignore Bot Messages (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
talenfisher committed Apr 18, 2023
1 parent 4c5babc commit e969347
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Expand Up @@ -3,6 +3,10 @@ root = true
[*.{cs,csx}]
insert_final_newline = true

# Allow one-liner if-returns
dotnet_diagnostic.SA1503.severity = none
dotnet_diagnostic.SA1137.severity = error

# Namespace must match folder structure
dotnet_diagnostic.IDE0130.severity = none

Expand Down
4 changes: 2 additions & 2 deletions cicd/template.yml
Expand Up @@ -231,8 +231,8 @@ Resources:
TaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Cpu: "512"
Memory: "1024"
Cpu: "256"
Memory: "512"
Family: !Ref AWS::StackName
NetworkMode: awsvpc
ExecutionRoleArn: !GetAtt TaskRole.Arn
Expand Down
Expand Up @@ -16,6 +16,8 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

#pragma warning disable IDE0011 // Relax required braces

namespace Brighid.Discord.Adapter.Events
{
/// <summary>
Expand Down Expand Up @@ -78,6 +80,7 @@ public async Task Handle(MessageCreateEvent @event, CancellationToken cancellati
{
cancellationToken.ThrowIfCancellationRequested();
gateway.ThrowIfNotReady();
if (@event.IsBotMessage) return;

using var trace = tracingService.StartTrace();
using var scope = logger.BeginScope("{@Event} {@TraceId}", nameof(MessageCreateEvent), trace.Id);
Expand Down
5 changes: 5 additions & 0 deletions src/Adapter/Events/Events/MessageCreateEvent.cs
Expand Up @@ -15,5 +15,10 @@ public struct MessageCreateEvent : IGatewayEvent, IMessageEvent
/// Gets or sets the event message.
/// </summary>
public Message Message { get; set; }

/// <summary>
/// Gets a value indicating whether the message originates from a bot user or not.
/// </summary>
public bool IsBotMessage => Message.Author.IsBot ?? false;
}
}
Expand Up @@ -69,6 +69,27 @@ public class HandleTests
gateway.Received().ThrowIfNotReady();
}

[Test, Auto]
public async Task ShouldDoNothingIfMessageWasFromABot(
string content,
[Frozen, Substitute] IGatewayService gateway,
[Frozen, Substitute] ITracingService tracing,
[Frozen, Substitute] IMessageEmitter emitter,
[Frozen, Substitute] IUserService userService,
[Target] MessageCreateEventController controller
)
{
var cancellationToken = new CancellationToken(false);
var message = new Message { Content = content, Author = new User { IsBot = true } };
var @event = new MessageCreateEvent { Message = message };

await controller.Handle(@event, cancellationToken);

tracing.DidNotReceive().StartTrace();
await emitter.DidNotReceive().Emit(Any<Message>(), Any<Snowflake>(), Any<CancellationToken>());
await userService.DidNotReceive().IsUserRegistered(Any<User>(), Any<CancellationToken>());
}

[Test, Auto]
public async Task ShouldStartAndStopATraceWithMessageAndEventAnnotations(
string content,
Expand Down

0 comments on commit e969347

Please sign in to comment.