From 2be15c921d2d5badf692733350c1221c381eccb1 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Tue, 30 Aug 2016 02:17:21 +0200 Subject: [PATCH] Fixed delmsgoncmd, added CommandExecuted event --- .../Modules/Administration/Administration.cs | 31 ++++++++++++++++--- src/NadekoBot/Services/CommandHandler.cs | 15 +++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/NadekoBot/Modules/Administration/Administration.cs b/src/NadekoBot/Modules/Administration/Administration.cs index b96ec950..f0e8b7a5 100644 --- a/src/NadekoBot/Modules/Administration/Administration.cs +++ b/src/NadekoBot/Modules/Administration/Administration.cs @@ -14,7 +14,6 @@ using Discord.WebSocket; using NadekoBot.Services.Database; using NadekoBot.Services.Database.Models; -//todo fix delmsgoncmd namespace NadekoBot.Modules.Administration { [Module(".", AppendSpace = false)] @@ -22,8 +21,32 @@ namespace NadekoBot.Modules.Administration { public Administration(ILocalization loc, CommandService cmds, DiscordSocketClient client) : base(loc, cmds, client) { - + NadekoBot.CommandHandler.CommandExecuted += DelMsgOnCmd_Handler; } + + private void DelMsgOnCmd_Handler(object sender, CommandExecutedEventArgs e) + { + try + { + var channel = e.Message.Channel as ITextChannel; + if (channel == null) + return; + + bool shouldDelete; + using (var uow = DbHandler.UnitOfWork()) + { + shouldDelete = uow.GuildConfigs.For(channel.Guild.Id).DeleteMessageOnCommand; + } + + if (shouldDelete) + e.Message.DeleteAsync(); + } + catch (Exception ex) + { + _log.Warn(ex, "Delmsgoncmd errored..."); + } + } + ////todo owner only //[LocalizedCommand, LocalizedDescription, LocalizedSummary] //[RequireContext(ContextType.Guild)] @@ -36,7 +59,7 @@ namespace NadekoBot.Modules.Administration // System.Diagnostics.Process.Start(System.Reflection.Assembly.GetEntryAssembly().Location); // Environment.Exit(0); //} - + [LocalizedCommand, LocalizedDescription, LocalizedSummary] [RequireContext(ContextType.Guild)] [RequirePermission(GuildPermission.Administrator)] @@ -379,7 +402,6 @@ namespace NadekoBot.Modules.Administration public async Task CreatVoiChanl(IUserMessage umsg, [Remainder] string channelName) { var channel = (ITextChannel)umsg.Channel; - //todo actually print info about created channel var ch = await channel.Guild.CreateVoiceChannelAsync(channelName).ConfigureAwait(false); await channel.SendMessageAsync($"Created voice channel **{ch.Name}**, id `{ch.Id}`.").ConfigureAwait(false); } @@ -399,7 +421,6 @@ namespace NadekoBot.Modules.Administration public async Task CreaTxtChanl(IUserMessage umsg, [Remainder] string channelName) { var channel = (ITextChannel)umsg.Channel; - //todo actually print info about created channel var txtCh = await channel.Guild.CreateTextChannelAsync(channelName).ConfigureAwait(false); await channel.SendMessageAsync($"Added text channel **{txtCh.Name}**, id `{txtCh.Id}`.").ConfigureAwait(false); } diff --git a/src/NadekoBot/Services/CommandHandler.cs b/src/NadekoBot/Services/CommandHandler.cs index 8dc7fc4d..3ab26fe9 100644 --- a/src/NadekoBot/Services/CommandHandler.cs +++ b/src/NadekoBot/Services/CommandHandler.cs @@ -17,6 +17,8 @@ namespace NadekoBot.Services private CommandService _commandService; private Logger _log; + public event EventHandler CommandExecuted = async delegate { }; + public CommandHandler(DiscordSocketClient client, CommandService commandService) { _client = client; @@ -42,6 +44,7 @@ namespace NadekoBot.Services var channel = (usrMsg.Channel as ITextChannel); if (result.IsSuccess) { + CommandExecuted(this, new CommandExecutedEventArgs(usrMsg, command)); _log.Info("Command Executed after {4}s\n\t" + "User: {0}\n\t" + "Server: {1}\n\t" + @@ -75,4 +78,16 @@ namespace NadekoBot.Services return Task.CompletedTask; } } + + public class CommandExecutedEventArgs + { + public Command Command { get; } + public IUserMessage Message { get; } + + public CommandExecutedEventArgs(IUserMessage msg, Command cmd) + { + Message = msg; + Command = cmd; + } + } }