Example #1
0
        public async Task ProcessMessage(DiscordMessage discordMessage)
        {
            logger.LogInformation($"MessageProcessor {ProcessorName} received message with id {discordMessage.Id}");

            logger.LogInformation($"MessageProcessor {ProcessorName} is handling message with id {discordMessage.Id}");
            var isSuccess = await HandleMessage(discordMessage);

            if (isSuccess)
            {
                await metrics.AddCounter($"Discord.{ProcessorName}.Success", 1);

                logger.LogInformation($"MessageProcessor {ProcessorName} succesfully handled message with id {discordMessage.Id}");
            }
            else
            {
                await metrics.AddCounter($"Discord.{ProcessorName}.Error", 1);

                logger.LogInformation($"MessageProcessor {ProcessorName} did not successfully handle message with id {discordMessage.Id}");
            }
        }
        public async void Poll(object sender, ElapsedEventArgs evt)
        {
            logger.LogInformation($"Polling for messages from queue {QueueUrl}");

            ReceiveMessageResponse messages = await ReceiveMessages();

            logger.LogInformation($"Got {messages.Messages.Count} messages from queue {QueueUrl}");
            await metrics.AddCounter("SQS.MessagesReceived", messages.Messages.Count);

            if (messages.Messages.Count > 0)
            {
                foreach (var message in messages.Messages)
                {
                    var queueMessage = JObject.Parse(message.Body);

                    var dynamoMessage = queueMessage["Message"].ToString();

                    logger.LogDebug(dynamoMessage);

                    var dynamoEvent = Serializer.Deserialize <Record>(new JsonTextReader(new StringReader(dynamoMessage)));

                    if (dynamoEvent.EventName.Value == "REMOVE")
                    {
                        var cleanupMessage = dynamoEvent.Dynamodb.OldImage;
                        var channelId      = ulong.Parse(cleanupMessage["ChannelId"].N);
                        var messageId      = ulong.Parse(cleanupMessage["MessageCleanupTableId"].S);

                        try {
                            var channel = await discordClient.GetChannelAsync(channelId);

                            var discordMessage = await channel.GetMessageAsync(messageId);

                            await channel.DeleteMessageAsync(discordMessage, "expired link");

                            logger.LogInformation($"Deleted discord message with id {messageId} from channel {channelId}");
                            await LogChannel.SendMessageAsync($"Removed message '{messageId}'");
                        }
                        catch (NotFoundException ex)
                        {
                            logger.LogWarning($"Tried to delete message {messageId} but it was not found and probably already deleted");
                        }
                        catch (UnauthorizedException ex)
                        {
                            logger.LogError($"Could not delete message {messageId} in channel {channelId} because of lacking permissions");
                        }
                    }

                    await DeleteMessage(message);

                    logger.LogInformation($"Successfully deleted message with id {message.ReceiptHandle} from queue");
                }
            }
        }
Example #3
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            logger.LogInformation("Starting the DiscordClient that will receive events");

            discordClient.MessageCreated += async(evt) =>
            {
                await metrics.AddCounter("Discord.MessagesReceived", 1);

                await dispatcher.Dispatch(evt.Message);
            };

            await discordClient.ConnectAsync();
        }