Exemplo n.º 1
0
        private async Task MessageReceived(SocketMessage rawMessage)
        {
            await _messageHandlingService !.MessageReceived(rawMessage).ConfigureAwait(false);

            // Ignore system messages and messages from bots
            if (rawMessage is not SocketUserMessage message)
            {
                return;
            }
            if (message.Source != MessageSource.User)
            {
                return;
            }

            var context = new SocketCommandContext(_client, message);

            if (_messageHandlingService.CheckForRepetition(message))
            {
                await context.Channel.SendMessageAsync(message.Content).ConfigureAwait(false);
            }

            if (message.Channel is IDMChannel dmChannel)
            {
                await _messageHandlingService.LogDMMessageAsync(message).ConfigureAwait(false);
            }

            var argPos = 0;

            if (!(message.HasMentionPrefix(_client.CurrentUser, ref argPos) || message.HasStringPrefix(_prefix, ref argPos)))
            {
                return;
            }

            ////ignore msg with prefix only - disabled as per request
            //if (string.IsNullOrEmpty(message.Content?.Replace(prefix, "").Trim())) return;

            var result = await _commands.ExecuteAsync(context, argPos, _services).ConfigureAwait(false);

            if (result.Error.HasValue)
            {
                switch (result.Error.Value)
                {
                case CommandError.UnknownCommand:
                    await context.Message.AddReactionAsync(new Emoji("❓")).ConfigureAwait(false);

                    break;

                case CommandError.Exception:
                    //await LoggingService.LogException(((ExecuteResult)result).Exception);
                    await message.AddReactionAsync(new Emoji("❗")).ConfigureAwait(false);

                    break;

                case CommandError.ParseFailed:
                case CommandError.BadArgCount:
                case CommandError.ObjectNotFound:
                case CommandError.MultipleMatches:
                case CommandError.UnmetPrecondition:
                case CommandError.Unsuccessful:
                default:
                    await context.MarkCmdFailedAsync(result.ErrorReason).ConfigureAwait(false);

                    break;
                }
            }
        }