2017-07-17 19:42:36 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using NadekoBot.Common.Replacements;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services;
|
|
|
|
|
using NadekoBot.Core.Services.Database.Models;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
using NLog;
|
2017-10-15 09:48:29 +00:00
|
|
|
|
using NadekoBot.Modules.Music.Services;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
|
2017-07-17 19:42:36 +00:00
|
|
|
|
namespace NadekoBot.Modules.Administration.Services
|
2017-05-27 17:42:23 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
public class PlayingRotateService : INService
|
2017-05-27 17:42:23 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly Timer _t;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
private readonly DiscordSocketClient _client;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
private readonly Logger _log;
|
2017-09-14 17:37:41 +00:00
|
|
|
|
private readonly IDataCache _cache;
|
2017-06-28 00:44:30 +00:00
|
|
|
|
private readonly Replacer _rep;
|
|
|
|
|
private readonly DbService _db;
|
2017-07-25 16:31:30 +00:00
|
|
|
|
private readonly IBotConfigProvider _bcp;
|
|
|
|
|
|
|
|
|
|
public BotConfig BotConfig => _bcp.BotConfig;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
|
|
|
|
|
private class TimerState
|
|
|
|
|
{
|
|
|
|
|
public int Index { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-29 22:46:33 +00:00
|
|
|
|
public PlayingRotateService(DiscordSocketClient client, IBotConfigProvider bcp,
|
2017-10-15 09:48:29 +00:00
|
|
|
|
DbService db, IDataCache cache, NadekoBot bot, MusicService music)
|
2017-05-27 17:42:23 +00:00
|
|
|
|
{
|
|
|
|
|
_client = client;
|
2017-07-25 16:31:30 +00:00
|
|
|
|
_bcp = bcp;
|
2017-06-28 00:44:30 +00:00
|
|
|
|
_db = db;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
2017-09-14 17:37:41 +00:00
|
|
|
|
_cache = cache;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
|
2017-09-14 17:37:41 +00:00
|
|
|
|
if (client.ShardId == 0)
|
2017-05-27 17:42:23 +00:00
|
|
|
|
{
|
2017-09-14 17:37:41 +00:00
|
|
|
|
|
|
|
|
|
_rep = new ReplacementBuilder()
|
|
|
|
|
.WithClient(client)
|
|
|
|
|
.WithStats(client)
|
2017-10-15 09:48:29 +00:00
|
|
|
|
.WithMusic(music)
|
2017-09-14 17:37:41 +00:00
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
_t = new Timer(async (objState) =>
|
2017-05-27 17:42:23 +00:00
|
|
|
|
{
|
2017-09-14 17:37:41 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
bcp.Reload();
|
2017-07-25 16:31:30 +00:00
|
|
|
|
|
2017-09-14 17:37:41 +00:00
|
|
|
|
var state = (TimerState)objState;
|
|
|
|
|
if (!BotConfig.RotatingStatuses)
|
|
|
|
|
return;
|
|
|
|
|
if (state.Index >= BotConfig.RotatingStatusMessages.Count)
|
|
|
|
|
state.Index = 0;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
|
2017-09-14 17:37:41 +00:00
|
|
|
|
if (!BotConfig.RotatingStatusMessages.Any())
|
|
|
|
|
return;
|
|
|
|
|
var status = BotConfig.RotatingStatusMessages[state.Index++].Status;
|
|
|
|
|
if (string.IsNullOrWhiteSpace(status))
|
|
|
|
|
return;
|
2017-06-28 00:44:30 +00:00
|
|
|
|
|
2017-09-14 17:37:41 +00:00
|
|
|
|
status = _rep.Replace(status);
|
2017-06-28 00:44:30 +00:00
|
|
|
|
|
2017-09-14 17:37:41 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await bot.SetGameAsync(status).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Warn(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-19 13:42:10 +00:00
|
|
|
|
catch (Exception ex)
|
2017-05-27 17:42:23 +00:00
|
|
|
|
{
|
2017-09-14 17:37:41 +00:00
|
|
|
|
_log.Warn("Rotating playing status errored.\n" + ex);
|
2017-05-27 17:42:23 +00:00
|
|
|
|
}
|
2017-09-14 17:37:41 +00:00
|
|
|
|
}, new TimerState(), TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
|
|
|
|
|
}
|
2017-05-27 17:42:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|