示例#1
0
        private static void OnTimerElapsed(object sender, ElapsedEventArgs args)
        {
            try
            {
                using (BaseDBReader <BotTimer> timerReader = new BaseDBReader <BotTimer>())
                {
                    List <BotTimer> timers = timerReader.Where(bt => !bt.Expired);

                    for (int i = 0; i < timers.Count; i++)
                    {
                        BotTimer btimer = timers[i];

                        if (btimer.EndTime >= DateTime.UtcNow)
                        {
                            return;
                        }

                        btimer.Expired = true;
                        timerReader.Update(btimer);

                        string msg = $"<@!{btimer.CreatedByDiscordId}> ";

                        if (!string.IsNullOrEmpty(btimer.PingMessage))
                        {
                            msg += "Reminding you of: " + Environment.NewLine + btimer.PingMessage;
                        }

                        Coding.Discord.SendMessage((ulong)btimer.DiscordChannelId, msg);
                    }

                    timerReader.Save();
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString(), LogLevel.ERROR);
            }
        }
示例#2
0
        public void Activate(CommandEventArg e)
        {
            try
            {
                if (string.IsNullOrEmpty(e.AfterCMD))
                {
                    Coding.Discord.GetChannel(e.ChannelID).SendMessageAsync(embed: BuildUsageInfo());
                    return;
                }

                string[] paramSplit = e.AfterCMD.Split(' ');

                int pageIndex = 1;
                switch (paramSplit.Length)
                {
                default:
                    break;

                case 2:
                    if (!paramSplit[0].Equals("delete", StringComparison.InvariantCultureIgnoreCase))
                    {
                        int.TryParse(paramSplit[1], out pageIndex);
                        goto case 1;
                    }

                    if (!int.TryParse(paramSplit[1], out int timerId))
                    {
                        Coding.Discord.GetChannel(e.ChannelID).SendMessageAsync(content: "Could not parse timer id " + paramSplit[1], embed: BuildUsageInfo());
                        return;
                    }

                    lock (_timersLock)
                    {
                        using (BaseDBReader <BotTimer> timerReader = new BaseDBReader <BotTimer>())
                        {
                            timerReader.Remove(bt => bt.Id == timerId);
                            timerReader.Save();
                        }
                    }

                    Coding.Discord.SendMessage(e.ChannelID, "Removed timer with id " + timerId);
                    return;

                case 1:
                    if (paramSplit[0].Equals("list"))
                    {
                        List <BotTimer> timers     = GetTimersByDiscordId(e.DUserID);
                        double          totalPages = 1;

                        if (timers.Count > 10)
                        {
                            totalPages = timers.Count / 10.0;
                        }

                        if ((int)totalPages < totalPages)
                        {
                            totalPages = (int)totalPages + 1;
                        }

                        timers = GetAvailableIndexes((pageIndex - 1) * 10, 10, timers);

                        if (timers.Count == 0)
                        {
                            Coding.Discord.SendMessage(e.ChannelID, "No reminders found");
                            return;
                        }

                        Coding.Discord.GetChannel(e.ChannelID).SendMessageAsync(embed: BuildRemindMeList(timers, pageIndex, (int)totalPages, e.DUserID, e.ChannelID, e.GuildID));
                        return;
                    }
                    break;

                case 0:
                    Coding.Discord.GetChannel(e.ChannelID).SendMessageAsync(embed: BuildUsageInfo());
                    return;
                }

                string[] timeSplit = paramSplit[0].Split(':');

                if (timeSplit.Length < 3)
                {
                    Coding.Discord.GetChannel(e.ChannelID).SendMessageAsync(embed: BuildUsageInfo());
                    return;
                }

                if (!int.TryParse(timeSplit[0], out int days) ||
                    !int.TryParse(timeSplit[1], out int hours) ||
                    !int.TryParse(timeSplit[2], out int minutes))
                {
                    Coding.Discord.GetChannel(e.ChannelID).SendMessageAsync(embed: BuildUsageInfo());
                    return;
                }

                days    = Math.Max(0, Math.Min(days, 31));
                hours   = Math.Max(0, Math.Min(hours, 23));
                minutes = Math.Max(0, Math.Min(minutes, 59));

                if (days == 0 &&
                    hours == 0 &&
                    minutes == 0)
                {
                    Coding.Discord.SendMessage(e.ChannelID, "Days, hours and minutes cannot be 0 all at once!");
                    return;
                }

                string message = null;

                if (paramSplit.Length >= 2)
                {
                    message = paramSplit[1];

                    for (int i = 2; i < paramSplit.Length; i++)
                    {
                        message += ' ' + paramSplit[i];
                    }
                }

                BotTimer timer;
                using (BaseDBReader <BotTimer> timerReader = new BaseDBReader <BotTimer>())
                {
                    DateTime startTime = DateTime.UtcNow;
                    DateTime endTime   = startTime.AddHours(hours).AddMinutes(minutes).AddDays(days);

                    timer = timerReader.Add(new BotTimer(true, DateTime.UtcNow, endTime, (long)e.DUserID, false, (long)e.ChannelID, !e.GuildID.HasValue, message));
                    timerReader.Save();
                }

                Coding.Discord.SendMessage(e.ChannelID, $"Created timer {timer.Id}, expires on: {GetExpirationDate(timer)} UTC");
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString(), LogLevel.ERROR);
            }
        }