performance improvements?

This commit is contained in:
Kwoth 2017-02-02 14:16:28 +01:00
parent a0e23b4c2c
commit dfbabea8b9
2 changed files with 87 additions and 78 deletions

View File

@ -38,20 +38,24 @@ namespace NadekoBot.Modules.Administration
} }
private static async Task DelMsgOnCmd_Handler(SocketUserMessage msg, CommandInfo cmd) private static Task DelMsgOnCmd_Handler(SocketUserMessage msg, CommandInfo cmd)
{ {
try var _ = Task.Run(async () =>
{ {
var channel = msg.Channel as SocketTextChannel; try
if (channel == null) {
return; var channel = msg.Channel as SocketTextChannel;
if (DeleteMessagesOnCommand.Contains(channel.Guild.Id) && cmd.Name != "prune") if (channel == null)
await msg.DeleteAsync().ConfigureAwait(false); return;
} if (DeleteMessagesOnCommand.Contains(channel.Guild.Id) && cmd.Name != "prune")
catch (Exception ex) await msg.DeleteAsync().ConfigureAwait(false);
{ }
_log.Warn(ex, "Delmsgoncmd errored..."); catch (Exception ex)
} {
_log.Warn(ex, "Delmsgoncmd errored...");
}
});
return Task.CompletedTask;
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]

View File

@ -105,9 +105,8 @@ namespace NadekoBot.Services
BlacklistCommands.BlacklistedUsers.Contains(usrMsg.Author.Id); BlacklistCommands.BlacklistedUsers.Contains(usrMsg.Author.Id);
const float oneThousandth = 1.0f / 1000; const float oneThousandth = 1.0f / 1000;
private async Task LogSuccessfulExecution(SocketUserMessage usrMsg, ExecuteCommandResult exec, SocketTextChannel channel, int ticks) private Task LogSuccessfulExecution(SocketUserMessage usrMsg, ExecuteCommandResult exec, SocketTextChannel channel, int ticks)
{ {
await CommandExecuted(usrMsg, exec.CommandInfo).ConfigureAwait(false);
_log.Info("Command Executed after {4}s\n\t" + _log.Info("Command Executed after {4}s\n\t" +
"User: {0}\n\t" + "User: {0}\n\t" +
"Server: {1}\n\t" + "Server: {1}\n\t" +
@ -118,6 +117,7 @@ namespace NadekoBot.Services
(channel == null ? "PRIVATE" : channel.Name + " [" + channel.Id + "]"), // {2} (channel == null ? "PRIVATE" : channel.Name + " [" + channel.Id + "]"), // {2}
usrMsg.Content, // {3} usrMsg.Content, // {3}
ticks * oneThousandth); ticks * oneThousandth);
return Task.CompletedTask;
} }
private void LogErroredExecution(SocketUserMessage usrMsg, ExecuteCommandResult exec, SocketTextChannel channel, int ticks) private void LogErroredExecution(SocketUserMessage usrMsg, ExecuteCommandResult exec, SocketTextChannel channel, int ticks)
@ -184,93 +184,98 @@ namespace NadekoBot.Services
return false; return false;
} }
private async Task MessageReceivedHandler(SocketMessage msg) private Task MessageReceivedHandler(SocketMessage msg)
{ {
try var _ = Task.Run(async () =>
{ {
if (msg.Author.IsBot || !NadekoBot.Ready) //no bots, wait until bot connected and initialized try
return; {
if (msg.Author.IsBot || !NadekoBot.Ready) //no bots, wait until bot connected and initialized
return;
var execTime = Environment.TickCount; var execTime = Environment.TickCount;
var usrMsg = msg as SocketUserMessage; var usrMsg = msg as SocketUserMessage;
if (usrMsg == null) //has to be an user message, not system/other messages. if (usrMsg == null) //has to be an user message, not system/other messages.
return; return;
#if !GLOBAL_NADEKO #if !GLOBAL_NADEKO
// track how many messagges each user is sending // track how many messagges each user is sending
UserMessagesSent.AddOrUpdate(usrMsg.Author.Id, 1, (key, old) => ++old); UserMessagesSent.AddOrUpdate(usrMsg.Author.Id, 1, (key, old) => ++old);
#endif #endif
var channel = msg.Channel as SocketTextChannel; var channel = msg.Channel as SocketTextChannel;
var guild = channel?.Guild; var guild = channel?.Guild;
if (guild != null && guild.OwnerId != msg.Author.Id) if (guild != null && guild.OwnerId != msg.Author.Id)
{ {
if (await InviteFiltered(guild, usrMsg).ConfigureAwait(false)) if (await InviteFiltered(guild, usrMsg).ConfigureAwait(false))
return;
if (await WordFiltered(guild, usrMsg).ConfigureAwait(false))
return;
}
if (IsBlacklisted(guild, usrMsg))
return; return;
if (await WordFiltered(guild, usrMsg).ConfigureAwait(false)) var cleverBotRan = await Task.Run(() => TryRunCleverbot(usrMsg, guild)).ConfigureAwait(false);
if (cleverBotRan)
return; return;
}
if (IsBlacklisted(guild, usrMsg)) // maybe this message is a custom reaction
return; // todo log custom reaction executions. return struct with info
var crExecuted = await Task.Run(() => CustomReactions.TryExecuteCustomReaction(usrMsg)).ConfigureAwait(false);
if (crExecuted) //if it was, don't execute the command
return;
var cleverBotRan = await Task.Run(() => TryRunCleverbot(usrMsg, guild)).ConfigureAwait(false); string messageContent = usrMsg.Content;
if (cleverBotRan)
return;
// maybe this message is a custom reaction // execute the command and measure the time it took
// todo log custom reaction executions. return struct with info var exec = await Task.Run(() => ExecuteCommand(new CommandContext(_client, usrMsg), messageContent, DependencyMap.Empty, MultiMatchHandling.Best)).ConfigureAwait(false);
var crExecuted = await Task.Run(() => CustomReactions.TryExecuteCustomReaction(usrMsg)).ConfigureAwait(false); execTime = Environment.TickCount - execTime;
if (crExecuted) //if it was, don't execute the command
return;
string messageContent = usrMsg.Content; if (exec.Result.IsSuccess)
// execute the command and measure the time it took
var exec = await Task.Run(() => ExecuteCommand(new CommandContext(_client, usrMsg), messageContent, DependencyMap.Empty, MultiMatchHandling.Best)).ConfigureAwait(false);
execTime = Environment.TickCount - execTime;
if (exec.Result.IsSuccess)
{
await LogSuccessfulExecution(usrMsg, exec, channel, execTime).ConfigureAwait(false);
}
else if (!exec.Result.IsSuccess && exec.Result.Error != CommandError.UnknownCommand)
{
LogErroredExecution(usrMsg, exec, channel, execTime);
if (guild != null && exec.CommandInfo != null && exec.Result.Error == CommandError.Exception)
{ {
if (exec.PermissionCache != null && exec.PermissionCache.Verbose) await CommandExecuted(usrMsg, exec.CommandInfo).ConfigureAwait(false);
try { await msg.Channel.SendMessageAsync("⚠️ " + exec.Result.ErrorReason).ConfigureAwait(false); } catch { } await LogSuccessfulExecution(usrMsg, exec, channel, execTime).ConfigureAwait(false);
}
else if (!exec.Result.IsSuccess && exec.Result.Error != CommandError.UnknownCommand)
{
LogErroredExecution(usrMsg, exec, channel, execTime);
if (guild != null && exec.CommandInfo != null && exec.Result.Error == CommandError.Exception)
{
if (exec.PermissionCache != null && exec.PermissionCache.Verbose)
try { await msg.Channel.SendMessageAsync("⚠️ " + exec.Result.ErrorReason).ConfigureAwait(false); } catch { }
}
}
else
{
if (msg.Channel is IPrivateChannel)
{
// rofl, gotta do this to prevent dm help message being sent to
// users who are voting on private polls (sending a number in a DM)
int vote;
if (int.TryParse(msg.Content, out vote)) return;
await msg.Channel.SendMessageAsync(Help.DMHelpString).ConfigureAwait(false);
await DMForwardCommands.HandleDMForwarding(msg, ownerChannels).ConfigureAwait(false);
}
} }
} }
else catch (Exception ex)
{ {
if (msg.Channel is IPrivateChannel) _log.Warn("Error in CommandHandler");
_log.Warn(ex);
if (ex.InnerException != null)
{ {
// rofl, gotta do this to prevent dm help message being sent to _log.Warn("Inner Exception of the error in CommandHandler");
// users who are voting on private polls (sending a number in a DM) _log.Warn(ex.InnerException);
int vote;
if (int.TryParse(msg.Content, out vote)) return;
await msg.Channel.SendMessageAsync(Help.DMHelpString).ConfigureAwait(false);
await DMForwardCommands.HandleDMForwarding(msg, ownerChannels).ConfigureAwait(false);
} }
} }
} });
catch (Exception ex) return Task.CompletedTask;
{
_log.Warn("Error in CommandHandler");
_log.Warn(ex);
if (ex.InnerException != null)
{
_log.Warn("Inner Exception of the error in CommandHandler");
_log.Warn(ex.InnerException);
}
}
} }
public Task<ExecuteCommandResult> ExecuteCommandAsync(CommandContext context, int argPos, IDependencyMap dependencyMap = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception) public Task<ExecuteCommandResult> ExecuteCommandAsync(CommandContext context, int argPos, IDependencyMap dependencyMap = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)