/// <summary> /// Tries to set up a new schedule for <see cref="Program.PicScheduler"/>. /// </summary> /// <param name="msg">The user message.</param> /// <param name="name">The name of the schedule.</param> /// <param name="interval">The interval of the schedule.</param> /// <returns>Returns whether the operation was successful.</returns> private bool TrySetupSchedule(SocketMessage msg, string name, TimeSpan interval) { PictureScheduler scheduler = (PictureScheduler)Program.Workers.FirstOrDefault(w => w is PictureScheduler); //Returns false if the given schedule already exists if (scheduler?.Settings.Count(a => a.Name.Equals(name)) > 0) { return(false); } //Gets the channel path where the command was received in string channelPath = (msg.Channel as SocketGuildChannel)?.GetPath(); //Creates a new array with the new schedule appended PictureSchedule[] newArr = scheduler.Settings .Append(new PictureSchedule(name, channelPath, interval)).ToArray(); //Tries to write the new schedule to file try { File.WriteAllText( PictureScheduler.File, JsonSerializer.Serialize <PictureSchedule[]>(newArr, new JsonSerializerOptions { WriteIndented = true })); } catch (Exception) { return(false); } return(true); }
/// <summary> /// Tries to stop a schedule. /// </summary> /// <param name="name">The name of the schedule to stop.</param> /// <returns>Returns whether the operation was successful.</returns> private bool TryStopSchedule(string name) { PictureScheduler scheduler = (PictureScheduler)Program.Workers.FirstOrDefault(w => w is PictureScheduler); //Returns false if the given schedule does not exist if (scheduler.Settings.Count(a => a.Name.Equals(name)) == 0) { return(false); } //Creates a new array with the given schedule removed PictureSchedule[] newArr = scheduler.Settings.Where(a => !a.Name.Equals(name)).ToArray(); //Tries to write the schedules with the given one removed to file try { File.WriteAllText( PictureScheduler.File, JsonSerializer.Serialize <PictureSchedule[]>(newArr, new JsonSerializerOptions { WriteIndented = true })); } catch (Exception) { return(false); } return(true); }