private async Task HandleMessageFromRegisteredUser(MessageCreateEvent @event, TraceContext trace, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var user = await userService.GetIdentityServiceUserId(@event.Message.Author, cancellationToken); var userId = user.Id.ToString(); if (!user.Enabled) { return; } logger.LogInformation("Message author is registered, parsing for possible command & emitting message."); _ = emitter.Emit(@event.Message, @event.Message.ChannelId, cancellationToken); var result = await commandsService.ParseAndExecuteCommandAsUser( message : @event.Message.Content, userId : userId, sourceSystemId : @event.Message.ChannelId, cancellationToken : cancellationToken ); logger.LogInformation("Got result: {@result}", result); if (result?.ReplyImmediately == true) { var createMessagePayload = new CreateMessagePayload { Content = result.Response, Embed = GetDebugEmbed(user, trace), }; await discordChannelClient.CreateMessage(@event.Message.ChannelId, createMessagePayload, cancellationToken); } }
/// <inheritdoc /> public async Task CreateMessage(Snowflake channelId, CreateMessagePayload request, CancellationToken cancellationToken = default) { await handler.Handle( endpoint : ChannelEndpoint.CreateMessage, request : request, parameters : new Dictionary <string, string> { ["channel.id"] = channelId }, cancellationToken : cancellationToken ); }
public async Task ShouldCreateAMessageInTheCorrectChannel( Snowflake channelId, CreateMessagePayload payload, [Frozen, Substitute] IDiscordRequestHandler handler, [Target] DefaultDiscordChannelClient client, CancellationToken cancellationToken ) { await client.CreateMessage(channelId, payload, cancellationToken); await handler.Received().Handle( endpoint: Is((Endpoint)ChannelEndpoint.CreateMessage), request: Is(payload), parameters: Is <Dictionary <string, string> >(parameters => parameters["channel.id"] == channelId), headers: Is((Dictionary <string, string>?)null), cancellationToken: Is(cancellationToken) ); }
private async Task PerformWelcome(MessageCreateEvent @event, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); try { logger.LogInformation("User {@authorId} is not registered, sending invite through direct messages.", @event.Message.Author.Id); var dmChannel = await discordUserClient.CreateDirectMessageChannel(@event.Message.Author.Id, cancellationToken); var message = (string)strings["RegistrationGreeting", adapterOptions.RegistrationUrl] !; var payload = new CreateMessagePayload { Content = message }; await discordChannelClient.CreateMessage(dmChannel.Id, payload, cancellationToken); } catch (Exception exception) { logger.LogError("An error occured while attempting to direct message {@authorId}: {@exception}", @event.Message.Author.Id, exception); } }