2017-07-17 19:42:36 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using NadekoBot.Common.Replacements;
|
2017-07-17 02:37:51 +00:00
|
|
|
|
using NadekoBot.Modules.Music.Services;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Services;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
using NadekoBot.Services.Database.Models;
|
|
|
|
|
using NLog;
|
|
|
|
|
|
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 MusicService _music;
|
|
|
|
|
private readonly Logger _log;
|
2017-06-28 00:44:30 +00:00
|
|
|
|
private readonly Replacer _rep;
|
|
|
|
|
private readonly DbService _db;
|
|
|
|
|
public BotConfig BotConfig { get; private set; } //todo load whole botconifg, not just for this service when you have the time
|
2017-05-27 17:42:23 +00:00
|
|
|
|
|
|
|
|
|
private class TimerState
|
|
|
|
|
{
|
|
|
|
|
public int Index { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 00:44:30 +00:00
|
|
|
|
public PlayingRotateService(DiscordSocketClient client, BotConfig bc, MusicService music, DbService db)
|
2017-05-27 17:42:23 +00:00
|
|
|
|
{
|
|
|
|
|
_client = client;
|
2017-06-28 00:44:30 +00:00
|
|
|
|
BotConfig = bc;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
_music = music;
|
2017-06-28 00:44:30 +00:00
|
|
|
|
_db = db;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
2017-06-28 00:44:30 +00:00
|
|
|
|
_rep = new ReplacementBuilder()
|
|
|
|
|
.WithClient(client)
|
|
|
|
|
.WithStats(client)
|
2017-07-09 10:43:14 +00:00
|
|
|
|
.WithMusic(music)
|
2017-06-28 00:44:30 +00:00
|
|
|
|
.Build();
|
2017-05-27 17:42:23 +00:00
|
|
|
|
|
|
|
|
|
_t = new Timer(async (objState) =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-06-28 00:44:30 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
|
|
|
|
BotConfig = uow.BotConfig.GetOrCreate();
|
|
|
|
|
}
|
2017-05-27 17:42:23 +00:00
|
|
|
|
var state = (TimerState)objState;
|
2017-06-28 00:44:30 +00:00
|
|
|
|
if (!BotConfig.RotatingStatuses)
|
2017-05-27 17:42:23 +00:00
|
|
|
|
return;
|
2017-06-28 00:44:30 +00:00
|
|
|
|
if (state.Index >= BotConfig.RotatingStatusMessages.Count)
|
2017-05-27 17:42:23 +00:00
|
|
|
|
state.Index = 0;
|
|
|
|
|
|
2017-06-28 00:44:30 +00:00
|
|
|
|
if (!BotConfig.RotatingStatusMessages.Any())
|
2017-05-27 17:42:23 +00:00
|
|
|
|
return;
|
2017-06-28 00:44:30 +00:00
|
|
|
|
var status = BotConfig.RotatingStatusMessages[state.Index++].Status;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(status))
|
|
|
|
|
return;
|
2017-06-28 00:44:30 +00:00
|
|
|
|
|
|
|
|
|
status = _rep.Replace(status);
|
|
|
|
|
|
2017-06-19 13:42:10 +00:00
|
|
|
|
try { await client.SetGameAsync(status).ConfigureAwait(false); }
|
|
|
|
|
catch (Exception ex)
|
2017-05-27 17:42:23 +00:00
|
|
|
|
{
|
2017-06-19 13:42:10 +00:00
|
|
|
|
_log.Warn(ex);
|
2017-05-27 17:42:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Warn("Rotating playing status errored.\n" + ex);
|
|
|
|
|
}
|
|
|
|
|
}, new TimerState(), TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|