Message forwarding with appropriate commands
This commit is contained in:
parent
6f0a78613e
commit
e92db040be
@ -0,0 +1,89 @@
|
|||||||
|
using Discord;
|
||||||
|
using Discord.Commands;
|
||||||
|
using NadekoBot.Attributes;
|
||||||
|
using NadekoBot.Services;
|
||||||
|
using NadekoBot.Services.Database;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace NadekoBot.Modules.Administration
|
||||||
|
{
|
||||||
|
public partial class Administration
|
||||||
|
{
|
||||||
|
[Group]
|
||||||
|
public class DMForwardCommands
|
||||||
|
{
|
||||||
|
private static bool ForwardDMs { get; set; }
|
||||||
|
private static bool ForwardDMsToAllOwners { get; set; }
|
||||||
|
|
||||||
|
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(IUserMessage imsg)
|
||||||
|
{
|
||||||
|
var channel = imsg.Channel;
|
||||||
|
|
||||||
|
using (var uow = DbHandler.UnitOfWork())
|
||||||
|
{
|
||||||
|
var config = uow.BotConfig.GetOrCreate();
|
||||||
|
ForwardDMs = config.ForwardMessages = !config.ForwardMessages;
|
||||||
|
uow.Complete();
|
||||||
|
}
|
||||||
|
if (ForwardDMs)
|
||||||
|
await channel.SendMessageAsync("`I will forward DMs from now on.`").ConfigureAwait(false);
|
||||||
|
else
|
||||||
|
await channel.SendMessageAsync("`I will stop forwarding DMs.`").ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
[NadekoCommand, Usage, Description, Aliases]
|
||||||
|
[OwnerOnly]
|
||||||
|
public async Task ForwardToAll(IUserMessage imsg)
|
||||||
|
{
|
||||||
|
var channel = imsg.Channel;
|
||||||
|
|
||||||
|
using (var uow = DbHandler.UnitOfWork())
|
||||||
|
{
|
||||||
|
var config = uow.BotConfig.GetOrCreate();
|
||||||
|
ForwardDMsToAllOwners = config.ForwardToAllOwners = !config.ForwardToAllOwners;
|
||||||
|
uow.Complete();
|
||||||
|
}
|
||||||
|
if (ForwardDMsToAllOwners)
|
||||||
|
await channel.SendMessageAsync("`I will forward DMs to all owners.`").ConfigureAwait(false);
|
||||||
|
else
|
||||||
|
await channel.SendMessageAsync("`I will forward DMs only to the first owner.`").ConfigureAwait(false);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task HandleDMForwarding(IMessage msg, List<IDMChannel> ownerChannels)
|
||||||
|
{
|
||||||
|
if (ForwardDMs && ownerChannels.Any())
|
||||||
|
{
|
||||||
|
var toSend = $"`I received a message from {msg.Author} ({msg.Author.Id})`: {msg.Content}";
|
||||||
|
if (ForwardDMsToAllOwners)
|
||||||
|
{
|
||||||
|
var msgs = await Task.WhenAll(ownerChannels.Where(ch => ch.Recipient.Id != msg.Author.Id)
|
||||||
|
.Select(ch => ch.SendMessageAsync(toSend))).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var firstOwnerChannel = ownerChannels.First();
|
||||||
|
if (firstOwnerChannel.Recipient.Id != msg.Author.Id)
|
||||||
|
try { await firstOwnerChannel.SendMessageAsync(msg.Content).ConfigureAwait(false); } catch { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -88,6 +88,9 @@ namespace NadekoBot
|
|||||||
{
|
{
|
||||||
ModulePrefixes = new ReadOnlyDictionary<string, string>(uow.BotConfig.GetOrCreate().ModulePrefixes.ToDictionary(m => m.ModuleName, m => m.Prefix));
|
ModulePrefixes = new ReadOnlyDictionary<string, string>(uow.BotConfig.GetOrCreate().ModulePrefixes.ToDictionary(m => m.ModuleName, m => m.Prefix));
|
||||||
}
|
}
|
||||||
|
// start handling messages received in commandhandler
|
||||||
|
await CommandHandler.StartHandling();
|
||||||
|
|
||||||
await CommandService.LoadAssembly(Assembly.GetEntryAssembly(), depMap).ConfigureAwait(false);
|
await CommandService.LoadAssembly(Assembly.GetEntryAssembly(), depMap).ConfigureAwait(false);
|
||||||
|
|
||||||
Console.WriteLine(await Stats.Print().ConfigureAwait(false));
|
Console.WriteLine(await Stats.Print().ConfigureAwait(false));
|
||||||
|
54
src/NadekoBot/Resources/CommandStrings.Designer.cs
generated
54
src/NadekoBot/Resources/CommandStrings.Designer.cs
generated
@ -2381,6 +2381,60 @@ namespace NadekoBot.Resources {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to fwmsgs.
|
||||||
|
/// </summary>
|
||||||
|
public static string forwardmessages_cmd {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("forwardmessages_cmd", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to Toggles forwarding of non-command messages sent to bot's DM to the bot owners.
|
||||||
|
/// </summary>
|
||||||
|
public static string forwardmessages_desc {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("forwardmessages_desc", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to `.fwmsgs`.
|
||||||
|
/// </summary>
|
||||||
|
public static string forwardmessages_usage {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("forwardmessages_usage", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to fwtoall.
|
||||||
|
/// </summary>
|
||||||
|
public static string forwardtoall_cmd {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("forwardtoall_cmd", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to Toggles whether messages will be forwarded to all bot owners or only to the first one specified in the credentials.json.
|
||||||
|
/// </summary>
|
||||||
|
public static string forwardtoall_desc {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("forwardtoall_desc", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to `.fwtoall`.
|
||||||
|
/// </summary>
|
||||||
|
public static string forwardtoall_usage {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("forwardtoall_usage", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Looks up a localized string similar to gelbooru.
|
/// Looks up a localized string similar to gelbooru.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -2619,4 +2619,22 @@
|
|||||||
<data name="shuffledeck_usage" xml:space="preserve">
|
<data name="shuffledeck_usage" xml:space="preserve">
|
||||||
<value>`$sh`</value>
|
<value>`$sh`</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="forwardmessages_cmd" xml:space="preserve">
|
||||||
|
<value>fwmsgs</value>
|
||||||
|
</data>
|
||||||
|
<data name="forwardmessages_desc" xml:space="preserve">
|
||||||
|
<value>Toggles forwarding of non-command messages sent to bot's DM to the bot owners</value>
|
||||||
|
</data>
|
||||||
|
<data name="forwardmessages_usage" xml:space="preserve">
|
||||||
|
<value>`.fwmsgs`</value>
|
||||||
|
</data>
|
||||||
|
<data name="forwardtoall_cmd" xml:space="preserve">
|
||||||
|
<value>fwtoall</value>
|
||||||
|
</data>
|
||||||
|
<data name="forwardtoall_desc" xml:space="preserve">
|
||||||
|
<value>Toggles whether messages will be forwarded to all bot owners or only to the first one specified in the credentials.json</value>
|
||||||
|
</data>
|
||||||
|
<data name="forwardtoall_usage" xml:space="preserve">
|
||||||
|
<value>`.fwtoall`</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
@ -17,15 +17,30 @@ using NadekoBot.Extensions;
|
|||||||
using static NadekoBot.Modules.Permissions.Permissions;
|
using static NadekoBot.Modules.Permissions.Permissions;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using NadekoBot.Modules.Help;
|
using NadekoBot.Modules.Help;
|
||||||
|
using static NadekoBot.Modules.Administration.Administration;
|
||||||
|
|
||||||
namespace NadekoBot.Services
|
namespace NadekoBot.Services
|
||||||
{
|
{
|
||||||
|
public class IGuildUserComparer : IEqualityComparer<IGuildUser>
|
||||||
|
{
|
||||||
|
public bool Equals(IGuildUser x, IGuildUser y)
|
||||||
|
{
|
||||||
|
return x.Id == y.Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetHashCode(IGuildUser obj)
|
||||||
|
{
|
||||||
|
return obj.Id.GetHashCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
public class CommandHandler
|
public class CommandHandler
|
||||||
{
|
{
|
||||||
private ShardedDiscordClient _client;
|
private ShardedDiscordClient _client;
|
||||||
private CommandService _commandService;
|
private CommandService _commandService;
|
||||||
private Logger _log;
|
private Logger _log;
|
||||||
|
|
||||||
|
private List<IDMChannel> ownerChannels { get; set; }
|
||||||
|
|
||||||
public event EventHandler<CommandExecutedEventArgs> CommandExecuted = delegate { };
|
public event EventHandler<CommandExecutedEventArgs> CommandExecuted = delegate { };
|
||||||
|
|
||||||
public CommandHandler(ShardedDiscordClient client, CommandService commandService)
|
public CommandHandler(ShardedDiscordClient client, CommandService commandService)
|
||||||
@ -33,6 +48,20 @@ namespace NadekoBot.Services
|
|||||||
_client = client;
|
_client = client;
|
||||||
_commandService = commandService;
|
_commandService = commandService;
|
||||||
_log = LogManager.GetCurrentClassLogger();
|
_log = LogManager.GetCurrentClassLogger();
|
||||||
|
}
|
||||||
|
public async Task StartHandling()
|
||||||
|
{
|
||||||
|
ownerChannels = (await Task.WhenAll(_client.GetGuilds().SelectMany(g => g.GetUsers())
|
||||||
|
.Where(u => NadekoBot.Credentials.OwnerIds.Contains(u.Id))
|
||||||
|
.Distinct(new IGuildUserComparer())
|
||||||
|
.Select(async u => { try { return await u.CreateDMChannelAsync(); } catch { return null; } })))
|
||||||
|
.Where(ch => ch != null)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
if (!ownerChannels.Any())
|
||||||
|
_log.Warn("No owner channels created! Make sure you've specified correct OwnerId in the credentials.json file.");
|
||||||
|
else
|
||||||
|
_log.Info($"Created {ownerChannels.Count} out of {NadekoBot.Credentials.OwnerIds.Length} owner message channels.");
|
||||||
|
|
||||||
_client.MessageReceived += MessageReceivedHandler;
|
_client.MessageReceived += MessageReceivedHandler;
|
||||||
}
|
}
|
||||||
@ -110,7 +139,7 @@ namespace NadekoBot.Services
|
|||||||
permRole = config.PermissionRole.Trim().ToLowerInvariant();
|
permRole = config.PermissionRole.Trim().ToLowerInvariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var throwaway = Task.Run(async () =>
|
var throwaway = Task.Run(async () =>
|
||||||
@ -158,7 +187,7 @@ namespace NadekoBot.Services
|
|||||||
if (guild != null && command != null && result.Error == CommandError.Exception)
|
if (guild != null && command != null && result.Error == CommandError.Exception)
|
||||||
{
|
{
|
||||||
if (verbose)
|
if (verbose)
|
||||||
await msg.Channel.SendMessageAsync(":warning: " + result.ErrorReason).ConfigureAwait(false);
|
try { await msg.Channel.SendMessageAsync(":warning: " + result.ErrorReason).ConfigureAwait(false); } catch { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -166,6 +195,8 @@ namespace NadekoBot.Services
|
|||||||
if (msg.Channel is IPrivateChannel)
|
if (msg.Channel is IPrivateChannel)
|
||||||
{
|
{
|
||||||
await msg.Channel.SendMessageAsync(Help.DMHelpString).ConfigureAwait(false);
|
await msg.Channel.SendMessageAsync(Help.DMHelpString).ConfigureAwait(false);
|
||||||
|
|
||||||
|
await DMForwardCommands.HandleDMForwarding(msg, ownerChannels);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -185,7 +216,7 @@ namespace NadekoBot.Services
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Tuple<Command,IResult>> ExecuteCommand(IUserMessage message, string input, IGuild guild, IUser user, Permission rootPerm, string permRole, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Best) {
|
public async Task<Tuple<Command, IResult>> ExecuteCommand(IUserMessage message, string input, IGuild guild, IUser user, Permission rootPerm, string permRole, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Best) {
|
||||||
var searchResult = _commandService.Search(message, input);
|
var searchResult = _commandService.Search(message, input);
|
||||||
if (!searchResult.IsSuccess)
|
if (!searchResult.IsSuccess)
|
||||||
return new Tuple<Command, IResult>(null, searchResult);
|
return new Tuple<Command, IResult>(null, searchResult);
|
||||||
|
Loading…
Reference in New Issue
Block a user