예제 #1
0
        public static async Task AddReminder(IUser user, string text, DateTime time, IGuild guild)
        {
            if (Reminders.Count(ur => ur.UserId == user.Id) + 1 > Information.MaximumReminders)
            {
                throw new MaximumRemindersException(
                          $"Maximum simultaneous reminders for {user.Username} has been reached!");
            }

            var reminder = new UserReminder(user.Mention, user.Id, text, time);

            Reminders.Add(reminder);
            WriteOut(guild);

            IMessageChannel channel;

            try
            {
                channel = await user.GetOrCreateDMChannelAsync();
            } catch
            {
                // not accepting dms
                channel = await guild.GetDefaultChannelAsync();
            }

            var unused = ReminderWaiter(reminder, channel, guild);
        }
예제 #2
0
        private static async Task ReminderWaiter(UserReminder reminder, IMessageChannel channel, IGuild guild)
        {
            var offset = reminder.Time - DateTime.Now;

            //Remove if already passed
            if (offset.TotalMilliseconds < 0)
            {
                for (int i = 0; i < Reminders.Count; i++)
                {
                    if (Reminders[i].Equals(reminder))
                    {
                        Reminders.RemoveAt(i);
                        i--;
                    }
                }

                return;
            }

            await Task.Delay((int)offset.TotalMilliseconds);

            string message = $"{reminder.UserMention}, you told me to remind you to: \"_{reminder.Text}_\"!";

            if (channel != null)
            {
                await channel.SendMessageAsync(message);
            }
            else
            {
                try
                {
                    IEnumerable <IGuildUser> users = await guild.GetUsersAsync();

                    var user = users.FirstOrDefault(u => u.Mention == reminder.UserMention);
                    var dm   = await user.GetOrCreateDMChannelAsync();

                    await dm.SendMessageAsync(message);
                } catch
                {
                    //could not dm user
                }
            }

            for (int i = 0; i < Reminders.Count; i++)
            {
                if (Reminders[i].Equals(reminder))
                {
                    Reminders.RemoveAt(i);
                    i--;
                }
            }

            WriteOut(guild);
        }