2017-05-27 23:51:22 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.WebSocket;
|
2017-06-28 00:44:30 +00:00
|
|
|
|
using NadekoBot.DataStructures.Replacements;
|
2017-05-27 23:51:22 +00:00
|
|
|
|
using NadekoBot.Extensions;
|
2017-06-22 21:59:54 +00:00
|
|
|
|
using NadekoBot.Services.Database;
|
2017-05-27 23:51:22 +00:00
|
|
|
|
using NadekoBot.Services.Database.Models;
|
|
|
|
|
using NLog;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Services.Utility
|
|
|
|
|
{
|
|
|
|
|
public class RemindService
|
|
|
|
|
{
|
|
|
|
|
public readonly Regex Regex = new Regex(@"^(?:(?<months>\d)mo)?(?:(?<weeks>\d)w)?(?:(?<days>\d{1,2})d)?(?:(?<hours>\d{1,2})h)?(?:(?<minutes>\d{1,2})m)?$",
|
|
|
|
|
RegexOptions.Compiled | RegexOptions.Multiline);
|
|
|
|
|
|
|
|
|
|
public string RemindMessageFormat { get; }
|
|
|
|
|
|
|
|
|
|
private readonly Logger _log;
|
|
|
|
|
private readonly CancellationTokenSource cancelSource;
|
|
|
|
|
private readonly CancellationToken cancelAllToken;
|
|
|
|
|
private readonly BotConfig _config;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
private readonly DiscordSocketClient _client;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
private readonly DbService _db;
|
2017-05-27 23:51:22 +00:00
|
|
|
|
|
2017-06-22 21:59:54 +00:00
|
|
|
|
public RemindService(DiscordSocketClient client, BotConfig config, DbService db,
|
|
|
|
|
List<long> guilds, IUnitOfWork uow)
|
2017-05-27 23:51:22 +00:00
|
|
|
|
{
|
|
|
|
|
_config = config;
|
|
|
|
|
_client = client;
|
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
|
|
|
|
_db = db;
|
|
|
|
|
|
|
|
|
|
cancelSource = new CancellationTokenSource();
|
|
|
|
|
cancelAllToken = cancelSource.Token;
|
2017-06-22 21:59:54 +00:00
|
|
|
|
|
|
|
|
|
var reminders = uow.Reminders.GetIncludedReminders(guilds).ToList();
|
2017-05-27 23:51:22 +00:00
|
|
|
|
RemindMessageFormat = _config.RemindMessageFormat;
|
|
|
|
|
|
|
|
|
|
foreach (var r in reminders)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(() => StartReminder(r));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task StartReminder(Reminder r)
|
|
|
|
|
{
|
|
|
|
|
var t = cancelAllToken;
|
2017-06-12 23:40:39 +00:00
|
|
|
|
var now = DateTime.UtcNow;
|
2017-05-27 23:51:22 +00:00
|
|
|
|
|
|
|
|
|
var time = r.When - now;
|
|
|
|
|
|
|
|
|
|
if (time.TotalMilliseconds > int.MaxValue)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
await Task.Delay(time, t).ConfigureAwait(false);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
IMessageChannel ch;
|
|
|
|
|
if (r.IsPrivate)
|
|
|
|
|
{
|
|
|
|
|
var user = _client.GetGuild(r.ServerId).GetUser(r.ChannelId);
|
|
|
|
|
if (user == null)
|
|
|
|
|
return;
|
|
|
|
|
ch = await user.CreateDMChannelAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ch = _client.GetGuild(r.ServerId)?.GetTextChannel(r.ChannelId);
|
|
|
|
|
}
|
|
|
|
|
if (ch == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2017-06-28 00:44:30 +00:00
|
|
|
|
var rep = new ReplacementBuilder()
|
|
|
|
|
.WithOverride("%user%", () => $"<@!{r.UserId}>")
|
|
|
|
|
.WithOverride("%message%", () => r.Message)
|
|
|
|
|
.WithOverride("%target%", () => r.IsPrivate ? "Direct Message" : $"<#{r.ChannelId}>")
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
await ch.SendMessageAsync(rep.Replace(RemindMessageFormat).SanitizeMentions()).ConfigureAwait(false); //it works trust me
|
2017-05-27 23:51:22 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) { _log.Warn(ex); }
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
|
|
|
|
uow.Reminders.Remove(r);
|
|
|
|
|
await uow.CompleteAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|