2017-05-22 23:59:31 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Net;
|
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using NadekoBot.Extensions;
|
|
|
|
|
using NadekoBot.Services.Database.Models;
|
|
|
|
|
using NLog;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2017-05-27 23:51:22 +00:00
|
|
|
|
namespace NadekoBot.Services.Utility
|
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; }
|
|
|
|
|
private IUserMessage oldMsg = null;
|
2017-06-12 23:40:39 +00:00
|
|
|
|
private Timer _t;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
|
|
|
|
|
public RepeatRunner(DiscordShardedClient client, Repeater repeater, ITextChannel channel = null)
|
|
|
|
|
{
|
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
|
|
|
|
Repeater = repeater;
|
|
|
|
|
Channel = channel;
|
|
|
|
|
|
2017-05-29 04:13:22 +00:00
|
|
|
|
//todo 40 @.@ fix all of this
|
2017-05-22 23:59:31 +00:00
|
|
|
|
Guild = client.GetGuild(repeater.GuildId);
|
|
|
|
|
if (Guild != null)
|
2017-06-12 23:40: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
|
|
|
|
{
|
2017-06-12 23:40:39 +00:00
|
|
|
|
TimeSpan initialInterval = Repeater.Interval;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
|
2017-06-12 23:40:39 +00:00
|
|
|
|
//if (Repeater.StartTimeOfDay != null)
|
|
|
|
|
//{
|
|
|
|
|
// if ((initialInterval = Repeater.StartTimeOfDay.Value - DateTime.UtcNow.TimeOfDay) < TimeSpan.Zero)
|
|
|
|
|
// initialInterval += TimeSpan.FromDays(1);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
_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)}";
|
|
|
|
|
}
|
|
|
|
|
}
|