diff --git a/Budet-cho-bot/Functions/Functions.cs b/Budet-cho-bot/Functions/Functions.cs index 0f8f997..747af85 100644 --- a/Budet-cho-bot/Functions/Functions.cs +++ b/Budet-cho-bot/Functions/Functions.cs @@ -61,6 +61,18 @@ namespace Discord_Bot return (JObject)JsonConvert.DeserializeObject(configJson.ReadToEnd()); } + public static JToken GetConfigItem(string item) + { + var config = Functions.GetConfig(); + if (config.TryGetValue(item, out var result)) + { + return result; + } + + var res = new JValue(0); + return res.Type == JTokenType.Null; + } + public static void SaveConfig(JObject config) { using (StreamWriter file = File.CreateText(Directory.GetCurrentDirectory() + @"/Config.json")) diff --git a/Budet-cho-bot/Modules/ReminderModule.cs b/Budet-cho-bot/Modules/ReminderModule.cs index c2af22d..9a8298e 100644 --- a/Budet-cho-bot/Modules/ReminderModule.cs +++ b/Budet-cho-bot/Modules/ReminderModule.cs @@ -1,71 +1,70 @@ using Discord.Commands; +using Discord.Commands.Builders; +using Newtonsoft.Json.Linq; namespace Discord_Bot { public class ReminderModule : ModuleBase { - private long dayPeriod = 24 * 60 * 60 * 1000; + private const long DAY_PERIOD = 24 * 60 * 60 * 1000; // 10 * 1000; // private Timer reminderTimer; - - [Command("remindertime")] - public async Task ReminderTime([Remainder]string message = "") + + protected override void OnModuleBuilding(CommandService commandService, ModuleBuilder builder) { - SaveToConfig("reminderTime", message); - - var time = message.Split(":"); - SetReminderTime(int.Parse(time[0]), int.Parse(time[1])); + base.OnModuleBuilding(commandService, builder); + Console.WriteLine($"{DateTime.Now.TimeOfDay:hh\\:mm\\:ss} | hello world "); + ReloadTimers(); + } + + [Command("remindertime")] + private async Task SetReminderTime([Remainder]string message = "") + { + SaveToConfig("reminderTime", Utility.ParseMessage(message)); + ReloadTimers(); await ReplyAsync($"Заебись"); } - [Command("calltime")] - public async Task CallTime([Remainder]string message = "") + [Command("sequence")] + private async Task SetSequence([Remainder]string message = "") { - SaveToConfig("callTime", message); - - var time = message.Split(":"); - SetReminderTime(int.Parse(time[0]), int.Parse(time[1])); + SaveToConfig("sequence", message); await ReplyAsync($"Заебись"); } [Command("users")] - public async Task Users([Remainder]string message = "") + private async Task Users([Remainder]string message = "") { SaveToConfig("users", message); - - var time = message.Split(":"); - SetReminderTime(int.Parse(time[0]), int.Parse(time[1])); await ReplyAsync($"Заебись"); } - public void SetReminderTime(int hours, int minutes) + private void ReloadTimers() { + var time = Functions.GetConfigItem("reminderTime"); + if (time.Type == JTokenType.Boolean) + return; + + var nextReminder = Utility.ParseTime(time.ToString()); var now = DateTime.Now; - var nextReminder = new DateTime(now.Year, now.Month, now.Day, hours, minutes, 0); + var span = (nextReminder - now).TotalMilliseconds; - reminderTimer = new Timer(SendReminder, "sd", (long)span, dayPeriod); - - ReloadTimers(); - } - - public void SetCallTime(int hours, int minutes) - { - var now = DateTime.Now; - var nextReminder = new DateTime(now.Year, now.Month, now.Day, hours, minutes, 0); - var span = (nextReminder - now).TotalMilliseconds; - reminderTimer = new Timer(SendReminder, "sd", (long)span, dayPeriod); - - ReloadTimers(); + if (span < 0) + { + // skip to next day + nextReminder = nextReminder.AddDays(1); + span = (nextReminder - now).TotalMilliseconds; + } + + reminderTimer = new Timer(SendReminder, "sd", (long)span, DAY_PERIOD); } private async void SendReminder(object? state) { - var users = Functions.GetConfig()["users"].ToString(); + //check sequence + + var users = Functions.GetConfigItem("users").ToString(); await ReplyAsync($"{users}\nБудет чо?"); } - private async void SendCall(object? state) - { - await ReplyAsync($"?"); - } private void SaveToConfig(string key, string data) { @@ -73,10 +72,5 @@ namespace Discord_Bot config[key] = data; Functions.SaveConfig(config); } - - private void ReloadTimers() - { - - } } } \ No newline at end of file diff --git a/Budet-cho-bot/Utility.cs b/Budet-cho-bot/Utility.cs new file mode 100644 index 0000000..f039f46 --- /dev/null +++ b/Budet-cho-bot/Utility.cs @@ -0,0 +1,25 @@ +using Newtonsoft.Json.Linq; + +namespace Discord_Bot; + +public static class Utility +{ + public static string ParseMessage(string message) + { + var time = message.Split(":"); + if (time.Length != 2) + { + throw new ArgumentException("Время неправильное. Хуйня твоё время"); + return null; + } + + var now = DateTime.Now; + var date = new DateTime(now.Year, now.Month, now.Day, int.Parse(time[0]), int.Parse(time[1]), 0); + return Newtonsoft.Json.JsonConvert.SerializeObject(date); + } + + public static DateTime ParseTime(string token) + { + return Newtonsoft.Json.JsonConvert.DeserializeObject(token); + } +} \ No newline at end of file