From 64d43fed748d47be09dfe066e64c0c2be8959db6 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Tue, 14 Feb 2017 17:22:57 +0100 Subject: [PATCH] moved dmfoward commands to selfcommands --- .../Commands/DMForwardCommands.cs | 96 ------------------- .../Administration/Commands/SelfCommands.cs | 86 ++++++++++++++++- src/NadekoBot/Services/CommandHandler.cs | 2 +- 3 files changed, 83 insertions(+), 101 deletions(-) delete mode 100644 src/NadekoBot/Modules/Administration/Commands/DMForwardCommands.cs diff --git a/src/NadekoBot/Modules/Administration/Commands/DMForwardCommands.cs b/src/NadekoBot/Modules/Administration/Commands/DMForwardCommands.cs deleted file mode 100644 index ef6d1231..00000000 --- a/src/NadekoBot/Modules/Administration/Commands/DMForwardCommands.cs +++ /dev/null @@ -1,96 +0,0 @@ -using Discord; -using Discord.Commands; -using Discord.WebSocket; -using NadekoBot.Attributes; -using NadekoBot.Extensions; -using NadekoBot.Services; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; - -namespace NadekoBot.Modules.Administration -{ - public partial class Administration - { - [Group] - public class DmForwardCommands : NadekoSubmodule - { - private static volatile bool _forwardDMs; - private static volatile bool _forwardDMsToAllOwners; - - private static readonly object _locker = new object(); - - static DmForwardCommands() - { - - using (var uow = DbHandler.UnitOfWork()) - { - var config = uow.BotConfig.GetOrCreate(); - _forwardDMs = config.ForwardMessages; - _forwardDMsToAllOwners = config.ForwardToAllOwners; - } - } - - [NadekoCommand, Usage, Description, Aliases] - [OwnerOnly] - public async Task ForwardMessages() - { - using (var uow = DbHandler.UnitOfWork()) - { - var config = uow.BotConfig.GetOrCreate(); - lock(_locker) - _forwardDMs = config.ForwardMessages = !config.ForwardMessages; - uow.Complete(); - } - if (_forwardDMs) - await ReplyConfirmLocalized("fwdm_start").ConfigureAwait(false); - else - await ReplyConfirmLocalized("fwdm_stop").ConfigureAwait(false); - } - - [NadekoCommand, Usage, Description, Aliases] - [OwnerOnly] - public async Task ForwardToAll() - { - using (var uow = DbHandler.UnitOfWork()) - { - var config = uow.BotConfig.GetOrCreate(); - lock(_locker) - _forwardDMsToAllOwners = config.ForwardToAllOwners = !config.ForwardToAllOwners; - uow.Complete(); - } - if (_forwardDMsToAllOwners) - await ReplyConfirmLocalized("fwall_start").ConfigureAwait(false); - else - await ReplyConfirmLocalized("fwall_stop").ConfigureAwait(false); - - } - - public static async Task HandleDmForwarding(SocketMessage msg, List ownerChannels) - { - if (_forwardDMs && ownerChannels.Any()) - { - var title = - 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) - .Select(ch => ch.SendConfirmAsync(title, msg.Content))).ConfigureAwait(false); - } - else - { - var firstOwnerChannel = ownerChannels.First(); - if (firstOwnerChannel.Recipient.Id != msg.Author.Id) - try { await firstOwnerChannel.SendConfirmAsync(title, msg.Content).ConfigureAwait(false); } - catch - { - // ignored - } - } - } - } - } - } -} diff --git a/src/NadekoBot/Modules/Administration/Commands/SelfCommands.cs b/src/NadekoBot/Modules/Administration/Commands/SelfCommands.cs index 3361eff4..b67f5d3e 100644 --- a/src/NadekoBot/Modules/Administration/Commands/SelfCommands.cs +++ b/src/NadekoBot/Modules/Administration/Commands/SelfCommands.cs @@ -3,11 +3,13 @@ using Discord.Commands; using NadekoBot.Attributes; using NadekoBot.Extensions; using System; -using System.Diagnostics; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading.Tasks; +using Discord.WebSocket; +using NadekoBot.Services; namespace NadekoBot.Modules.Administration { @@ -16,6 +18,82 @@ namespace NadekoBot.Modules.Administration [Group] public class SelfCommands : NadekoSubmodule { + private static volatile bool _forwardDMs; + private static volatile bool _forwardDMsToAllOwners; + + private static readonly object _locker = new object(); + + static SelfCommands() + { + using (var uow = DbHandler.UnitOfWork()) + { + var config = uow.BotConfig.GetOrCreate(); + _forwardDMs = config.ForwardMessages; + _forwardDMsToAllOwners = config.ForwardToAllOwners; + } + } + + [NadekoCommand, Usage, Description, Aliases] + [OwnerOnly] + public async Task ForwardMessages() + { + using (var uow = DbHandler.UnitOfWork()) + { + var config = uow.BotConfig.GetOrCreate(); + lock (_locker) + _forwardDMs = config.ForwardMessages = !config.ForwardMessages; + uow.Complete(); + } + if (_forwardDMs) + await ReplyConfirmLocalized("fwdm_start").ConfigureAwait(false); + else + await ReplyConfirmLocalized("fwdm_stop").ConfigureAwait(false); + } + + [NadekoCommand, Usage, Description, Aliases] + [OwnerOnly] + public async Task ForwardToAll() + { + using (var uow = DbHandler.UnitOfWork()) + { + var config = uow.BotConfig.GetOrCreate(); + lock (_locker) + _forwardDMsToAllOwners = config.ForwardToAllOwners = !config.ForwardToAllOwners; + uow.Complete(); + } + if (_forwardDMsToAllOwners) + await ReplyConfirmLocalized("fwall_start").ConfigureAwait(false); + else + await ReplyConfirmLocalized("fwall_stop").ConfigureAwait(false); + + } + + public static async Task HandleDmForwarding(SocketMessage msg, List ownerChannels) + { + if (_forwardDMs && ownerChannels.Any()) + { + var title = + 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) + .Select(ch => ch.SendConfirmAsync(title, msg.Content))).ConfigureAwait(false); + } + else + { + var firstOwnerChannel = ownerChannels.First(); + if (firstOwnerChannel.Recipient.Id != msg.Author.Id) + try { await firstOwnerChannel.SendConfirmAsync(title, msg.Content).ConfigureAwait(false); } + catch + { + // ignored + } + } + } + } + + [NadekoCommand, Usage, Description, Aliases] [OwnerOnly] public async Task ConnectShard(int shardid) @@ -148,7 +226,7 @@ namespace NadekoBot.Modules.Administration if (ids.Length != 2) return; var sid = ulong.Parse(ids[0]); - var server = NadekoBot.Client.GetGuilds().Where(s => s.Id == sid).FirstOrDefault(); + var server = NadekoBot.Client.GetGuilds().FirstOrDefault(s => s.Id == sid); if (server == null) return; @@ -156,7 +234,7 @@ namespace NadekoBot.Modules.Administration if (ids[1].ToUpperInvariant().StartsWith("C:")) { var cid = ulong.Parse(ids[1].Substring(2)); - var ch = server.TextChannels.Where(c => c.Id == cid).FirstOrDefault(); + var ch = server.TextChannels.FirstOrDefault(c => c.Id == cid); if (ch == null) { return; @@ -166,7 +244,7 @@ namespace NadekoBot.Modules.Administration else if (ids[1].ToUpperInvariant().StartsWith("U:")) { var uid = ulong.Parse(ids[1].Substring(2)); - var user = server.Users.Where(u => u.Id == uid).FirstOrDefault(); + var user = server.Users.FirstOrDefault(u => u.Id == uid); if (user == null) { return; diff --git a/src/NadekoBot/Services/CommandHandler.cs b/src/NadekoBot/Services/CommandHandler.cs index 4dcfd461..a944a21e 100644 --- a/src/NadekoBot/Services/CommandHandler.cs +++ b/src/NadekoBot/Services/CommandHandler.cs @@ -273,7 +273,7 @@ namespace NadekoBot.Services await msg.Channel.SendMessageAsync(Help.DMHelpString).ConfigureAwait(false); - await DmForwardCommands.HandleDmForwarding(msg, ownerChannels).ConfigureAwait(false); + await SelfCommands.HandleDmForwarding(msg, ownerChannels).ConfigureAwait(false); } } }