NadekoBot/NadekoBot.Core/Modules/Administration/Services/PlayingRotateService.cs

86 lines
2.7 KiB
C#
Raw Normal View History

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