Fixed some stuff in rotating statuses, more optimal too

This commit is contained in:
Kwoth 2016-10-15 01:20:06 +02:00
parent fd56e14fd3
commit 4001cf2e41

View File

@ -22,6 +22,18 @@ namespace NadekoBot.Modules.Administration
public class PlayingRotateCommands
{
private Logger _log { get; }
public static List<PlayingStatus> RotatingStatusMessages { get; }
public static bool RotatingStatuses { get; private set; } = false;
static PlayingRotateCommands()
{
using (var uow = DbHandler.UnitOfWork())
{
var conf = uow.BotConfig.GetOrCreate();
RotatingStatusMessages = conf.RotatingStatusMessages;
RotatingStatuses = conf.RotatingStatuses;
}
}
public PlayingRotateCommands()
{
@ -33,22 +45,16 @@ namespace NadekoBot.Modules.Administration
{
try
{
BotConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.BotConfig.GetOrCreate();
}
if (!conf.RotatingStatuses)
if (!RotatingStatuses)
continue;
else
{
if (index >= conf.RotatingStatusMessages.Count)
if (index >= RotatingStatusMessages.Count)
index = 0;
if (!conf.RotatingStatusMessages.Any())
if (!RotatingStatusMessages.Any())
continue;
var status = conf.RotatingStatusMessages[index++].Status;
var status = RotatingStatusMessages[index++].Status;
if (string.IsNullOrWhiteSpace(status))
continue;
PlayingPlaceholders.ForEach(e => status = status.Replace(e.Key, e.Value()));
@ -94,16 +100,15 @@ namespace NadekoBot.Modules.Administration
public async Task RotatePlaying(IUserMessage umsg)
{
var channel = (ITextChannel)umsg.Channel;
bool status;
using (var uow = DbHandler.UnitOfWork())
{
var config = uow.BotConfig.GetOrCreate();
status = config.RotatingStatuses = !config.RotatingStatuses;
RotatingStatuses = config.RotatingStatuses = !config.RotatingStatuses;
await uow.CompleteAsync();
}
if (status)
if (RotatingStatuses)
await channel.SendMessageAsync("`Rotating playing status enabled.`");
else
await channel.SendMessageAsync("`Rotating playing status disabled.`");
@ -119,7 +124,9 @@ namespace NadekoBot.Modules.Administration
using (var uow = DbHandler.UnitOfWork())
{
var config = uow.BotConfig.GetOrCreate();
config.RotatingStatusMessages.Add(new PlayingStatus { Status = status });
var toAdd = new PlayingStatus { Status = status };
config.RotatingStatusMessages.Add(toAdd);
RotatingStatusMessages.Add(toAdd);
await uow.CompleteAsync();
}
@ -132,19 +139,14 @@ namespace NadekoBot.Modules.Administration
public async Task ListPlaying(IUserMessage umsg)
{
var channel = (ITextChannel)umsg.Channel;
List<PlayingStatus> statuses;
using (var uow = DbHandler.UnitOfWork())
{
statuses = uow.BotConfig.GetOrCreate().RotatingStatusMessages;
}
if (!statuses.Any())
if (!RotatingStatusMessages.Any())
await channel.SendMessageAsync("`No rotating playing statuses set.`");
else
{
var i = 1;
await channel.SendMessageAsync($"{umsg.Author.Mention} `Here is a list of rotating statuses:`\n\n\t" + string.Join("\n\t", statuses.Select(rs => $"`{i++}.` {rs.Status}")));
await channel.SendMessageAsync($"{umsg.Author.Mention} `Here is a list of rotating statuses:`\n\n\t" + string.Join("\n\t", RotatingStatusMessages.Select(rs => $"`{i++}.` {rs.Status}")));
}
}
@ -166,6 +168,7 @@ namespace NadekoBot.Modules.Administration
return;
msg = config.RotatingStatusMessages[index].Status;
config.RotatingStatusMessages.RemoveAt(index);
RotatingStatusMessages.RemoveAt(index);
await uow.CompleteAsync();
}
await channel.SendMessageAsync($"`Removed the the playing message:` {msg}").ConfigureAwait(false);