localized playing rotate commands

This commit is contained in:
Kwoth 2017-02-14 19:54:02 +01:00
parent b24e68c24c
commit b890506de6
3 changed files with 118 additions and 38 deletions

View File

@ -1,5 +1,4 @@
using Discord; using Discord.Commands;
using Discord.Commands;
using Discord.WebSocket; using Discord.WebSocket;
using NadekoBot.Attributes; using NadekoBot.Attributes;
using NadekoBot.Extensions; using NadekoBot.Extensions;
@ -8,7 +7,6 @@ using NadekoBot.Services.Database.Models;
using NLog; using NLog;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -20,14 +18,15 @@ namespace NadekoBot.Modules.Administration
[Group] [Group]
public class PlayingRotateCommands : NadekoSubmodule public class PlayingRotateCommands : NadekoSubmodule
{ {
private static Logger _log { get; }
public static List<PlayingStatus> RotatingStatusMessages { get; } public static List<PlayingStatus> RotatingStatusMessages { get; }
public static bool RotatingStatuses { get; private set; } = false; public static volatile bool RotatingStatuses;
private static Timer _t { get; } private readonly object _locker = new object();
private new static Logger _log { get; }
private static readonly Timer _t;
private class TimerState private class TimerState
{ {
public int Index { get; set; } = 0; public int Index { get; set; }
} }
static PlayingRotateCommands() static PlayingRotateCommands()
@ -37,8 +36,6 @@ namespace NadekoBot.Modules.Administration
RotatingStatusMessages = NadekoBot.BotConfig.RotatingStatusMessages; RotatingStatusMessages = NadekoBot.BotConfig.RotatingStatusMessages;
RotatingStatuses = NadekoBot.BotConfig.RotatingStatuses; RotatingStatuses = NadekoBot.BotConfig.RotatingStatuses;
_t = new Timer(async (objState) => _t = new Timer(async (objState) =>
{ {
try try
@ -46,26 +43,24 @@ namespace NadekoBot.Modules.Administration
var state = (TimerState)objState; var state = (TimerState)objState;
if (!RotatingStatuses) if (!RotatingStatuses)
return; return;
else if (state.Index >= RotatingStatusMessages.Count)
{ state.Index = 0;
if (state.Index >= RotatingStatusMessages.Count)
state.Index = 0;
if (!RotatingStatusMessages.Any()) if (!RotatingStatusMessages.Any())
return; return;
var status = RotatingStatusMessages[state.Index++].Status; var status = RotatingStatusMessages[state.Index++].Status;
if (string.IsNullOrWhiteSpace(status)) if (string.IsNullOrWhiteSpace(status))
return; return;
PlayingPlaceholders.ForEach(e => status = status.Replace(e.Key, e.Value())); PlayingPlaceholders.ForEach(e => status = status.Replace(e.Key, e.Value()));
var shards = NadekoBot.Client.Shards; var shards = NadekoBot.Client.Shards;
for (int i = 0; i < shards.Count; i++) for (int i = 0; i < shards.Count; i++)
{
var curShard = shards.ElementAt(i);
ShardSpecificPlaceholders.ForEach(e => status = status.Replace(e.Key, e.Value(curShard)));
try { await shards.ElementAt(i).SetGameAsync(status).ConfigureAwait(false); }
catch (Exception ex)
{ {
ShardSpecificPlaceholders.ForEach(e => status = status.Replace(e.Key, e.Value(shards.ElementAt(i)))); _log.Warn(ex);
try { await shards.ElementAt(i).SetGameAsync(status).ConfigureAwait(false); }
catch (Exception ex)
{
_log.Warn(ex);
}
} }
} }
} }
@ -107,17 +102,20 @@ namespace NadekoBot.Modules.Administration
[OwnerOnly] [OwnerOnly]
public async Task RotatePlaying() public async Task RotatePlaying()
{ {
using (var uow = DbHandler.UnitOfWork()) lock (_locker)
{ {
var config = uow.BotConfig.GetOrCreate(); using (var uow = DbHandler.UnitOfWork())
{
var config = uow.BotConfig.GetOrCreate();
RotatingStatuses = config.RotatingStatuses = !config.RotatingStatuses; RotatingStatuses = config.RotatingStatuses = !config.RotatingStatuses;
await uow.CompleteAsync(); uow.Complete();
}
} }
if (RotatingStatuses) if (RotatingStatuses)
await Context.Channel.SendConfirmAsync("🆗 **Rotating playing status enabled.**").ConfigureAwait(false); await ReplyConfirmLocalized("ropl_enabled").ConfigureAwait(false);
else else
await Context.Channel.SendConfirmAsync(" **Rotating playing status disabled.**").ConfigureAwait(false); await ReplyConfirmLocalized("ropl_disabled").ConfigureAwait(false);
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
@ -133,7 +131,7 @@ namespace NadekoBot.Modules.Administration
await uow.CompleteAsync(); await uow.CompleteAsync();
} }
await Context.Channel.SendConfirmAsync("✅ **Added.**").ConfigureAwait(false); await ReplyConfirmLocalized("ropl_added").ConfigureAwait(false);
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
@ -141,11 +139,13 @@ namespace NadekoBot.Modules.Administration
public async Task ListPlaying() public async Task ListPlaying()
{ {
if (!RotatingStatusMessages.Any()) if (!RotatingStatusMessages.Any())
await Context.Channel.SendErrorAsync("❎ **No rotating playing statuses set.**"); await ReplyErrorLocalized("ropl_not_set").ConfigureAwait(false);
else else
{ {
var i = 1; var i = 1;
await Context.Channel.SendConfirmAsync($" {Context.User.Mention} `Here is a list of rotating statuses:`\n\n\t" + string.Join("\n\t", RotatingStatusMessages.Select(rs => $"`{i++}.` {rs.Status}"))); await ReplyConfirmLocalized("ropl_list",
string.Join("\n\t", RotatingStatusMessages.Select(rs => $"`{i++}.` {rs.Status}")))
.ConfigureAwait(false);
} }
} }
@ -156,7 +156,7 @@ namespace NadekoBot.Modules.Administration
{ {
index -= 1; index -= 1;
string msg = ""; string msg;
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
var config = uow.BotConfig.GetOrCreate(); var config = uow.BotConfig.GetOrCreate();
@ -168,7 +168,7 @@ namespace NadekoBot.Modules.Administration
RotatingStatusMessages.RemoveAt(index); RotatingStatusMessages.RemoveAt(index);
await uow.CompleteAsync(); await uow.CompleteAsync();
} }
await Context.Channel.SendConfirmAsync($"🗑 **Removed the the playing message:** {msg}").ConfigureAwait(false); await ReplyConfirmLocalized("reprm", msg).ConfigureAwait(false);
} }
} }
} }

