2017-07-17 19:42:36 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord;
|
2017-05-27 23:51:22 +00:00
|
|
|
|
using Discord.WebSocket;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common.Replacements;
|
2017-05-27 23:51:22 +00:00
|
|
|
|
using NadekoBot.Extensions;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services;
|
|
|
|
|
using NadekoBot.Core.Services.Database.Models;
|
|
|
|
|
using NadekoBot.Core.Services.Impl;
|
2017-05-27 23:51:22 +00:00
|
|
|
|
using NLog;
|
2017-10-04 22:51:12 +00:00
|
|
|
|
using System.Collections.Generic;
|
2017-05-27 23:51:22 +00:00
|
|
|
|
|
2017-07-17 19:42:36 +00:00
|
|
|
|
namespace NadekoBot.Modules.Utility.Services
|
2017-05-27 23:51:22 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
public class RemindService : INService
|
2017-05-27 23:51:22 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
2017-07-20 03:10:39 +00:00
|
|
|
|
private readonly IBotConfigProvider _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-10-04 22:51:12 +00:00
|
|
|
|
public RemindService(DiscordSocketClient client,
|
|
|
|
|
IBotConfigProvider config,
|
|
|
|
|
DbService db,
|
|
|
|
|
StartingGuildsService guilds)
|
2017-05-27 23:51:22 +00:00
|
|
|
|
{
|
|
|
|
|
_config = config;
|
|
|
|
|
_client = client;
|
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
|
|
|
|
_db = db;
|
|
|
|
|
|
|
|
|
|
cancelSource = new CancellationTokenSource();
|
|
|
|
|
cancelAllToken = cancelSource.Token;
|
2017-10-04 22:51:12 +00:00
|
|
|
|
|
|
|
|
|
List<Reminder> reminders;
|
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
|
|
|
|
reminders = uow.Reminders.GetIncludedReminders(guilds).ToList();
|
|
|
|
|
}
|
2017-07-20 03:10:39 +00:00
|
|
|
|
RemindMessageFormat = _config.BotConfig.RemindMessageFormat;
|
2017-05-27 23:51:22 +00:00
|
|
|
|
|
|
|
|
|
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;
|
2017-07-05 15:38:38 +00:00
|
|
|
|
ch = await user.GetOrCreateDMChannelAsync().ConfigureAwait(false);
|
2017-05-27 23:51:22 +00:00
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|