Example #1
0
        private async Task OnMessageReceivedAsync(SocketMessage message)
        {
            CalledEventStats.Increment("MessageReceived");

            var messageStopwatch = new Stopwatch();

            messageStopwatch.Start();

            try
            {
                if (!TryParseMessage(message, out SocketUserMessage userMessage))
                {
                    return;
                }

                var commandStopwatch = new Stopwatch();
                var context          = new SocketCommandContext(Client, userMessage);

                if (message.Channel is IPrivateChannel && !Config.IsUserBotAdmin(userMessage.Author.Id))
                {
                    return;
                }

                int argPos = 0;
                if (userMessage.HasStringPrefix(Config.CommandPrefix, ref argPos) || userMessage.HasMentionPrefix(Client.CurrentUser, ref argPos))
                {
                    await LogCommandAsync(userMessage, context, argPos).ConfigureAwait(false);

                    commandStopwatch.Start();
                    var result = await Commands.ExecuteAsync(context, userMessage.Content.Substring(argPos), Services).ConfigureAwait(false);

                    commandStopwatch.Stop();

                    if (!result.IsSuccess && result.Error != null)
                    {
                        switch (result.Error.Value)
                        {
                        case CommandError.UnknownCommand: return;

                        case CommandError.UnmetPrecondition:
                        case CommandError.ParseFailed:
                            await context.Channel.SendMessageAsync(result.ErrorReason.PreventMassTags()).ConfigureAwait(false);

                            break;

                        case CommandError.BadArgCount:
                            await SendCommandHelp(context, argPos).ConfigureAwait(false);

                            break;

                        default:
                            throw new BotException(result);
                        }
                    }

                    var command = message.Content.Split(' ')[0];
                    Statistics.LogCall(command, commandStopwatch.ElapsedMilliseconds);
                    await EmoteChain.CleanupAsync(context.Channel, true).ConfigureAwait(false);
                }
                else
                {
                    await ChannelStats.IncrementCounterAsync(userMessage.Channel).ConfigureAwait(false);

                    await AutoReply.TryReplyAsync(userMessage).ConfigureAwait(false);

                    await EmoteChain.ProcessChainAsync(context).ConfigureAwait(false);

                    await EmoteStats.AnylyzeMessageAndIncrementValuesAsync(context).ConfigureAwait(false);
                }
            }
            finally
            {
                messageStopwatch.Stop();
                Statistics.ComputeAvgReact(messageStopwatch.ElapsedMilliseconds);
            }
        }
Example #2
0
 private async Task OnClientReadyAsync()
 {
     CalledEventStats.Increment("Ready");
     await InitService.InitAsync().ConfigureAwait(false);
 }