1
0

rework ReminderModule

This commit is contained in:
Oreh 2023-07-25 19:06:46 +08:00
parent 16723fee4d
commit 7b9342e6e1
3 changed files with 74 additions and 43 deletions

View File

@ -61,6 +61,18 @@ namespace Discord_Bot
return (JObject)JsonConvert.DeserializeObject(configJson.ReadToEnd()); 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) public static void SaveConfig(JObject config)
{ {
using (StreamWriter file = File.CreateText(Directory.GetCurrentDirectory() + @"/Config.json")) using (StreamWriter file = File.CreateText(Directory.GetCurrentDirectory() + @"/Config.json"))

View File

@ -1,71 +1,70 @@
using Discord.Commands; using Discord.Commands;
using Discord.Commands.Builders;
using Newtonsoft.Json.Linq;
namespace Discord_Bot namespace Discord_Bot
{ {
public class ReminderModule : ModuleBase<SocketCommandContext> public class ReminderModule : ModuleBase<SocketCommandContext>
{ {
private long dayPeriod = 24 * 60 * 60 * 1000; private const long DAY_PERIOD = 24 * 60 * 60 * 1000; // 10 * 1000; //
private Timer reminderTimer; private Timer reminderTimer;
[Command("remindertime")] protected override void OnModuleBuilding(CommandService commandService, ModuleBuilder builder)
public async Task ReminderTime([Remainder]string message = "")
{ {
SaveToConfig("reminderTime", message); base.OnModuleBuilding(commandService, builder);
Console.WriteLine($"{DateTime.Now.TimeOfDay:hh\\:mm\\:ss} | hello world ");
var time = message.Split(":"); ReloadTimers();
SetReminderTime(int.Parse(time[0]), int.Parse(time[1])); }
[Command("remindertime")]
private async Task SetReminderTime([Remainder]string message = "")
{
SaveToConfig("reminderTime", Utility.ParseMessage(message));
ReloadTimers();
await ReplyAsync($"Заебись"); await ReplyAsync($"Заебись");
} }
[Command("calltime")] [Command("sequence")]
public async Task CallTime([Remainder]string message = "") private async Task SetSequence([Remainder]string message = "")
{ {
SaveToConfig("callTime", message); SaveToConfig("sequence", message);
var time = message.Split(":");
SetReminderTime(int.Parse(time[0]), int.Parse(time[1]));
await ReplyAsync($"Заебись"); await ReplyAsync($"Заебись");
} }
[Command("users")] [Command("users")]
public async Task Users([Remainder]string message = "") private async Task Users([Remainder]string message = "")
{ {
SaveToConfig("users", message); SaveToConfig("users", message);
var time = message.Split(":");
SetReminderTime(int.Parse(time[0]), int.Parse(time[1]));
await ReplyAsync($"Заебись"); 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 now = DateTime.Now;
var nextReminder = new DateTime(now.Year, now.Month, now.Day, hours, minutes, 0);
var span = (nextReminder - now).TotalMilliseconds; var span = (nextReminder - now).TotalMilliseconds;
reminderTimer = new Timer(SendReminder, "sd", (long)span, dayPeriod); if (span < 0)
{
ReloadTimers(); // skip to next day
} nextReminder = nextReminder.AddDays(1);
span = (nextReminder - now).TotalMilliseconds;
public void SetCallTime(int hours, int minutes) }
{
var now = DateTime.Now; reminderTimer = new Timer(SendReminder, "sd", (long)span, DAY_PERIOD);
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();
} }
private async void SendReminder(object? state) private async void SendReminder(object? state)
{ {
var users = Functions.GetConfig()["users"].ToString(); //check sequence
var users = Functions.GetConfigItem("users").ToString();
await ReplyAsync($"{users}\nБудет чо?"); await ReplyAsync($"{users}\nБудет чо?");
} }
private async void SendCall(object? state)
{
await ReplyAsync($"?");
}
private void SaveToConfig(string key, string data) private void SaveToConfig(string key, string data)
{ {
@ -73,10 +72,5 @@ namespace Discord_Bot
config[key] = data; config[key] = data;
Functions.SaveConfig(config); Functions.SaveConfig(config);
} }
private void ReloadTimers()
{
}
} }
} }

25
Budet-cho-bot/Utility.cs Normal file
View File

@ -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<DateTime>(token);
}
}