public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
        {
            // Ensure only one message per conversation is processed at a time
            using (var x = new AsyncDuplicateLock().Lock(turnContext.Activity.Conversation.Id))
            {
                var channelData = turnContext.Activity.ChannelData;
                if (channelData != null)
                {
                    // check syncId against this conversation's previously stored syncId
                    var channelDataDeserialized = JsonConvert.DeserializeObject(channelData.ToString());
                    var asDictionary            = channelDataDeserialized as IDictionary <string, JToken>;
                    if (asDictionary != null && asDictionary.ContainsKey("syncId"))
                    {
                        string repliedToId = asDictionary["syncId"].Value <string>();

                        var    lastIdProperty    = ConversationState.CreateProperty <string>(AdapterWithErrorHandler.LastActivityIdPropertyName);
                        string expectedReplyToId = await lastIdProperty.GetAsync(turnContext, () => string.Empty);

                        if (!string.IsNullOrEmpty(repliedToId) && !string.IsNullOrEmpty(expectedReplyToId))
                        {
                            // If the ids do not match, just ignore this message
                            // (this means the user sent an unexpected message)
                            if (expectedReplyToId != repliedToId)
                            {
                                return;
                            }
                        }
                    }
                }
                await base.OnTurnAsync(turnContext, cancellationToken);

                // Save any state changes that might have occured during the turn.
                await ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);

                await UserState.SaveChangesAsync(turnContext, false, cancellationToken);
            }
        }
Пример #2
0
 public StarboardService(DatabaseService databaseService, DiscordShardedClient discordShardedClient)
 {
     _db     = databaseService;
     _client = discordShardedClient;
     _starboardReadWriteLock = new AsyncDuplicateLock <ulong>();
 }