Пример #1
0
        public override async Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)
        {
            input = input.Trim(',', '.');

            var baseResult = await base.ReadAsync(context, input, services);

            if (baseResult.IsSuccess)
            {
                return(TypeReaderResult.FromSuccess(new DiscordUserOrMessageAuthorEntity(((IUser)baseResult.BestMatch).Id)));
            }

            if (MentionUtils.TryParseUser(input, out var userId))
            {
                return(SnowflakeUtilities.IsValidSnowflake(userId)
                    ? GetUserResult(userId)
                    : GetInvalidSnowflakeResult());
            }

            // The base class covers users that are in the guild, and the previous condition covers mentioning users that are not in the guild.
            // At this point, it's one of the following:
            //   - A snowflake for a user not in the guild.
            //   - A snowflake for a message in the guild.
            //   - Something we can't handle.
            if (ulong.TryParse(input, out var snowflake))
            {
                if (!SnowflakeUtilities.IsValidSnowflake(snowflake))
                {
                    return(GetInvalidSnowflakeResult());
                }

                // Try to find the user if they are not in the guild
                var restClient = services.GetRequiredService <IDiscordRestClient>();

                var user = await restClient.GetUserAsync(snowflake);

                if (user is not null)
                {
                    return(GetUserResult(user.Id));
                }

                var messageService = services.GetRequiredService <IMessageService>();

                if (await messageService.FindMessageAsync(context.Guild.Id, snowflake) is { } message)
                {
                    return(GetMessageAuthorResult(message.Author.Id, message.Channel.Id, message.Id));
                }
            }

            return(GetBadInputResult());
        }
Пример #2
0
        public override async Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)
        {
            var baseResult = await base.ReadAsync(context, input, services);

            if (baseResult.IsSuccess)
            {
                return(TypeReaderResult.FromSuccess(DiscordUserEntity.FromIUser(baseResult.BestMatch as IUser)));
            }

            if (ulong.TryParse(input, out var uid) || MentionUtils.TryParseUser(input, out uid))
            {
                if (!SnowflakeUtilities.IsValidSnowflake(uid))
                {
                    return(TypeReaderResult.FromError(CommandError.ParseFailed, "Snowflake was almost certainly invalid."));
                }

                return(TypeReaderResult.FromSuccess(new DiscordUserEntity(uid)));
            }

            return(TypeReaderResult.FromError(CommandError.ParseFailed, "Could not find user / parse user ID"));
        }