NadekoBot/NadekoBot.Core/Modules/Administration/Services/AdministrationService.cs

52 lines
1.6 KiB
C#
Raw Normal View History

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;
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
{
public class AdministrationService : INService
2017-05-27 08:19:27 +00:00
{
public readonly ConcurrentHashSet<ulong> DeleteMessagesOnCommand;
private readonly Logger _log;
private readonly NadekoBot _bot;
2017-05-27 08:19:27 +00:00
public AdministrationService(NadekoBot bot, CommandHandler cmdHandler)
2017-05-27 08:19:27 +00:00
{
_log = LogManager.GetCurrentClassLogger();
_bot = bot;
2017-05-27 08:19:27 +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;
}
}
}