dm foward commands cleaned up and localized

This commit is contained in:
Kwoth 2017-02-14 17:15:53 +01:00
parent 45fb8b925d
commit a89ca8d185
3 changed files with 76 additions and 22 deletions

View File

@ -4,10 +4,9 @@ using Discord.WebSocket;
using NadekoBot.Attributes; using NadekoBot.Attributes;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using NadekoBot.Services; using NadekoBot.Services;
using NLog;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace NadekoBot.Modules.Administration namespace NadekoBot.Modules.Administration
@ -15,19 +14,21 @@ namespace NadekoBot.Modules.Administration
public partial class Administration public partial class Administration
{ {
[Group] [Group]
public class DMForwardCommands : NadekoSubmodule public class DmForwardCommands : NadekoSubmodule
{ {
private static bool ForwardDMs { get; set; } private static volatile bool _forwardDMs;
private static bool ForwardDMsToAllOwners { get; set; } private static volatile bool _forwardDMsToAllOwners;
private static readonly object _locker = new object();
static DMForwardCommands() static DmForwardCommands()
{ {
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
var config = uow.BotConfig.GetOrCreate(); var config = uow.BotConfig.GetOrCreate();
ForwardDMs = config.ForwardMessages; _forwardDMs = config.ForwardMessages;
ForwardDMsToAllOwners = config.ForwardToAllOwners; _forwardDMsToAllOwners = config.ForwardToAllOwners;
} }
} }
@ -38,13 +39,14 @@ namespace NadekoBot.Modules.Administration
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
var config = uow.BotConfig.GetOrCreate(); var config = uow.BotConfig.GetOrCreate();
ForwardDMs = config.ForwardMessages = !config.ForwardMessages; lock(_locker)
_forwardDMs = config.ForwardMessages = !config.ForwardMessages;
uow.Complete(); uow.Complete();
} }
if (ForwardDMs) if (_forwardDMs)
await Context.Channel.SendConfirmAsync("✅ **I will forward DMs from now on.**").ConfigureAwait(false); await ReplyConfirmLocalized("fwdm_start").ConfigureAwait(false);
else else
await Context.Channel.SendConfirmAsync("🆗 **I will stop forwarding DMs from now on.**").ConfigureAwait(false); await ReplyConfirmLocalized("fwdm_stop").ConfigureAwait(false);
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
@ -54,22 +56,25 @@ namespace NadekoBot.Modules.Administration
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
var config = uow.BotConfig.GetOrCreate(); var config = uow.BotConfig.GetOrCreate();
ForwardDMsToAllOwners = config.ForwardToAllOwners = !config.ForwardToAllOwners; lock(_locker)
_forwardDMsToAllOwners = config.ForwardToAllOwners = !config.ForwardToAllOwners;
uow.Complete(); uow.Complete();
} }
if (ForwardDMsToAllOwners) if (_forwardDMsToAllOwners)
await Context.Channel.SendConfirmAsync(" **I will forward DMs to all owners.**").ConfigureAwait(false); await ReplyConfirmLocalized("fwall_start").ConfigureAwait(false);
else else
await Context.Channel.SendConfirmAsync(" **I will forward DMs only to the first owner.**").ConfigureAwait(false); await ReplyConfirmLocalized("fwall_stop").ConfigureAwait(false);
} }
public static async Task HandleDMForwarding(SocketMessage msg, List<IDMChannel> ownerChannels) public static async Task HandleDmForwarding(SocketMessage msg, List<IDMChannel> ownerChannels)
{ {
if (ForwardDMs && ownerChannels.Any()) if (_forwardDMs && ownerChannels.Any())
{ {
var title = $"DM from [{msg.Author}]({msg.Author.Id})"; var title =
if (ForwardDMsToAllOwners) GetTextStatic("dm_from", NadekoBot.Localization.DefaultCultureInfo,
typeof(Administration).Name.ToLowerInvariant()) + $" [{msg.Author}]({msg.Author.Id})";
if (_forwardDMsToAllOwners)
{ {
await Task.WhenAll(ownerChannels.Where(ch => ch.Recipient.Id != msg.Author.Id) await Task.WhenAll(ownerChannels.Where(ch => ch.Recipient.Id != msg.Author.Id)
.Select(ch => ch.SendConfirmAsync(title, msg.Content))).ConfigureAwait(false); .Select(ch => ch.SendConfirmAsync(title, msg.Content))).ConfigureAwait(false);
@ -78,7 +83,11 @@ namespace NadekoBot.Modules.Administration
{ {
var firstOwnerChannel = ownerChannels.First(); var firstOwnerChannel = ownerChannels.First();
if (firstOwnerChannel.Recipient.Id != msg.Author.Id) if (firstOwnerChannel.Recipient.Id != msg.Author.Id)
try { await firstOwnerChannel.SendConfirmAsync(title, msg.Content).ConfigureAwait(false); } catch { } try { await firstOwnerChannel.SendConfirmAsync(title, msg.Content).ConfigureAwait(false); }
catch
{
// ignored
}
} }
} }
} }

View File

@ -77,6 +77,51 @@ namespace NadekoBot.Resources {
} }
} }
/// <summary>
/// Looks up a localized string similar to DM from.
/// </summary>
public static string administration_dm_from {
get {
return ResourceManager.GetString("administration_dm_from", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to I will forward DMs to all owners..
/// </summary>
public static string administration_fwall_start {
get {
return ResourceManager.GetString("administration_fwall_start", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to I will forward DMs only to the first owner..
/// </summary>
public static string administration_fwall_stop {
get {
return ResourceManager.GetString("administration_fwall_stop", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to I will forward DMs from now on..
/// </summary>
public static string administration_fwdm_start {
get {
return ResourceManager.GetString("administration_fwdm_start", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to I will stop forwarding DMs from now on..
/// </summary>
public static string administration_fwdm_stop {
get {
return ResourceManager.GetString("administration_fwdm_stop", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to I don&apos;t have the permission necessary for that most likely.. /// Looks up a localized string similar to I don&apos;t have the permission necessary for that most likely..
/// </summary> /// </summary>

View File

@ -273,7 +273,7 @@ namespace NadekoBot.Services
await msg.Channel.SendMessageAsync(Help.DMHelpString).ConfigureAwait(false); await msg.Channel.SendMessageAsync(Help.DMHelpString).ConfigureAwait(false);
await DMForwardCommands.HandleDMForwarding(msg, ownerChannels).ConfigureAwait(false); await DmForwardCommands.HandleDmForwarding(msg, ownerChannels).ConfigureAwait(false);
} }
} }
} }