diff --git a/src/NadekoBot/Common/Configs/BotConfig.cs b/src/NadekoBot/Common/Configs/BotConfig.cs index 11fe60beb..9570271a5 100644 --- a/src/NadekoBot/Common/Configs/BotConfig.cs +++ b/src/NadekoBot/Common/Configs/BotConfig.cs @@ -12,7 +12,7 @@ namespace NadekoBot.Common.Configs; public sealed partial class BotConfig : ICloneable { [Comment(@"DO NOT CHANGE")] - public int Version { get; set; } = 4; + public int Version { get; set; } = 5; [Comment(@"Most commands, when executed, have a small colored line next to the response. The color depends whether the command @@ -39,6 +39,10 @@ Allowed values: Simple, Normal, None")] @"Do you want the message to be forwarded only to the first owner specified in the list of owners (in creds.yml), or all owners? (this might cause the bot to lag if there's a lot of owners specified)")] public bool ForwardToAllOwners { get; set; } + + [Comment(@"Any messages sent by users in Bot's DM to be forwarded to the specified channel. +This option will only work when ForwardToAllOwners is set to false")] + public ulong? ForwardToChannel { get; set; } [Comment(@"When a user DMs the bot with a message which is not a command they will receive this message. Leave empty for no response. The string which will be sent whenever someone DMs the bot. diff --git a/src/NadekoBot/Modules/Administration/Self/SelfCommands.cs b/src/NadekoBot/Modules/Administration/Self/SelfCommands.cs index bd8f8d974..257a0b8d4 100644 --- a/src/NadekoBot/Modules/Administration/Self/SelfCommands.cs +++ b/src/NadekoBot/Modules/Administration/Self/SelfCommands.cs @@ -230,6 +230,19 @@ public partial class Administration await ReplyPendingLocalizedAsync(strs.fwall_stop); } + [Cmd] + [RequireContext(ContextType.Guild)] + [OwnerOnly] + public async Task ForwardToChannel() + { + var enabled = _service.ForwardToChannel(ctx.Channel.Id); + + if (enabled) + await ReplyConfirmLocalizedAsync(strs.fwch_start); + else + await ReplyPendingLocalizedAsync(strs.fwch_stop); + } + [Cmd] public async Task ShardStats(int page = 1) { diff --git a/src/NadekoBot/Modules/Administration/Self/SelfService.cs b/src/NadekoBot/Modules/Administration/Self/SelfService.cs index a17a6cb92..fa8a70ca0 100644 --- a/src/NadekoBot/Modules/Administration/Self/SelfService.cs +++ b/src/NadekoBot/Modules/Administration/Self/SelfService.cs @@ -85,12 +85,12 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService await using var uow = _db.GetDbContext(); autoCommands = uow.AutoCommands.AsNoTracking() - .Where(x => x.Interval >= 5) - .AsEnumerable() - .GroupBy(x => x.GuildId) - .ToDictionary(x => x.Key, - y => y.ToDictionary(x => x.Id, TimerFromAutoCommand).ToConcurrent()) - .ToConcurrent(); + .Where(x => x.Interval >= 5) + .AsEnumerable() + .GroupBy(x => x.GuildId) + .ToDictionary(x => x.Key, + y => y.ToDictionary(x => x.Id, TimerFromAutoCommand).ToConcurrent()) + .ToConcurrent(); var startupCommands = uow.AutoCommands.AsNoTracking().Where(x => x.Interval == 0); foreach (var cmd in startupCommands) @@ -169,18 +169,18 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService private async Task LoadOwnerChannels() { var channels = await _creds.OwnerIds.Select(id => - { - var user = _client.GetUser(id); - if (user is null) - return Task.FromResult(null); + { + var user = _client.GetUser(id); + if (user is null) + return Task.FromResult(null); - return user.CreateDMChannelAsync(); - }) - .WhenAll(); + return user.CreateDMChannelAsync(); + }) + .WhenAll(); ownerChannels = channels.Where(x => x is not null) - .ToDictionary(x => x.Recipient.Id, x => x) - .ToImmutableDictionary(); + .ToDictionary(x => x.Recipient.Id, x => x) + .ToImmutableDictionary(); if (!ownerChannels.Any()) { @@ -202,7 +202,7 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService public async Task ExecOnNoCommandAsync(IGuild guild, IUserMessage msg) { var bs = _bss.Data; - if (msg.Channel is IDMChannel && bs.ForwardMessages && ownerChannels.Any()) + if (msg.Channel is IDMChannel && bs.ForwardMessages && (ownerChannels.Any() || bs.ForwardToChannel is not null)) { var title = _strings.GetText(strs.dm_from) + $" [{msg.Author}]({msg.Author.Id})"; @@ -232,6 +232,18 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService } } } + else if (bs.ForwardToChannel is ulong cid) + { + try + { + if (_client.GetChannel(cid) is ITextChannel ch) + await ch.SendConfirmAsync(_eb, title, toSend); + } + catch + { + Log.Warning("Error forwarding message to the channel"); + } + } else { var firstOwnerChannel = ownerChannels.Values.First(); @@ -333,6 +345,20 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService return isToAll; } + public bool ForwardToChannel(ulong? channelId) + { + using var uow = _db.GetDbContext(); + + _bss.ModifyConfig(config => + { + config.ForwardToChannel = channelId == config.ForwardToChannel + ? null + : channelId; + }); + + return channelId is not null; + } + private void HandleStatusChanges() => _pubSub.Sub(_activitySetKey, async data => diff --git a/src/NadekoBot/Services/Settings/BotConfigService.cs b/src/NadekoBot/Services/Settings/BotConfigService.cs index 2644db8f0..62c5c6ea2 100644 --- a/src/NadekoBot/Services/Settings/BotConfigService.cs +++ b/src/NadekoBot/Services/Settings/BotConfigService.cs @@ -56,5 +56,11 @@ public sealed class BotConfigService : ConfigServiceBase c.Version = 4; c.CheckForUpdates = true; }); + + if(data.Version < 5) + ModifyConfig(c => + { + c.Version = 5; + }); } } \ No newline at end of file diff --git a/src/NadekoBot/data/aliases.yml b/src/NadekoBot/data/aliases.yml index 18e913248..55eb2363c 100644 --- a/src/NadekoBot/data/aliases.yml +++ b/src/NadekoBot/data/aliases.yml @@ -744,6 +744,10 @@ forwardmessages: forwardtoall: - forwardtoall - fwtoall +forwardtochannel: + - forwardtochannel + - fwtoch + - fwtochannel resetperms: - resetperms antiraid: diff --git a/src/NadekoBot/data/bot.yml b/src/NadekoBot/data/bot.yml index 8eed650eb..b8f0fe760 100644 --- a/src/NadekoBot/data/bot.yml +++ b/src/NadekoBot/data/bot.yml @@ -1,5 +1,5 @@ # DO NOT CHANGE -version: 4 +version: 5 # Most commands, when executed, have a small colored line # next to the response. The color depends whether the command # is completed, errored or in progress (pending) @@ -25,6 +25,9 @@ forwardMessages: false # Do you want the message to be forwarded only to the first owner specified in the list of owners (in creds.yml), # or all owners? (this might cause the bot to lag if there's a lot of owners specified) forwardToAllOwners: false +# Any messages sent by users in Bot's DM to be forwarded to the specified channel. +# This option will only work when ForwardToAllOwners is set to false +forwardToChannel: # When a user DMs the bot with a message which is not a command # they will receive this message. Leave empty for no response. The string which will be sent whenever someone DMs the bot. # Supports embeds. How it looks: https://puu.sh/B0BLV.png diff --git a/src/NadekoBot/data/strings/commands/commands.en-US.yml b/src/NadekoBot/data/strings/commands/commands.en-US.yml index c036cde7b..2baba9fc1 100644 --- a/src/NadekoBot/data/strings/commands/commands.en-US.yml +++ b/src/NadekoBot/data/strings/commands/commands.en-US.yml @@ -1257,6 +1257,10 @@ forwardtoall: desc: "Toggles whether messages will be forwarded to all bot owners or only to the first one specified in the creds.yml file" args: - "" +forwardtochannel: + desc: "Toggles forwarding of non-command messages sent to bot's DM to the current channel" + args: + - "" resetperms: desc: "Resets the bot's permissions module on this server to the default value." args: diff --git a/src/NadekoBot/data/strings/responses/responses.en-US.json b/src/NadekoBot/data/strings/responses/responses.en-US.json index 85fc68bcf..e40f70923 100644 --- a/src/NadekoBot/data/strings/responses/responses.en-US.json +++ b/src/NadekoBot/data/strings/responses/responses.en-US.json @@ -68,6 +68,8 @@ "fwall_stop": "I will forward DMs only to the first owner.", "fwdm_start": "I will forward DMs from now on.", "fwdm_stop": "I will stop forwarding DMs from now on.", + "fwch_start": "Any message sent to bot's DMs will be forwarded to this channel.", + "fwch_stop": "Messages sent to bot's DMs will no longer be forwarded to this channel.", "greetdel_off": "Automatic deletion of greet messages has been disabled.", "greetdel_on": "Greet messages will be deleted after {0} seconds.", "greetdmmsg_cur": "Current DM greet message: {0}",