add sequence command
This commit is contained in:
parent
7b9342e6e1
commit
bcf72d68b9
@ -1,19 +1,18 @@
|
|||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using Discord.Commands.Builders;
|
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
namespace Discord_Bot
|
namespace Discord_Bot
|
||||||
{
|
{
|
||||||
public class ReminderModule : ModuleBase<SocketCommandContext>
|
public class ReminderModule : ModuleBase<SocketCommandContext>
|
||||||
{
|
{
|
||||||
private const long DAY_PERIOD = 24 * 60 * 60 * 1000; // 10 * 1000; //
|
private const long DAY_PERIOD = 24 * 60 * 60 * 1000; // 10 * 1000; //
|
||||||
private Timer reminderTimer;
|
private Timer reminderTimer;
|
||||||
|
|
||||||
protected override void OnModuleBuilding(CommandService commandService, ModuleBuilder builder)
|
[Command("reloadTimer")]
|
||||||
|
private async Task SetReloadTimer()
|
||||||
{
|
{
|
||||||
base.OnModuleBuilding(commandService, builder);
|
|
||||||
Console.WriteLine($"{DateTime.Now.TimeOfDay:hh\\:mm\\:ss} | hello world ");
|
|
||||||
ReloadTimers();
|
ReloadTimers();
|
||||||
|
await ReplyAsync($"Заебись");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Command("remindertime")]
|
[Command("remindertime")]
|
||||||
@ -28,6 +27,7 @@ namespace Discord_Bot
|
|||||||
private async Task SetSequence([Remainder]string message = "")
|
private async Task SetSequence([Remainder]string message = "")
|
||||||
{
|
{
|
||||||
SaveToConfig("sequence", message);
|
SaveToConfig("sequence", message);
|
||||||
|
SaveToConfig("sequenceIndex", "0");
|
||||||
await ReplyAsync($"Заебись");
|
await ReplyAsync($"Заебись");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,30 +40,87 @@ namespace Discord_Bot
|
|||||||
|
|
||||||
private void ReloadTimers()
|
private void ReloadTimers()
|
||||||
{
|
{
|
||||||
var time = Functions.GetConfigItem("reminderTime");
|
var reminder = GetReminderTime();
|
||||||
if (time.Type == JTokenType.Boolean)
|
|
||||||
return;
|
|
||||||
|
|
||||||
var nextReminder = Utility.ParseTime(time.ToString());
|
|
||||||
var now = DateTime.Now;
|
var now = DateTime.Now;
|
||||||
|
|
||||||
var span = (nextReminder - now).TotalMilliseconds;
|
var span = (reminder - now).TotalMilliseconds;
|
||||||
if (span < 0)
|
if (span < 0)
|
||||||
{
|
{
|
||||||
// skip to next day
|
// skip to next period
|
||||||
nextReminder = nextReminder.AddDays(1);
|
var d = (now - reminder).TotalMilliseconds + DAY_PERIOD;
|
||||||
span = (nextReminder - now).TotalMilliseconds;
|
reminder = reminder.AddMilliseconds(d);
|
||||||
|
span = (reminder - now).TotalMilliseconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
reminderTimer = new Timer(SendReminder, "sd", (long)span, DAY_PERIOD);
|
reminderTimer = new Timer(SendReminder, "sd", (long)span, DAY_PERIOD);
|
||||||
|
|
||||||
|
RecalcSequenceIndex();
|
||||||
|
}
|
||||||
|
|
||||||
|
private DateTime GetReminderTime()
|
||||||
|
{
|
||||||
|
var time = Functions.GetConfigItem("reminderTime");
|
||||||
|
if (time.Type == JTokenType.Boolean)
|
||||||
|
return DateTime.Now;
|
||||||
|
|
||||||
|
return Utility.ParseTime(time.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void SendReminder(object? state)
|
private async void SendReminder(object? state)
|
||||||
{
|
{
|
||||||
//check sequence
|
if (!CheckSequenceAndIncrementIndex())
|
||||||
|
return;
|
||||||
|
|
||||||
var users = Functions.GetConfigItem("users").ToString();
|
var users = Functions.GetConfigItem("users").ToString();
|
||||||
await ReplyAsync($"{users}\nБудет чо?");
|
var message = $"{users}\nБудет чо? {DateTime.Now}";
|
||||||
|
await ReplyAsync(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RecalcSequenceIndex()
|
||||||
|
{
|
||||||
|
var reminder = GetReminderTime();
|
||||||
|
var now = DateTime.Now;
|
||||||
|
var sequence = GetSequence();
|
||||||
|
|
||||||
|
var span = (now - reminder).TotalMilliseconds;
|
||||||
|
var spanMinusFullSequences = (int)MathF.Floor((float)span % (DAY_PERIOD * sequence.Length));
|
||||||
|
var index = spanMinusFullSequences / DAY_PERIOD;
|
||||||
|
SaveToConfig("sequenceIndex", index.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool CheckSequenceAndIncrementIndex()
|
||||||
|
{
|
||||||
|
var sequenceIndex = GetSequenceIndex();
|
||||||
|
var sequence = GetSequence();
|
||||||
|
if (string.IsNullOrEmpty(sequence))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
var result = sequence[sequenceIndex] == '!';
|
||||||
|
sequenceIndex = (sequenceIndex + 1) % sequence.Length;
|
||||||
|
SaveToConfig("sequenceIndex", sequenceIndex.ToString());
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int GetSequenceIndex()
|
||||||
|
{
|
||||||
|
var sequenceIndexToken = Functions.GetConfigItem("sequenceIndex");
|
||||||
|
if (sequenceIndexToken.Type == JTokenType.Boolean)
|
||||||
|
{
|
||||||
|
SaveToConfig("sequenceIndex", "0");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return (int)sequenceIndexToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetSequence()
|
||||||
|
{
|
||||||
|
var sequenceIndexToken = Functions.GetConfigItem("sequence");
|
||||||
|
if (sequenceIndexToken.Type == JTokenType.Boolean)
|
||||||
|
{
|
||||||
|
SaveToConfig("sequence", "");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return sequenceIndexToken.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SaveToConfig(string key, string data)
|
private void SaveToConfig(string key, string data)
|
||||||
|
|||||||
Reference in New Issue
Block a user