public async Task ShouldExecuteCommandButNotReplyIfShouldNotReplyImmediately(
                string content,
                Snowflake userId,
                Snowflake channelId,
                UserId identityUserId,
                [Frozen] Command command,
                [Frozen] ExecuteCommandResponse executeCommandResponse,
                [Frozen, Substitute] IMessageEmitter emitter,
                [Frozen, Substitute] IBrighidCommandsService commandsClient,
                [Frozen, Substitute] IDiscordChannelClient channelClient,
                [Frozen, Substitute] IUserService userService,
                [Target] MessageCreateEventController controller
                )
            {
                var cancellationToken = new CancellationToken(false);
                var author            = new User {
                    Id = userId
                };
                var message = new Message {
                    Content = content, Author = author, ChannelId = channelId
                };
                var @event = new MessageCreateEvent {
                    Message = message
                };

                executeCommandResponse.ReplyImmediately = false;
                userService.GetIdentityServiceUserId(Any <User>(), Any <CancellationToken>()).Returns(identityUserId);
                userService.IsUserRegistered(Any <User>(), Any <CancellationToken>()).Returns(true);

                await controller.Handle(@event, cancellationToken);

                await channelClient.DidNotReceive().CreateMessage(Is(channelId), Is <CreateMessagePayload>(payload => payload.Content == executeCommandResponse.Response), Is(cancellationToken));
            }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MessageCreateEventController" /> class.
 /// </summary>
 /// <param name="tracingService">Service for doing traces.</param>
 /// <param name="userService">Service to manage users with.</param>
 /// <param name="emitter">Emitter to emit messages to.</param>
 /// <param name="discordUserClient">Client used to send User API requests to Discord.</param>
 /// <param name="discordChannelClient">Client used to send Channel API requests to Discord.</param>
 /// <param name="strings">Localizer service for retrieving strings.</param>
 /// <param name="adapterOptions">Options to use for the adapter.</param>
 /// <param name="commandsService">Client for interacting with the commands service.</param>
 /// <param name="gateway">Gateway that discord sends events through.</param>
 /// <param name="reporter">Reporter to report metrics to.</param>
 /// <param name="logger">Logger used to log information to some destination(s).</param>
 public MessageCreateEventController(
     ITracingService tracingService,
     IUserService userService,
     IMessageEmitter emitter,
     IDiscordUserClient discordUserClient,
     IDiscordChannelClient discordChannelClient,
     IStringLocalizer <Strings> strings,
     IOptions <AdapterOptions> adapterOptions,
     IBrighidCommandsService commandsService,
     IGatewayService gateway,
     IMetricReporter reporter,
     ILogger <MessageCreateEventController> logger
     )
 {
     this.tracingService       = tracingService;
     this.userService          = userService;
     this.emitter              = emitter;
     this.discordUserClient    = discordUserClient;
     this.discordChannelClient = discordChannelClient;
     this.strings              = strings;
     this.adapterOptions       = adapterOptions.Value;
     this.commandsService      = commandsService;
     this.gateway              = gateway;
     this.reporter             = reporter;
     this.logger = logger;
 }
            public async Task ShouldReplyWithTraceIdIfUserHasDebugModeEnabled(
                string content,
                Snowflake userId,
                Snowflake channelId,
                [Frozen] Command command,
                [Frozen] TraceContext traceContext,
                [Frozen] ExecuteCommandResponse executeCommandResponse,
                [Frozen, Substitute] IMessageEmitter emitter,
                [Frozen, Substitute] IBrighidCommandsService commandsClient,
                [Frozen, Substitute] IDiscordChannelClient channelClient,
                [Frozen, Substitute] IUserService userService,
                [Target] MessageCreateEventController controller
                )
            {
                var cancellationToken = new CancellationToken(false);
                var author            = new User {
                    Id = userId
                };
                var message = new Message {
                    Content = content, Author = author, ChannelId = channelId
                };
                var @event = new MessageCreateEvent {
                    Message = message
                };
                var identityUserId = new UserId(Guid.NewGuid(), true, true);

                executeCommandResponse.ReplyImmediately = true;
                userService.GetIdentityServiceUserId(Any <User>(), Any <CancellationToken>()).Returns(identityUserId);
                userService.IsUserRegistered(Any <User>(), Any <CancellationToken>()).Returns(true);

                await controller.Handle(@event, cancellationToken);

                await channelClient.Received().CreateMessage(
                    Is(channelId),
                    Is <CreateMessagePayload>(payload =>
                                              payload.Embed !.Value.Fields.Any(field => field.Name == "TraceId" && field.Value == traceContext.Id)
                                              ),
                    Is(cancellationToken)
                    );
            }
            public async Task ShouldSendGreetingToUserViaDmsIfNotRegisteredAndWasMentioned(
                string content,
                Snowflake userId,
                Snowflake botId,
                Snowflake channelId,
                [Frozen] Channel channel,
                [Frozen] AdapterOptions options,
                [Frozen] IStringLocalizer <Strings> strings,
                [Frozen, Substitute] IDiscordUserClient userClient,
                [Frozen, Substitute] IDiscordChannelClient channelClient,
                [Frozen, Substitute] IGatewayService gateway,
                [Frozen, Substitute] IMessageEmitter emitter,
                [Frozen, Substitute] IUserService userService,
                [Target] MessageCreateEventController controller
                )
            {
                var cancellationToken = new CancellationToken(false);
                var author            = new User {
                    Id = userId
                };
                var mention = new UserMention {
                    Id = botId
                };
                var message = new Message {
                    Content = content, Author = author, ChannelId = channelId, Mentions = new[] { mention }
                };
                var @event = new MessageCreateEvent {
                    Message = message
                };

                gateway.BotId = botId;
                userService.IsUserRegistered(Any <User>(), Any <CancellationToken>()).Returns(false);

                await controller.Handle(@event, cancellationToken);

                await userClient.Received().CreateDirectMessageChannel(Is(userId), Is(cancellationToken));

                await channelClient.Received().CreateMessage(Is(channel.Id), Is <CreateMessagePayload>(payload => payload.Content == strings["RegistrationGreeting", options.RegistrationUrl] !), Is(cancellationToken));
            }