public async Task SetReminder(SocketCommandContext Context, string message) { try { var time = GetTimeInterval(message); if (time == 0) { await Context.Channel.SendMessageAsync(":no_entry_sign: Your time format was incorrect!"); return; } if (!CheckTimeInterval(Context.User, time)) { await Context.Channel.SendMessageAsync(":no_entry_sign: You cannot have 2 Reminders that are within 60 seconds of each other"); return; } List <RemindData> dataList = new List <RemindData>(); if (_remindDict.ContainsKey(Context.User.Id)) { _remindDict.TryGetValue(Context.User.Id, out dataList); } message = message.Replace("mins", "m"); message = message.Replace("minutes", "m"); message = message.Replace("minute", "m"); message = message.Replace("min", "m"); var msg = message.Substring(0, message.LastIndexOf("in")); RemindData data = new RemindData { TimeToRemind = DateTime.UtcNow.AddSeconds(time), message = msg }; dataList.Add(data); //_punishLogs.AddOrUpdate(Context.Guild.Id, str, (key, oldValue) => str); _remindDict.AddOrUpdate(Context.User.Id, dataList, (key, oldValue) => dataList); ChangeToClosestInterval(); ReminderDB.SaveReminders(_remindDict); await Context.Channel.SendMessageAsync($":white_check_mark: Successfully set Reminder. I will remind you to `{data.message}` in `{message.Substring(message.LastIndexOf("in")+2)}`!"); } catch (Exception e) { Console.WriteLine(e); await SentryService.SendError(e, Context); } }
public async Task InitializeTimer() { try { _timer = new Timer(async _ => {////await (await Context.User.GetOrCreateDMChannelAsync()).SendMessageAsync("",false,eb); foreach (var user in _remindDict.ToArray()) { List <RemindData> itemsToRemove = new List <RemindData>(); foreach (var reminder in user.Value) { if (reminder.TimeToRemind.CompareTo(DateTime.UtcNow) <= 0) { var userToRemind = _client.GetUser(user.Key); await(await userToRemind.GetOrCreateDMChannelAsync()).SendMessageAsync($":alarm_clock: **Reminder:** {reminder.message}"); itemsToRemove.Add(reminder); } } foreach (var remove in itemsToRemove) { user.Value.Remove(remove); } _remindDict.TryUpdate(user.Key, user.Value); } ChangeToClosestInterval(); ReminderDB.SaveReminders(_remindDict); }, null, TimeSpan.FromSeconds(INITIAL_DELAY), // Time that message should fire after bot has started TimeSpan.FromSeconds(INITIAL_DELAY)); //time after which message should repeat (timout.infinite for no repeat) Console.WriteLine("TIMER INITIALIZED"); /*if(str.timeToTriggerAgain.CompareTo(DateTime.UtcNow) >0) * { * return; * }*/ } catch (Exception e) { Console.WriteLine(e); await SentryService.SendError(e); } }
public ReminderService(DiscordSocketClient client, InteractiveService inter) { try { _client = client; _interactiveService = inter; ReminderDB.InitializeLoader(); var reminders = ReminderDB.LoadReminders(); if (reminders != null) { _remindDict = reminders; Console.WriteLine("LOADED REMINDER DICT"); } Task.Factory.StartNew(() => { InitializeTimer(); }); //ChangeToClosestInterval(); DELAY THE FIRST RUN TO THE INITIAL DELAY! } catch (Exception e) { Console.WriteLine(e); } }
/* * public void Stop() //Example to make the timer stop running * { * _timer.Change(Timeout.Infinite, Timeout.Infinite); * } * * public void Restart() //Example to restart the timer * { * _timer.Change(TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(30)); * }*/ public async Task DelteReminder(SocketCommandContext Context) { try { if (!_remindDict.ContainsKey(Context.User.Id)) { await Context.Channel.SendMessageAsync(":no_entry_sign: You have no Reminders!"); return; } List <RemindData> data = new List <RemindData>(); _remindDict.TryGetValue(Context.User.Id, out data); if (data.Count < 1) { await Context.Channel.SendMessageAsync(":no_entry_sign: You have no Reminders!"); return; } var eb = new EmbedBuilder() { Color = new Color(4, 97, 247), Title = "Enter the Index of the Reminder you want to remove.", }; string reminders = ""; int count = 1; foreach (var v in data) { reminders += $"**{count}.** {v.message}\n"; count++; } reminders += $"**{count}.** Cancel"; eb.Description = reminders; var del = await Context.Channel.SendMessageAsync("", embed : eb); var response = await _interactiveService.WaitForMessage(Context.User, Context.Channel, TimeSpan.FromSeconds(20)); await del.DeleteAsync(); if (response == null) { await Context.Channel.SendMessageAsync($":no_entry_sign: Answer timed out {Context.User.Mention} (≧д≦ヾ)"); return; } int index = 0; if (!Int32.TryParse(response.Content, out index)) { await Context.Channel.SendMessageAsync($":no_entry_sign: Only add the Index"); return; } if (index > (data.Count + 1) || index < 1) { await Context.Channel.SendMessageAsync($":no_entry_sign: Invalid Number"); return; } if (index == count) { await Context.Channel.SendMessageAsync($":no_entry_sign: Action Cancelled"); return; } index -= 1; var msgThatGetsRemoved = data[index].message; data.RemoveAt(index); _remindDict.TryUpdate(Context.User.Id, data); ReminderDB.SaveReminders(_remindDict); await Context.Channel.SendMessageAsync($":white_check_mark: Successfully removed Reminder: `{msgThatGetsRemoved}`"); } catch (Exception e) { Console.WriteLine(e); await SentryService.SendError(e, Context); } }