Esempio n. 1
0
        public async Task AddReminder(CommandContext ctx, params string[] paramsList)
        {
            if (ctx.Member.GetRole().IsCHOrHigher())
            {
                string args        = ctx.RawArgumentString;
                int    firstQuote  = args.IndexOf('[');
                int    secondQuote = args.LastIndexOf(']');

                // Let's try to get the date, first of all.
                string dateSubstring;

                // If there's no quote, let's just see what happens if we put the whole string in.
                if (firstQuote == -1)
                {
                    dateSubstring = args.TrimStart();
                }
                else
                {
                    dateSubstring = args.TrimStart(' ').Substring(0, firstQuote);
                }

                MatchCollection matches = DateRegex.Matches(dateSubstring);

                DateTimeOffset dto = ctx.Message.CreationTimestamp.UtcDateTime;

                foreach (Match match in matches)
                {
                    if (match.Groups.Count == 3)
                    {
                        // Check if it's an integer just in case...
                        if (Int32.TryParse(match.Groups[1].Value, out int measure))
                        {
                            InterpretTime(
                                measure: measure,
                                unit: match.Groups[2].Value,
                                dto: ref dto);
                        }
                    }
                }

                // Cancels: ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !

                if (dto.UtcTicks == ctx.Message.CreationTimestamp.UtcTicks)
                {   // No time has been added.
                    await ctx.Channel.SendMessageAsync(
                        ChatObjects.GetErrMessage(@"Invalid time string..."));

                    return;
                }

                DateTimeOffset yearFromNow = new DateTimeOffset(ctx.Message.CreationTimestamp.UtcDateTime).AddYears(1);
                if (dto.UtcTicks > yearFromNow.UtcTicks)
                {   // More than a year away.
                    await ctx.Channel.SendMessageAsync(
                        ChatObjects.GetErrMessage(@"That's more than one year away. Please reduce your time..."));

                    return;
                }

                // Great, so now we have our time string. Now we need to try and figure out what our message string is.
                string msgString = @"no message provided";

                // Just checking to make sure everything is in bounds.
                if (firstQuote != -1 && firstQuote + 1 < args.Length && secondQuote - 1 > firstQuote)
                {
                    if (secondQuote == -1)
                    {
                        msgString = args.Substring(firstQuote + 1);
                    }
                    else
                    {
                        msgString = args.Substring(firstQuote + 1, secondQuote - firstQuote - 1);
                    }
                }

                // Now we have our message string. Let's see if there are any mentions.
                IEnumerable <ulong> mentionIds =
                    from mentionId in ctx.Message.MentionedUsers.Select(a => a.Id).Distinct()
                    where mentionId != 669347771312111619 && // Do not allow mentions of the bot,
                    mentionId != 676710243878830090 &&       // the dev bot,
                    mentionId != ctx.Message.Author.Id       // or the user who called the function.
                    select mentionId;


                StringBuilder sb = new StringBuilder();
                // DEB!
                DiscordEmbedBuilder deb = new DiscordEmbedBuilder();

                Reminder reminder = new Reminder
                {
                    Text          = msgString,
                    Time          = dto.ToUnixTimeMilliseconds(),
                    User          = ctx.Member.Id,
                    Channel       = ctx.Channel.Id,
                    UsersToNotify = mentionIds.ToArray()
                };

                foreach (ulong mentionId in mentionIds)
                {
                    sb.Append(String.Format("<@{0}> ", mentionId));
                }

                deb.WithTitle(@"Notification");
                deb.AddField(@"User", ctx.Member.Mention);
                deb.AddField(@"Time", dto.ToString());
                deb.AddField(@"Message", msgString);

                if (sb.Length > 0)
                {
                    deb.AddField(@"Users to notify:", sb.ToString().TrimEnd());
                }

                deb.AddField(@"Notification Identifier", reminder.GetIdentifier());
                deb.WithThumbnailUrl(ChatObjects.URL_REMINDER_GENERIC);

                ReminderSystem.AddReminder(reminder);
                ReminderSystem.Save();

                await ctx.Channel.SendMessageAsync(String.Empty, false, deb);
            }
        }