Ejemplo n.º 1
0
        ReplyList.Item SelectItem(ReplyEvent @event)
        {
            var items = _replies.CurrentValue.Items.Where(x => x.Event == @event).ToArray();

            if (items.Length == 0)
            {
                return(null);
            }

            double selected;

            lock (_random)
                selected = _random.NextDouble();

            selected *= items.Sum(x => x.Weight);

            var current = 0.0;

            foreach (var item in items)
            {
                current += item.Weight;

                if (selected < current)
                {
                    return(item);
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
        public async Task SendAsync(IMessageChannel channel, ReplyEvent @event, object substitutions, CancellationToken cancellationToken = default)
        {
            var selected = SelectItem(@event);

            if (selected == null || selected.Content == ".") // "." represents not sending a reply
            {
                return;
            }

            var message = ApplySubstitutions(selected.Content, substitutions);

            if (message.Length == 0)
            {
                return;
            }

            var options = _options.CurrentValue;

            foreach (var part in message.Split("\\n", StringSplitOptions.RemoveEmptyEntries)) // literal "\n" splits a message into multiple messages
            {
                var typingTime = TimeSpan.FromMinutes(part.Length / options.ReplyTypingCpm);

                lock (_random)
                    typingTime *= 0.9 + 0.2 * _random.NextDouble();

                using (channel.Typing())
                {
                    await Task.Delay(typingTime, cancellationToken);

                    await channel.SendMessageAsync(part);
                }
            }
        }