NadekoBot/NadekoBot.Modules.Utility/Common/RepeatRunner.cs

108 lines
3.2 KiB
C#
Raw Normal View History

2017-07-17 19:42:36 +00:00
using System;
using System.Threading;
using System.Threading.Tasks;
using Discord;
2017-05-22 23:59:31 +00:00
using Discord.Net;
using Discord.WebSocket;
using NadekoBot.Extensions;
using NadekoBot.Core.Services.Database.Models;
2017-05-22 23:59:31 +00:00
using NLog;
2017-07-17 19:42:36 +00:00
namespace NadekoBot.Modules.Utility.Common
2017-05-22 23:59:31 +00:00
{
public class RepeatRunner
{
private readonly Logger _log;
public Repeater Repeater { get; }
public SocketGuild Guild { get; }
public ITextChannel Channel { get; private set; }
public TimeSpan InitialInterval { get; private set; }
2017-05-22 23:59:31 +00:00
private IUserMessage oldMsg = null;
2017-06-12 23:40:39 +00:00
private Timer _t;
2017-05-22 23:59:31 +00:00
2017-07-26 16:19:39 +00:00
public RepeatRunner(DiscordSocketClient client, SocketGuild guild, Repeater repeater)
2017-05-22 23:59:31 +00:00
{
_log = LogManager.GetCurrentClassLogger();
Repeater = repeater;
2017-07-26 16:19:39 +00:00
Guild = guild;
InitialInterval = Repeater.Interval;
2017-07-26 16:19:39 +00:00
Run();
2017-05-22 23:59:31 +00:00
}
2017-06-12 23:40:39 +00:00
private void Run()
2017-05-22 23:59:31 +00:00
{
if (Repeater.StartTimeOfDay != null)
{
if ((InitialInterval = Repeater.StartTimeOfDay.Value - DateTime.UtcNow.TimeOfDay) < TimeSpan.Zero)
InitialInterval += TimeSpan.FromDays(1);
}
2017-05-22 23:59:31 +00:00
2017-06-12 23:40:39 +00:00
_t = new Timer(async (_) => {
try { await Trigger().ConfigureAwait(false); } catch { }
}, null, InitialInterval, Repeater.Interval);
2017-05-22 23:59:31 +00:00
}
public async Task Trigger()
{
var toSend = "🔄 " + Repeater.Message;
//var lastMsgInChannel = (await Channel.GetMessagesAsync(2)).FirstOrDefault();
// if (lastMsgInChannel.Id == oldMsg?.Id) //don't send if it's the same message in the channel
// continue;
if (oldMsg != null)
try
{
await oldMsg.DeleteAsync();
}
catch
{
// ignored
}
try
{
if (Channel == null)
Channel = Guild.GetTextChannel(Repeater.ChannelId);
if (Channel != null)
oldMsg = await Channel.SendMessageAsync(toSend.SanitizeMentions()).ConfigureAwait(false);
}
catch (HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.Forbidden)
{
_log.Warn("Missing permissions. Repeater stopped. ChannelId : {0}", Channel?.Id);
return;
}
catch (HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.NotFound)
{
_log.Warn("Channel not found. Repeater stopped. ChannelId : {0}", Channel?.Id);
return;
}
catch (Exception ex)
{
_log.Warn(ex);
}
}
public void Reset()
{
2017-06-12 23:40:39 +00:00
Stop();
Run();
2017-05-22 23:59:31 +00:00
}
public void Stop()
{
2017-06-12 23:40:39 +00:00
_t.Change(Timeout.Infinite, Timeout.Infinite);
2017-05-22 23:59:31 +00:00
}
public override string ToString() =>
$"{Channel?.Mention ?? $"<#{Repeater.ChannelId}>" } " +
$"| {(int)Repeater.Interval.TotalHours}:{Repeater.Interval:mm} " +
$"| {Repeater.Message.TrimTo(33)}";
}
}