Added .forwardtochannel which will forward messages to the current channel. It has lower priority than fwtoall

This commit is contained in:
Kwoth
2022-10-17 22:18:55 +02:00
parent 3f9a3c4c18
commit 27ac948463
8 changed files with 80 additions and 18 deletions

View File

@@ -12,7 +12,7 @@ namespace NadekoBot.Common.Configs;
public sealed partial class BotConfig : ICloneable<BotConfig> public sealed partial class BotConfig : ICloneable<BotConfig>
{ {
[Comment(@"DO NOT CHANGE")] [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 [Comment(@"Most commands, when executed, have a small colored line
next to the response. The color depends whether the command next to the response. The color depends whether the command
@@ -40,6 +40,10 @@ Allowed values: Simple, Normal, None")]
or all owners? (this might cause the bot to lag if there's a lot of owners specified)")] or all owners? (this might cause the bot to lag if there's a lot of owners specified)")]
public bool ForwardToAllOwners { get; set; } 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 [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. 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")] Supports embeds. How it looks: https://puu.sh/B0BLV.png")]

View File

@@ -230,6 +230,19 @@ public partial class Administration
await ReplyPendingLocalizedAsync(strs.fwall_stop); 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] [Cmd]
public async Task ShardStats(int page = 1) public async Task ShardStats(int page = 1)
{ {

View File

@@ -85,12 +85,12 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService
await using var uow = _db.GetDbContext(); await using var uow = _db.GetDbContext();
autoCommands = uow.AutoCommands.AsNoTracking() autoCommands = uow.AutoCommands.AsNoTracking()
.Where(x => x.Interval >= 5) .Where(x => x.Interval >= 5)
.AsEnumerable() .AsEnumerable()
.GroupBy(x => x.GuildId) .GroupBy(x => x.GuildId)
.ToDictionary(x => x.Key, .ToDictionary(x => x.Key,
y => y.ToDictionary(x => x.Id, TimerFromAutoCommand).ToConcurrent()) y => y.ToDictionary(x => x.Id, TimerFromAutoCommand).ToConcurrent())
.ToConcurrent(); .ToConcurrent();
var startupCommands = uow.AutoCommands.AsNoTracking().Where(x => x.Interval == 0); var startupCommands = uow.AutoCommands.AsNoTracking().Where(x => x.Interval == 0);
foreach (var cmd in startupCommands) foreach (var cmd in startupCommands)
@@ -169,18 +169,18 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService
private async Task LoadOwnerChannels() private async Task LoadOwnerChannels()
{ {
var channels = await _creds.OwnerIds.Select(id => var channels = await _creds.OwnerIds.Select(id =>
{ {
var user = _client.GetUser(id); var user = _client.GetUser(id);
if (user is null) if (user is null)
return Task.FromResult<IDMChannel>(null); return Task.FromResult<IDMChannel>(null);
return user.CreateDMChannelAsync(); return user.CreateDMChannelAsync();
}) })
.WhenAll(); .WhenAll();
ownerChannels = channels.Where(x => x is not null) ownerChannels = channels.Where(x => x is not null)
.ToDictionary(x => x.Recipient.Id, x => x) .ToDictionary(x => x.Recipient.Id, x => x)
.ToImmutableDictionary(); .ToImmutableDictionary();
if (!ownerChannels.Any()) if (!ownerChannels.Any())
{ {
@@ -202,7 +202,7 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService
public async Task ExecOnNoCommandAsync(IGuild guild, IUserMessage msg) public async Task ExecOnNoCommandAsync(IGuild guild, IUserMessage msg)
{ {
var bs = _bss.Data; 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})"; 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 else
{ {
var firstOwnerChannel = ownerChannels.Values.First(); var firstOwnerChannel = ownerChannels.Values.First();
@@ -333,6 +345,20 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService
return isToAll; 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() private void HandleStatusChanges()
=> _pubSub.Sub(_activitySetKey, => _pubSub.Sub(_activitySetKey,
async data => async data =>

View File

@@ -56,5 +56,11 @@ public sealed class BotConfigService : ConfigServiceBase<BotConfig>
c.Version = 4; c.Version = 4;
c.CheckForUpdates = true; c.CheckForUpdates = true;
}); });
if(data.Version < 5)
ModifyConfig(c =>
{
c.Version = 5;
});
} }
} }

View File

@@ -744,6 +744,10 @@ forwardmessages:
forwardtoall: forwardtoall:
- forwardtoall - forwardtoall
- fwtoall - fwtoall
forwardtochannel:
- forwardtochannel
- fwtoch
- fwtochannel
resetperms: resetperms:
- resetperms - resetperms
antiraid: antiraid:

View File

@@ -1,5 +1,5 @@
# DO NOT CHANGE # DO NOT CHANGE
version: 4 version: 5
# Most commands, when executed, have a small colored line # Most commands, when executed, have a small colored line
# next to the response. The color depends whether the command # next to the response. The color depends whether the command
# is completed, errored or in progress (pending) # 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), # 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) # or all owners? (this might cause the bot to lag if there's a lot of owners specified)
forwardToAllOwners: false 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 # 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. # 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 # Supports embeds. How it looks: https://puu.sh/B0BLV.png

View File

@@ -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" desc: "Toggles whether messages will be forwarded to all bot owners or only to the first one specified in the creds.yml file"
args: args:
- "" - ""
forwardtochannel:
desc: "Toggles forwarding of non-command messages sent to bot's DM to the current channel"
args:
- ""
resetperms: resetperms:
desc: "Resets the bot's permissions module on this server to the default value." desc: "Resets the bot's permissions module on this server to the default value."
args: args:

View File

@@ -68,6 +68,8 @@
"fwall_stop": "I will forward DMs only to the first owner.", "fwall_stop": "I will forward DMs only to the first owner.",
"fwdm_start": "I will forward DMs from now on.", "fwdm_start": "I will forward DMs from now on.",
"fwdm_stop": "I will stop forwarding 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_off": "Automatic deletion of greet messages has been disabled.",
"greetdel_on": "Greet messages will be deleted after {0} seconds.", "greetdel_on": "Greet messages will be deleted after {0} seconds.",
"greetdmmsg_cur": "Current DM greet message: {0}", "greetdmmsg_cur": "Current DM greet message: {0}",