public void AddAlarm(DateTime alarmdate, string alarmname, ulong userid, bool autosave = true) { AlarmData nAlarm = new AlarmData() { AlarmDate = alarmdate, AlarmName = alarmname, userId = userid }; this.alarms.Add(nAlarm); if (autosave) { this.SaveGuildData(); } }
public async Task Execute() { IsRunning = true; ShouldRun = true; //This loop ticks every new minute on the systemclock while (ShouldRun) { DateTime now = DateTime.Now; //Debug: Chime twice every minute //await AnnounceFile(Path.Combine(Environment.CurrentDirectory, "samples", "CHIME1.wav"), 2); //System.Console.WriteLine($"Guildhandler tick {Guild.Name} at T: {now.ToShortTimeString()}"); int alarmCountStart = alarms.Count; //Check if we need to trigger an alarm for (int i = 0; i < alarms.Count; i++) { AlarmData alarm = alarms[i]; if (alarm.recurring && alarm.AlarmDate.DayOfWeek != now.DayOfWeek) { continue; } if (alarm.AlarmDate.Hour == now.Hour && alarm.AlarmDate.Minute == now.Minute) { System.Console.WriteLine($"Triggering alarm ${alarm.AlarmName} - {alarm.AlarmDate.ToShortTimeString()}"); //Announce in the channel where the user that set this alarm currently is. await AnnounceFile(Path.Combine(Environment.CurrentDirectory, "samples", "CHIME1.wav"), 2, Channels.Where(x => x.Users.Where(x => x.Id == alarm.userId).Count() > 0).ToList()); alarms.RemoveAt(i); //Todo: implement recurring alarms i--; } } if (alarms.Count != alarmCountStart) { SaveGuildData(); //save the alarms list if it has changed } //Special case for 420 (blaze it) if (now.Minute == 20 && (now.Hour % 12) == 4) { await AnnounceFile(Path.Combine(Environment.CurrentDirectory, "samples", "420.mp3")); } if (now.Minute == 0) //If we entered a new hour { System.Console.WriteLine($"Guildhandler entered new hour"); await AnnounceFile(getFileNameForHour(now.Hour)); } //check for bonus clip if (nextBonusClip - now <= TimeSpan.Zero) { //Play bonus clip Random rnd = new Random(); string[] extraClipFiles = Directory.EnumerateFiles(Path.Combine(Environment.CurrentDirectory, "samples")).Where(x => x.Contains("extra_")).ToArray(); int clipIndex = (int)Math.Round((double)rnd.NextDouble() * (extraClipFiles.Length - 1)); System.Console.WriteLine($"Selected {extraClipFiles[clipIndex]} as bonus clip"); if (nextBonusClip != DateTime.UnixEpoch)//Ignore the first time as this runs when the handler is started { await AnnounceFile(extraClipFiles[clipIndex]); } //Determine when we next will play a bonusclip (after 2h and a random amount of minutes) nextBonusClip = DateTime.Now.AddHours(2).AddMinutes((int)(rnd.NextDouble() * 60f)); } //System.Console.WriteLine($"Guildhandler has ticked {Guild.Name}"); //Calculate the number of miliseconds until a new minute on the system clock (fix? add one second to account for task.delay() inaccuracy) double millisToNextMinute = (double)((60 * 1000) - DateTime.Now.TimeOfDay.TotalMilliseconds % (60 * 1000)); await Task.Delay((int)millisToNextMinute + 1000); } System.Console.WriteLine($"Guildhandler stopped {Guild.Name}"); IsRunning = false; }