View File

@ -447,6 +447,61 @@ namespace NadekoBot.Resources {
} }
} }
/// <summary>
/// Looks up a localized string similar to Removed the playing message: {0}.
/// </summary>
public static string administration_reprm {
get {
return ResourceManager.GetString("administration_reprm", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Added..
/// </summary>
public static string administration_ropl_added {
get {
return ResourceManager.GetString("administration_ropl_added", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Rotating playing status disabled..
/// </summary>
public static string administration_ropl_disabled {
get {
return ResourceManager.GetString("administration_ropl_disabled", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Rotating playing status enabled..
/// </summary>
public static string administration_ropl_enabled {
get {
return ResourceManager.GetString("administration_ropl_enabled", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Here is a list of rotating statuses:
///{0}.
/// </summary>
public static string administration_ropl_list {
get {
return ResourceManager.GetString("administration_ropl_list", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to No rotating playing statuses set..
/// </summary>
public static string administration_ropl_not_set {
get {
return ResourceManager.GetString("administration_ropl_not_set", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to soft-banned (kicked). /// Looks up a localized string similar to soft-banned (kicked).
/// </summary> /// </summary>

View File

@ -431,6 +431,25 @@
<data name="administration_old_topic" xml:space="preserve"> <data name="administration_old_topic" xml:space="preserve">
<value>Old Topic</value> <value>Old Topic</value>
</data> </data>
<data name="administration_reprm" xml:space="preserve">
<value>Removed the playing message: {0}</value>
</data>
<data name="administration_ropl_added" xml:space="preserve">
<value>Added.</value>
</data>
<data name="administration_ropl_disabled" xml:space="preserve">
<value>Rotating playing status disabled.</value>
</data>
<data name="administration_ropl_enabled" xml:space="preserve">
<value>Rotating playing status enabled.</value>
</data>
<data name="administration_ropl_list" xml:space="preserve">
<value>Here is a list of rotating statuses:
{0}</value>
</data>
<data name="administration_ropl_not_set" xml:space="preserve">
<value>No rotating playing statuses set.</value>
</data>
<data name="administration_soft_banned_pl" xml:space="preserve"> <data name="administration_soft_banned_pl" xml:space="preserve">
<value>soft-banned (kicked)</value> <value>soft-banned (kicked)</value>
<comment>PLURAL</comment> <comment>PLURAL</comment>
@ -514,6 +533,12 @@
<data name="administraton_user_unbanned" xml:space="preserve"> <data name="administraton_user_unbanned" xml:space="preserve">
<value>User Unbanned</value> <value>User Unbanned</value>
</data> </data>
<data name="adminsitration_migration_done" xml:space="preserve">
<value>Migration done!</value>
</data>
<data name="adminsitration_migration_error" xml:space="preserve">
<value>Error while migrating, check bot's console for more information.</value>
</data>
<data name="adminsitration_presence_updates" xml:space="preserve"> <data name="adminsitration_presence_updates" xml:space="preserve">
<value>Presence Updates</value> <value>Presence Updates</value>
</data> </data>