예제 #1
0
            private async Task StartReminder(Reminder r)
            {
                var now = DateTime.Now;
                var twoMins = new TimeSpan(0, 2, 0);
                TimeSpan time = r.When - now;

                if (time.TotalMilliseconds > int.MaxValue)
                    return;

                await Task.Delay(time);
                try
                {
                    IMessageChannel ch;
                    if (r.IsPrivate)
                    {
                        ch = await NadekoBot.Client.GetDMChannelAsync(r.ChannelId).ConfigureAwait(false);
                    }
                    else
                    {
                        ch = NadekoBot.Client.GetGuild(r.ServerId)?.GetTextChannel(r.ChannelId);
                    }
                    if (ch == null)
                        return;

                    await ch.SendMessageAsync(
                        replacements.Aggregate(RemindMessageFormat,
                            (cur, replace) => cur.Replace(replace.Key, replace.Value(r)))
                            .SanitizeMentions()
                            ).ConfigureAwait(false); //it works trust me
                }
                catch (Exception ex) { _log.Warn(ex); }
                finally
                {
                    using (var uow = DbHandler.UnitOfWork())
                    {
                        uow.Reminders.Remove(r);
                        await uow.CompleteAsync();
                    }
                }
            }
예제 #2
0
            public async Task Remind(IUserMessage umsg, IMessageChannel ch, string timeStr, [Remainder] string message)
            {
                var channel = (ITextChannel)umsg.Channel;

                if (ch == null)
                {
                    await channel.SendMessageAsync($"⚠️ {umsg.Author.Mention} Something went wrong (channel cannot be found) ;(").ConfigureAwait(false);
                    return;
                }

                var m = regex.Match(timeStr);

                if (m.Length == 0)
                {
                    await channel.SendMessageAsync("❎ **Not a valid time format.** type `-h .remind`").ConfigureAwait(false);
                    return;
                }

                string output = "";
                var namesAndValues = new Dictionary<string, int>();

                foreach (var groupName in regex.GetGroupNames())
                {
                    if (groupName == "0") continue;
                    int value = 0;
                    int.TryParse(m.Groups[groupName].Value, out value);

                    if (string.IsNullOrEmpty(m.Groups[groupName].Value))
                    {
                        namesAndValues[groupName] = 0;
                        continue;
                    }
                    else if (value < 1 ||
                        (groupName == "months" && value > 1) ||
                        (groupName == "weeks" && value > 4) ||
                        (groupName == "days" && value >= 7) ||
                        (groupName == "hours" && value > 23) ||
                        (groupName == "minutes" && value > 59))
                    {
                        await channel.SendMessageAsync($"⚠️ Invalid {groupName} value.").ConfigureAwait(false);
                        return;
                    }
                    else
                        namesAndValues[groupName] = value;
                    output += m.Groups[groupName].Value + " " + groupName + " ";
                }
                var time = DateTime.Now + new TimeSpan(30 * namesAndValues["months"] +
                                                        7 * namesAndValues["weeks"] +
                                                        namesAndValues["days"],
                                                        namesAndValues["hours"],
                                                        namesAndValues["minutes"],
                                                        0);

                var rem = new Reminder
                {
                    ChannelId = ch.Id,
                    IsPrivate = ch is IDMChannel,
                    When = time,
                    Message = message,
                    UserId = umsg.Author.Id,
                    ServerId = channel.Guild.Id
                };

                using (var uow = DbHandler.UnitOfWork())
                {
                    uow.Reminders.Add(rem);
                    await uow.CompleteAsync();
                }

                try { await channel.SendMessageAsync($"⏰ I will remind **\"{(ch is ITextChannel ? ((ITextChannel)ch).Name : umsg.Author.Username)}\"** to **\"{message.SanitizeMentions()}\"** in **{output}** `({time:d.M.yyyy.} at {time:HH:mm})`").ConfigureAwait(false); } catch { }
                await StartReminder(rem);
            }