2017-07-17 19:42:36 +00:00
|
|
|
|
using System;
|
2017-05-27 08:19:27 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using NadekoBot.Common.Collections;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NLog;
|
2017-05-27 08:19:27 +00:00
|
|
|
|
|
2017-07-17 19:42:36 +00:00
|
|
|
|
namespace NadekoBot.Modules.Administration.Services
|
2017-05-27 08:19:27 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
public class AdministrationService : INService
|
2017-05-27 08:19:27 +00:00
|
|
|
|
{
|
|
|
|
|
public readonly ConcurrentHashSet<ulong> DeleteMessagesOnCommand;
|
|
|
|
|
private readonly Logger _log;
|
2017-10-13 00:21:39 +00:00
|
|
|
|
private readonly NadekoBot _bot;
|
2017-05-27 08:19:27 +00:00
|
|
|
|
|
2017-10-13 00:21:39 +00:00
|
|
|
|
public AdministrationService(NadekoBot bot, CommandHandler cmdHandler)
|
2017-05-27 08:19:27 +00:00
|
|
|
|
{
|
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
2017-10-13 00:21:39 +00:00
|
|
|
|
_bot = bot;
|
2017-05-27 08:19:27 +00:00
|
|
|
|
|
2017-10-30 13:38:50 +00:00
|
|
|
|
DeleteMessagesOnCommand = new ConcurrentHashSet<ulong>(bot.AllGuildConfigs
|
|
|
|
|
.Where(g => g.DeleteMessageOnCommand)
|
|
|
|
|
.Select(g => g.GuildId));
|
2017-05-27 08:19:27 +00:00
|
|
|
|
cmdHandler.CommandExecuted += DelMsgOnCmd_Handler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task DelMsgOnCmd_Handler(IUserMessage msg, CommandInfo cmd)
|
|
|
|
|
{
|
|
|
|
|
var _ = Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var channel = msg.Channel as SocketTextChannel;
|
|
|
|
|
if (channel == null)
|
|
|
|
|
return;
|
|
|
|
|
if (DeleteMessagesOnCommand.Contains(channel.Guild.Id) && cmd.Name != "prune" && cmd.Name != "pick")
|
|
|
|
|
await msg.DeleteAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Warn("Delmsgoncmd errored...");
|
|
|
|
|
_log.Warn(ex);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|