예제 #1
0
파일: Reminders.cs 프로젝트: Chronojam/Mute
        private async Task <string> CreateReminder(ICommandContext context, string message)
        {
            var result = FuzzyParsing.Moment(message);

            string error = null;

            if (!result.IsValid)
            {
                error = result.ErrorMessage;
            }
            else if (result.Value < DateTime.UtcNow)
            {
                error = PastValueErrorMessage.Replace("$moment$", result.Value.ToString(CultureInfo.InvariantCulture));
            }

            if (error != null)
            {
                return(error);
            }
            else
            {
                var triggerTime = result.Value;
                var duration    = triggerTime - DateTime.UtcNow;

                //Add some context to the message
                var prelude = $"{context.Message.Author.Mention} Reminder from {DateTime.UtcNow.Humanize(dateToCompareAgainst: triggerTime, culture: CultureInfo.GetCultureInfo("en-gn"))}...";
                var msg     = $"remind me {message}";

                //Save to database
                var n = await _reminders.Create(triggerTime, prelude, msg, context.Message.Channel.Id, context.User.Id);

                var friendlyId = new FriendlyId32(n.ID);
                return($"I will remind you in {duration.Humanize(2, maxUnit: TimeUnit.Year, minUnit: TimeUnit.Second, toWords: true)} (id: `{friendlyId}`)");
            }
        }
예제 #2
0
        public void ParseComplexTime()
        {
            var time = FuzzyParsing.Moment("Midday tomorrow");

            Assert.IsTrue(time.IsValid);

            var tomorrow = DateTime.UtcNow.AddDays(1);
            var expected = new DateTime(tomorrow.Year, tomorrow.Month, tomorrow.Day, 12, 00, 0);

            Assert.AreEqual(expected, time.Value);
        }
예제 #3
0
        public void ParseSimpleTimeWithTz()
        {
            var time = FuzzyParsing.Moment("15:35 BST tomorrow");

            Assert.IsTrue(time.IsValid);

            var tomorrow = DateTime.UtcNow.AddDays(1);
            var expected = new DateTime(tomorrow.Year, tomorrow.Month, tomorrow.Day, 14, 35, 0);

            Assert.AreEqual(expected, time.Value);
        }
예제 #4
0
        public void DateTimeRange()
        {
            var r1 = DateTimeRecognizer.RecognizeDateTime("next week", "en-gb");

            Console.WriteLine(JsonConvert.SerializeObject(r1));

            var r2 = FuzzyParsing.MomentRange("next week");

            Console.WriteLine(JsonConvert.SerializeObject(r2));

            var r3 = FuzzyParsing.Moment("monday");

            Console.WriteLine(JsonConvert.SerializeObject(r3));
        }
예제 #5
0
        private async Task <string> CreateReminder(ICommandContext context, string message)
        {
            DateTime triggerMoment;

            // Parse a moment from message
            var exactTimeResult = FuzzyParsing.Moment(message);

            if (exactTimeResult.IsValid)
            {
                if (exactTimeResult.Value < DateTime.UtcNow)
                {
                    return(PastValueErrorMessage.Replace("$moment$", exactTimeResult.Value.ToString(CultureInfo.InvariantCulture)));
                }

                triggerMoment = exactTimeResult.Value;
            }
            else
            {
                // Attempt to parse a time range instead of an exact time (e.g. `next week`)
                var rangeTimeResult = FuzzyParsing.MomentRange(message);
                if (!rangeTimeResult.IsValid)
                {
                    return("That doesn't seem to be a valid moment.");
                }

                // Send the reminder just after the start of the range
                triggerMoment = rangeTimeResult.Value.Item1 + (rangeTimeResult.Value.Item2 - rangeTimeResult.Value.Item1) * 0.05f;
            }

            var duration = triggerMoment - DateTime.UtcNow;

            // Add some context to the message
            var prelude = $"{context.Message.Author.Mention} Reminder from {DateTime.UtcNow.Humanize(dateToCompareAgainst: triggerMoment, culture: CultureInfo.GetCultureInfo("en-gn"))}...";
            var msg     = $"remind me {message}";

            // Save to database
            var n = await _reminders.Create(triggerMoment, prelude, msg, context.Message.Channel.Id, context.User.Id);

            var friendlyId = new BalderHash32(n.ID);

            return($"I will remind you in {duration.Humanize(2, maxUnit: TimeUnit.Year, minUnit: TimeUnit.Second, toWords: true)} (id: `{friendlyId}`)");
        }